3 Matching Annotations
- Jun 2021
-
stackoverflow.com stackoverflow.com
-
I've seen (and fixed) Ruby code that needed to be refactored for the client objects to use the accessor rather than the underlying mechanism, even though instance variables aren't directly visible. The underlying mechanism isn't always an instance variable - it can be delegations to or manipulations of a class you're hiding behind a facade, or a session store with a particular format, or all kinds. And it can change. 'Self-encapsulation' can help if you need to swap a technology, a library, an object specification, etc.
-
a principle I use is: If you have an accessor, use the accessor rather than the raw variable or mechanism it's hiding. The raw variable is the implementation, the accessor is the interface. Should I ignore the interface because I'm internal to the instance? I wouldn't if it was an attr_accessor.
-
But sure, go ahead and enforce self-encapsulation if you like; it makes it easier to do memoization or whatever later on.
Tags
- public vs. private interface
- good policy/practice/procedure
- making it easy for later
- encapsulation
- accessors
- making it easy for later refactoring
- go through accessor instead of using instance variable directly
- good idea
- good explanation
- best practices
- good point
- I agree
- self-enforced
Annotators
URL
-