Practical Object-Oriented Design in Ruby
These are notes I took while reading POODR by Sandi Metz a while ago. If you’re a Ruby dev and haven’t read POODR, I highly recommend it.
All of code snippets below are examples from POODR and come from this repository.
Part 1 of those notes is here.
Ruby tips
Here are hints on how to write better Ruby code I noted down. They are short and might be meaningless when out of context, but they may still bring some value or refresh your memory if you read the book.
Simple classes
Use Struct
and OpenStruct
in place of simple classes. Main difference is that Struct
takes initialization arguments
in order, and OpenStruct
accepts a hash from which it derives attributes.
attr_reader
Use attr_reader
or private method in objects instead of instance variables.
It turns data into behaviour at once and by doing so it hides data:
Constructors
Using fetch(args[:something], default)
can be more handy than args[:something] || default
because fetch
returns default
only when key wasn’t found. ||
returns default
also when args[:something]
is nil
or false
.
Template Method Pattern
Template Method Pattern consists of a method that is used as default value. It is then overwritten in classes that inherit or include it:
Using hook methods allows subclasses to optionally override behaviour. This way you can remove super
calls and achieve less coupling:
Composition
A Bicycle
can be composed of Parts
which respond to spares
message. Parts
holds many Part
objects.
Use Forwordable
and Enumerable
for classes like Parts
to make it act similar to Arrays.
Summing up
I know that I’m praising POODR once again, but as you can see it contains not only design and architecture tips. There’s also a lot of practical advice on writing Ruby code. I noted down only those that were important for me at the time of reading, there are a lot of other hints that can’t be shortened to a note and are worth reading yourself.
You can find Part 1 of those notes here