Created
May 12, 2014 14:47
-
-
Save billeisenhauer/5844e96dd5b8e7f4c441 to your computer and use it in GitHub Desktop.
Illustrating effects of bloated protocols. Which class promotes God classes? Assume a HeatRegulator class collaborates to heat a room when needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LeakyRoom | |
DESIRED_MIN_TEMPERATURE = 72 | |
attr_accessor :temperature, :occupied | |
end | |
class Room | |
def heat_needed? | |
# Policy based upon temp and whether occupied. | |
end | |
private | |
attr_accessor :temperature, :occupied | |
DESIRED_MIN_TEMPERATURE = 72 | |
private_constant :DESIRED_MIN_TEMPERATURE | |
end | |
Also, I've never seen a defense of the "indent your privates" before. Makes sense now!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah, interesting. I can see how
LeakyRoom
could grow unwieldy if it had more aspects to it than just checking to see if the heat needs to be turned on. When that happens, I'd likely extract an entire collaborator:The preference of
attr_reader
overattr_accessor
is a compromise that reduces the surface area of the public API just enough to make the object immutable. If I take away the getters, thenHeaterSwitch
can't collaborate withRoom
. In some cases, however, I won't expose readers at all. In that case, I won't declare private accessors, I'll just use ivars:Here, I want to make sure access to the
@furniture
array is always guarded by a check to see if the furniture would fit.I think I'm seeing two very valid styles here; 1. pare down the public API as much as possible vs. 2. preserve enough public API to introduce collaborators. For me,
HeaterSwitch
is my private API. And when I eventually need to change how we detect if the heater should be on, I've got a context to work with where things likeDESIRED_MIN_TEMPERATURE
are first class citizens.Unfortunately, I've seen some God classes that had very tiny public APIs because they allowed too many features to flow through a humungous private API. I've also seen some cases of "too many objects." Both scenarios are preferable to original
LeakyRoom
case, though. I think the discipline of creating good object APIs is a bit of a lost art, sadly.