This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

To Corey Haines: yet another GoL implementation.  RSS feed

 
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey, thanks for coming and welcome once again!

I'll be rather weird on this one.

Instead of asking you a question, I'll ask You to ask me a question(s) in a form, so I could think further about the design subtleties how to make it better.

Please find the implementation of the GoL I was working on: https://github.com/liutaurasvilda/conways-gol/tree/master/core-lib/src (3 production classes + 3 test classes).


Note (just for reference): this augmented implementation of GoL is a result of long discussions, even arguings among the fellow ranchers in this thread: https://coderanch.com/t/700311/code-reviews/engineering/version-Conway-Game-Life

I couldn't miss this chance. Thank you!
 
author & creator of coderetreat
Posts: 14
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I always look for initially is duplication of the knowledge around the topology.

So, you have `World` which knows that it is a 2-d topology. For example
https://github.com/liutaurasvilda/conways-gol/blob/master/core-lib/src/main/java/io/github/liutaurasvilda/gol/World.java#L67-L71
and
https://github.com/liutaurasvilda/conways-gol/blob/master/core-lib/src/main/java/io/github/liutaurasvilda/gol/World.java#L48-L53

But you also have a `Location` which knows it.
https://github.com/liutaurasvilda/conways-gol/blob/master/core-lib/src/main/java/io/github/liutaurasvilda/gol/Location.java

That puts a limitation on ease of change.

I'd isolate that somewhere.
 
corey haines
author & creator of coderetreat
Posts: 14
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rules are always an interesting question of how to implement them.

So, looking here
https://github.com/liutaurasvilda/conways-gol/blob/master/core-lib/src/main/java/io/github/liutaurasvilda/gol/Cell.java#L7-L9

There are a couple things that make me go hmmmm:

`next` is a rather vague name for the function. Is there a better, more intention-revealing name for what this is doing?

Then, we have this
https://github.com/liutaurasvilda/conways-gol/tree/master/core-lib/src/main/java/io/github/liutaurasvilda/gol#L8-L9


There are these magic numbers here. Why 2 and 3? Why the or?

Is there an intention-revealing name that can be put here? What does the explanation say about the 2 and the 3? Well, if there are less than 2, then it dies as if by underpopulation, greater than 3 by overpopulation. 2 or 3 feels like sort of "just right population" or something. Could the meaning of the rules be encoded somehow? Maybe something like



The `this` in there is also a bit confusing to me. Why DEAD or this? Isn't it really DEAD or ALIVE? The code might be more clear if it was


It seems as though the purpose of `Cell` itself is just to store the rule. Perhaps the name of `Cell` would be more clear if it was `EvolutionRules` or some such like that?
 
corey haines
author & creator of coderetreat
Posts: 14
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also prefer to see an actual infinite, unbounded grid, not a torus (infinite, bounded) like you have here.
 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

corey haines wrote:

The `this` in there is also a bit confusing to me. Why DEAD or this? Isn't it really DEAD or ALIVE? The code might be more clear if it was

It seems as though the purpose of `Cell` itself is just to store the rule. Perhaps the name of `Cell` would be more clear if it was `EvolutionRules` or some such like that?


Love the comments!

We did this exercise today at work and one of the programmers asked "How do you know you've gone too far?" I have a feeling the "underpopulated(livingNeighbors)" would have felt like it was going too far. I love the suggestion to encode the 2 and 3 though.

From those questions/suggestions, I might refactor to:
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey, what would your reply be to the question of "How do you know when you've gone too far with the refactoring?"

Certainly, there are people who will question the value or even the need to do the "encoding" that you suggest. I would simply say, "Well, doesn't it make the intent of that expression clearer though? Is that not valuable? And how much time does it take to do that anyway? Maybe 10 seconds, including thinking of a good name to give and running the tests again? Is that next level of refactoring really that much of a bother?"

When you're already battling some resistance to these ideas, is it worth picking this fight and maybe winning the battle but losing the war?
 
Liutauras Vilda
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey, thankful for the review. And subtle tips!

Endless possibilities to discover something new.
Will absorb your thoughts and will try to code in.
 
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
!@Liutauras

For me, the most important question is indeed when to stop, as I asked in a previous topic about GoL.

Junilu's code snippet is a good example, for me going a tad too far. That previous topic started with the comprehensible method 'world.setAlive(x, y, true/false). But the code snippet now talks about the enum 'TransitionRule', formerly known as 'Cell', and with methods 'isAliveInNextWith', 'isStable' and 'isFertible'. Also, the values are 'WHEN_ALIVE' and 'WHEN_DEAD'. What is it that can be 'When_ALIVE'?

Is all this really more expressive than what we started with? And again, why the fixed values (2 and 3 or replacement names) in an enum? What if we wanted to experiment with some other values? Now, I've seen quite some good examples where code is being made more expressive indeed, but the danger is ending in something that looks more like getting involved in a game of words.

Well, this is not the right place to restart the discussion. But since it is an intriguing subject, no doubt we will have more of these in the near future.
 
Junilu Lacar
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:What is it that can be 'When_ALIVE'?



The applicable rule can be "WHEN_ALIVE". I was envisioning a usage like this:

 
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!