HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Inventorizing the universe

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
universetheinventorizing

Problem

I recently started re-factoring a project I hadn't touched in many months.

The original purpose was to be an inventory management system for a game I played, Ogame. In this game you play inside a universe filled with galaxies, which are filled with systems, which are filled with planets. Those planets have a coordinate consisting of three parts (galaxy, system, location in system). The usual sizes are 9 galaxies, 499 systems and 15 planets per system, which allows for 67365 planets.

I wrote a pseudo-CSV reader (the first two seperators are : instead of ; because that's what's used in-game as well) which can sort planets based on coordinates or properties. The properties of relevance are 3 mining facilities and the Solar (Power) plant.

Note negative values shouldn't exist. Although I feel it's the user's responsibility not to input those, I'm not checking for it either. This is by design, but may be a bad practice.

The brief UI is also by design. I think it's intuitive enough (especially since I'm currently the only user), but if it's possible to achieve the same result with more elegance I'm all for it.

CSV (Galaxy.txt):

```
1:2:12;20;18;15;0;7;6;4;
1:5:12;20;18;11;0;6;5;3;
1:9:4;10;7;6;0;1;0;0;
1:9:8;17;14;8;0;3;2;1;
1:9:9;18;16;7;0;4;3;2;
1:10:5;9;6;2;0;2;1;0;
1:10:12;2;2;2;0;1;0;0;
1:11:11;19;16;10;0;5;3;1;
1:11:12;23;20;16;0;7;6;5;
1:12:4;2;2;2;0;0;1;0;
1:14:5;4;2;1;0;1;0;0;
1:15:5;20;18;10;0;5;3;1;
1:16:5;18;16;9;0;5;4;4;
1:20:7;24;21;6;0;8;7;2;
1:20:8;22;20;14;0;9;7;5;
1:20:9;23;22;13;0;8;7;4;
1:21:6;11;12;11;0;6;4;3;
1:21:7;23;20;13;0;7;6;3;
1:22:7;14;13;9;0;3;2;2;
1:25:6;19;18;12;0;7;6;3;
1:25:11;16;15;4;0;6;5;3;
1:27:12;12;11;8;0;6;4;2;
1:32:6;19;17;13;0;6;5;3;
1:38:7;22;21;12;0;8;7;6;
1:38:10;18;14;13;0;7;5;4;
1:39:5;18;15;10;0;6;5;4;
1:39:12;20;18;12;0;7;6;4;
1:40:6;9;8;7;0;3;2;2;
1:42:7;19;18;15;6;6;5;4;
1:43:11;19;19;14;5;7;7;7;
1:43:12;19;19;14;5;7;7;7;
1:45:4;20;18;16;0;8;7;7;
1:45:5;20;17;16;0;7;6;5;
1:45:9;15;15;12;0;5;5;4;
1:45:13;15;15;1

Solution

-
You don't need an include guard in Main.cpp

#ifndef MAIN_CPP
#define MAIN_CPP


Include guards are meant to avoid a header file from being included more than once by the compiler/preprocessor (#include works just like text copy-pasting, so yes, the preprocessor is that dumb).

-
For a struct that has all of its members publicly accessible, having get* methods is silly. However, it should be noted that "getters" are normally const methods, to allow them to be called on a const instance and to enforce at compile-time that member class data can't be modified inside the methods. E.g.:

void printPlanet() const;
int getMlvl() const;


Also note that printPlanet() is not a getter, but it also falls into the category of only reading from the member data.

-
I'd suggest moving the class implementations from Universe.hpp into a Universe.cpp file. It will make it easier to navigate just the interface or just the implementation.

-
That summary comment in Main.cpp is superfluous and repeats itself with the declarations in Universe.hpp. If you move the implementation parts to a CPP file, as mentioned above, it will be much easier to read the declarations, so you won't need any extra summary or documentation.

-
If you are open to C++11, then you can replace all those little comparator structures with inline lambdas. This will keep the sorting predicate together with the sort call. Better "locality of reference" to the reader.

-
You should omit empty destructors and let the compiler provide the defaults.

Code Snippets

#ifndef MAIN_CPP
#define MAIN_CPP
void printPlanet() const;
int getMlvl() const;

Context

StackExchange Code Review Q#95007, answer score: 18

Revisions (0)

No revisions yet.