Skip to content

Instantly share code, notes, and snippets.

@coreyhaines
Created November 6, 2010 11:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coreyhaines/665346 to your computer and use it in GitHub Desktop.
Save coreyhaines/665346 to your computer and use it in GitHub Desktop.
Can you spot all the SRP violations? Can you spot the abstractions that are screaming to come out? There's at least one OCP violation, too.
def tick(cell, number_of_neighbors)
if number_of_neighbors < 2 || number_of_neighbors > 3
cell.setAlive false
end
if number_of_neighbors == 3
cell.setAlive true
end
end
@JonKernPA
Copy link

regarding SRP... well, this is an "Arrogant" type method, why ask an object (who is just a stupid worker anyway) to do work it should be responsible for when you can just "do it all" right here?

regarding OCP... not sure if you mean the mere fact that the cell object's live/die behavior is completely controlled externally? That is, I can randomly change life to be when there are 4 neighbors instead of 3.

On a side note, couldn't they at least have collapsed the "==3" bit into an "else?"

@cgorshing
Copy link

I'm a little lost on this - maybe a little more context is needed for me to understand what is going on.

My initial thought would be to change it into:
def tick(cell, number_of_neighbors)
cell.setAlive number_of_neighbors == 3 #pull out 3 into some meaningful name
end

But I don't understand what the tick method is for ... tick and setAlive don't go well for me. I might also pull the boolean expression out into its own method so it would read something like:
cell.setAlive ifCloseNeighbors

But in doing this, setAlive and ifCloseNeighbors don't meld well for me either.

I would like to hear other peoples interpretation.

@coreyhaines
Copy link
Author

Sorry about that. This is a method for handling the ruleset for conway's game of life (GoL): http://en.wikipedia.org/wiki/Conway's_game_of_life

@JonKernPA
Copy link

context? shoot, that makes it too easy .

Essentially, this code is invoked for each "tick" of the clock to see what the next "generation" looks like for each cell.

You can see my first (well, second) Game of Life code -- and you can run it locally to watch each tick: https://github.com/JonKernPA/gol-2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment