DEV Community

Discussion on: Ruby Science Saturday: use Mixin Modules or Reopen Classes? 👩‍🔬🔬🧬🧪

Collapse
 
ludamillion profile image
Luke Inglis

One of the big reasons I’ve preferred mixins over reopening classes is that it ‘reads’ a bit better. To me there is less cognitive load to thinking about collections of like behavior separated out into modules which are self-documenting rather than having the same class opened in multiple places which can ‘hide’ behavior.

I usually take the ‘middle way’ when trying to thin out my classes and extract common behavior into service objects.

Collapse
 
schwad profile image
Nick Schwaderer

I think you're right. There's a benefit to using well-trodden patterns to keep code readable for others as well.

Plus in my mind I do like one file being the 'home base' for a class, and then using mixins to group and store methods used by it elsewhere.

The class-reopening-approach definitely is used a lot more in gems though (in my experience!)