I try to keep the code clean and standardized, but if there's something I've learned, standards/best practices are hard and ambiguous and confusing and with no consensus, specially when you work with multiple languages, and learning them all would take forever. So I just try to stay on the sensible side. Clean code is nice, but if I start worrying too much about that kind of stuff I just bog myself down when the player doesn't care about any of that, they just want features.
Also when I first started the project I was working with Java at university so there's probably a lot of influence there. One class per file, getter/setters, camelCase, _members, etc. I'm sure that makes C++ gurus break out in hives, but I've kinda grown fond of the style.
Still if you wanna improve the code, be my guest. If it's sensible and consistent and easy to maintain, I'll try to adopt it. As long as I don't end up losing sight of my own code.
To be more specific:
Passing objects by value to functions - Most places in the code base pass std::string objects by value. This causes extra copies of the objects to be made. These can easily be replaced by a const reference, ie "const std::string&"
Well I tend to use strings like simple types (int etc.), won't I end up accidentally changing an object's string when I don't want to? Like:
string stuff(const string &s)
{
s = "lalalala";
return s;
}
Including "using namespace std" in header files - This is usually not a good idea to use "using namespace x" in a header file. This forces anyone who includes the header file to use that namespace.
Fair enough, I just can never remind myself to put std:: everywhere.
Use const on getter methods - It is a good idea to make getter methods const, this prevents member variables from being modified. For example, if you had a method "Target* getTarget()", it would change to "Target* getTarget() const"
Isn't that a bit restricting? Like requiring all related variables to be const or never changing or something? Gawd knows I probably have some half-assed code buried in there somewhere that might break.
Hard coded game data - I saw a couple of classes that had game data hard coded, example XcomRuleset. This may be to due to the current state of the project, I would think eventually you would want to clean this up.
This will be moved to external files later on when time permits.
Code duplication - I saw a couple of places where code was copied and pasted with a few things changed. Example Globe::cachePolygons(), same logic copied and pasted, just the data structures being operated on were different. It would be better to create a function instead of duplicating the code.
Every once in a while I get lazy or tired, and prefer to just get something done rather than doing it all neat and tidy.
After all it's still just my code, it probably won't get all spread around and used by various dangerous people like a library, so I can afford to be a bit lenient. If it's really bad I usually go back to it later to clean it up though. Although the Globe is pretty much one of the biggest ugliest classes and I never enjoy poking it.
I don't quite understand this one. The compiler already won't let you run code like "getTarget() = &DummyTarget".
I think a const method stops the implementation from changing the object and thus prevents mistakes like "getTarget() { _target = 1000; }". Don't quote me on that though.