Created
October 25, 2011 00:06
-
-
Save practicingruby/1310883 to your computer and use it in GitHub Desktop.
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
| # Looking to reduces rows of board game data to | |
| # primitives (for eventual JSON output). | |
| # each cell can be either unoccupied, or occupied by N | |
| # armies of a given color. What data format do you prefer, | |
| # and why? | |
| #---------------------------------------------------------- | |
| # 1) nil indicates empty territory | |
| [[:white, 2], nil, [:black, 3]] | |
| # 2) :empty indicates empty territory | |
| [[:white, 2], :empty, [:black, 3]] | |
| # 3) somewhat homogenous. nil indicates count being non-applicable | |
| [[:white, 2], [:empty, nil], [:black, 3]] | |
| # 4) fully homogenous. 0 indicates no units of either color present | |
| [[:white, 2], [:empty, 0], [:black, 3]] | |
| # your own representation (leave a note in the comments) |
Author
In that case I would go with number 4. I like constancy in my data and api. If the empty cells are going to take up too much space then I would go with 1 to save space.
I go for #4, after all the advantages already said, I'd add the self-documented style.
As you said is a serialization structure what we are defining here, so would be possible to think it is gonna be drunk up by external systems, better then that the info has an implicit way to be understood.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not to go all "appeal to authority" on you, but I just read this last night, and thought it worth mentioning:
The more code we write, the more we're convinced that we should define types to represent value concepts in the domain, even if they don't do much. It helps to create a consistent domain model that is more self-explanatory. If we create, for example, an Item type in a system, instead of just using String, we can find all the code that's relevant for a change without having to chase through the method calls. Specific types also reduce the risk of confusion -- as the Mars Climate Orbiter disaster showed, feet and metres may both be represented as numbers but they're different things. Finally, once we have a type to represent a concept, it usually turns out to be a good place to hang behavior, guiding us towards using a more object-oriented approach instead of scattering related behavior across the code.
-- Growing Object Oriented Software, Guided by Tests