Lead Developer, Stardock Entertainment
Published on July 31, 2009 By CariElf In Elemental Dev Journals

We've got a new coder starting on Monday and in order to help him out, my co-workers and I have been compiling a list of guidelines about our coding practices.  Up until now, I've just sat down with new coders and given them a tour of our codebase and tried to convey our coding practices at that point, but new people always have a lot to remember and having a document to refer to might help.  I thought that I'd share our list with you and see if you have any suggestions to add.

                                          Stardock Dev Guidelines

Read Effective C++ 3rd Edition (More Effective C++ is redundant as it's older than the 3rd edition, which compiled the earlier edition of Effective C++ and More Effective C++) and Effective STL.  You'll recognize some of Scott Meyer's tips in the list below, and it will help you write better code. 

1) Make Get functions (functions that just return a variable) const, unless it's returning a pointer or reference to a class that needs to be modified. Also use const correctness when passing variables.

2) Name variables so that they tell something about the variable.

Don't use single letter variables. Even if it's x y and z for coordinates, use the prefix to indicate what type it is.

Try to avoid generic variable names; instead of ulIndex, use ulShipIndex when iterating through a list of ships.

Class names should have a C in front of them unless they're functor, e.g. CBasicGameObject

Basic types should have a prefix before the variable name:

- b for BOOL,

- n for INT,

- l for LONG,

- ul for ULONG,

- f for FLOAT,

- d for DOUBLE,

- sz for null terminated strings,

- str for STL strings,

- c for CHAR (or TCHAR),

- by for BYTE (unsigned char)

Class member variables should have m_ as a prefix, e.g. BOOL m_bInitialized;

Pointers should have a p in the name, e.g. CMapData * m_pMapData;

References can have an r in the name, e.g. CMapData & rMapData;

Global variables should have g_ as a prefix, e.g. CResourceManager * g_pResourceManager;

3) Use the typedefs for the basic types; if we have to port our code to another platform, it will be easier to add in a header with the #defines. Try to avoid using ints as the size of ints can vary based on the OS, use LONGs instead. Use BOOL instead of bool when possible.

4) Game objects, data classes, and graphic resources should be ref counted. So derive them from CCoreRefCount.

5) STL algorithms make your life easier and can be faster than doing the same work manually. It's faster to call std::for_each on a vector and pass it a functor than to loop through the vector yourself. If you do need to loop through a vector yourself, use iterators instead of accessing the elements with the [] operator, particularly if you're going to delete items.

6) #include "Kumquat3D.h" in every header file.

Try to avoid using #include for other files in your header file. Use forward declarations if possible in the .h, then use #include in the .cpp. It helps with compile times.

7) Check for existing code before you re-invent the wheel, and ask about code you're not familiar with before changing it.

8) Use Skype + IRC. If you don't need an immediate answer to a question, this is less disruptive. It takes at least 15 minutes to get back in the zone once you get out of it. Also, you can seach chat logs for conversations that happened over IM and you can't for verbal conversations.

9) Get a Google e-mail for Google docs, and a Windows Live ID for Mesh.

10) When designing your code, think about if this is code that may be used again. If so, it may belong in Kumquat3D rather than the game project, or write a base class in Kumquat3D and inherit it in the game project so that you can use app specific code.

11) Write detailed change log entries. A text file to be used for the change log is saved with each project.

12) Commit the entire module at once so that there's less chance of code being left out, and so that people who are trying to update while you're comitting your changes won't get partial changes and have to update again when you're done.

13) Don't hard-code strings, use the string file for screens and data files for strings. Also try to avoid hardcoding filenames.

14) Use TCHAR types and their functions, and use _T() for literal strings. Not all of our code uses TCHARs, but we're converting it as we go.

15) Write wrapper functions instead of accessing screen code directly from game code, if it's absolutely necessary that the code be done in the screen code rather than game code.

16) Use this-> when calling member functions.

17) It's often useful to create typedefs for container classes, so that if you change from using a vector to a deque, you don't have to search and replace in your code, and even if you don't, it makes the code more readable.

e.g. typedef std::vector<CBasicGameObject*> BasicGameObjectArray;

18) Try to remember to get someone to update to grab your changes after you've commited them so that you can make sure that you've checked everything in and that everything works on someone else's computer. Or check it out yourself on your testbox and make sure that it compiles.

19) Function names should indicate what the function does. Get means that it's returning a value; if you're calculating the variable, use Calculate or Convert or something of that sort in the title. If you're doing more than one thing in the function, consider whether you should be breaking it up into smaller functions, particularly if you find yourself copying and pasting code in the same function. Breaking the functions into smaller functions may also help you make the task more efficient.


Comments (Page 2)
7 Pages1 2 3 4  Last
on Jul 31, 2009

Wow. I love how nothing of that made any sense whatsoever to me.

on Jul 31, 2009

good stuff.  I like how this was put in the elemental journals with what I assume to be Galactic Civilization variables, classes, and functions.  I mean ulShipIndex isn't a variable in Elemental, right?   how similar is the elemental and GalCiv2 code?

I was always frustrated by not hardcoding strings.  I guess I don't like keeping track of several files at once, though I can see how it would be easier to track down problems with strings later.

on Jul 31, 2009

landisaurus
I was always frustrated by not hardcoding strings.  I guess I don't like keeping track of several files at once, though I can see how it would be easier to track down problems with strings later.

Translation is where it really shines. We write bilingual (English/French) software where I work. I only speak English. So, when I write the software, I'm writing in English. At some point later on in the development cycle, when the strings are put into a strings file of some type (I'm using .net resource files, but I've also used a formatted text file in other cases), I can just send that file to the translation department.

When it comes back, I drop it in, and poof, the french version exists. To do that with hardcoded strings means you have to *find* all the strings to send them for translation, which is itself a PITA.

on Jul 31, 2009

Does Stardock use any scripting languages?

We're integrating support for Python in the Elemental engine.  The marketing dev uses Javascript, but I'm not sure if any of the webdevs do.

good stuff. I like how this was put in the elemental journals with what I assume to be Galactic Civilization variables, classes, and functions. I mean ulShipIndex isn't a variable in Elemental, right? how similar is the elemental and GalCiv2 code?

Yeah, I used GalCiv names because they're a bit more recognizeable.   The Elemental engine is a cleaned up and updated version of the GalCiv2 engine.  Before a new major project we tend to go through and do an overhaul on the engine because we learn a lot over the course of a project, and technology updates.

I was always frustrated by not hardcoding strings.  I guess I don't like keeping track of several files at once, though I can see how it would be easier to track down problems with strings later.

That, and you can't localize hardcoded strings. Also, hardcoding filenames means that modders can only replace files with those filenames, not add new ones. And again with the searching.  Some of our coders who are no longer here hardcoded strings in GalCiv2: Dread Lords because they wanted to get the work done quickly, and they figured that they could change it later.  Then came the time to localize GalCiv2 and I had to do a search for quote marks to find all the hardcoded strings.  It was not fun. 

The goals of our guidelines are to make the code readable, make it easier to maintain, make it reusable, make it stable and make it efficient.

 

on Jul 31, 2009

This is completely unintelligible. But I love it anyway.

on Jul 31, 2009

We usually use IClassName for interfaces (pure abstract classes).

Also, never use a bool as argument to change the behavior of a function call. Make two functions, or pass an enum with a descriptive name.

on Jul 31, 2009

I have a few suggestions.

 

First type the stack contents in every line of code as a comment.  This helps immensely when troubleshooting random errors, and helps you from developing memory leaks.

 

Second, don't calculate anything in the main program.  Have all calculations done with functions.  Also, make sure each function is short and don't calculate more than one thing in each.  Splitting up the functions like this helps organize the program as you go along, but the most beneficial part of this is that it allows very easy modification of the program.  Need to add a few features? Just adjust a few functions to call out additional functions that your new features will use.  It also makes sure you understand the interrelationships between the functions, so changing one thing won't adversely affect other things in your program.

 

Also, The C Programming Language (Ansi C) by Kenigan and Ritchie is one of the best reference books to have around.  It is a very good book for anyone in the industry. (Even if you aren't just a beginner)

on Jul 31, 2009

Consistent indentation

Every now and then I have to fix up some old script from programmers long past, and the indentation is often completely chaotic.  I just have to go through the file and reformat everything before I can even begin to make sense of it.

 

on Jul 31, 2009

Read Effective C++ 3rd Edition (More Effective C++ is redundant as it's older than the 3rd edition, which compiled the earlier edition of Effective C++ and More Effective C++) and Effective STL. You'll recognize some of Scott Meyer's tips in the list below, and it will help you write better code.

Just for hilarity, here's something that took me a quick websearch to understand...

You weren't reffering to being able to read / work in a specific version of C++ (something that didn't make much sense), you were reffering to two books that I will now have to buy once my financial aid paycheck comes in.

Ron, don't feel too bad.  You'll always learn more once you get some practical experience than you did at school.  A lot of the issue is just scale; the kind of scale you can get in a school project doesn't usually compare to the kind of scale you work with in a professional environment.  Some colleges and universities offer a class called something like "Senior Project" where you have to spend at least a semester working on a large project, sometimes sponsored by a local business.  This is a great opportunity that I saw a lot of my classmates blow off by undertaking too small of a project.  I probably spent at least 30 hours a week on my Senior Project.  Of course, I'd gotten permission to use my project at Stardock as my Senior Project so I was getting paid for that time. (If you're interested, check out LightWeight Ninja.)

Yeah, my school requires either a senior project (3 credit class) or take two of the 'chain' courses.  Probably for much the reasons you're outlining.


Naturally, I went with the latter, and am taking both the video and AI chains -- mainly because I want to take both of those anyway.  Guess it's time to change my mind!  (Basically, there are a bunch of classes that are 'too big' to fit into one semester so they're split into two and called chanis... you're required to take one, two if you want to skip the senior project).

 

Well, at least you confirmed my gradually sinking suspicion that my graduation will not mark the end of my educational career.

 

<<EXPLITIVE STRING DELETED>>

on Jul 31, 2009

CariElf

We don't go overboard on Hungarian notation.  One of the programmers who used to work at Stardock before my time was seriously into it so that if he had a class named classEditFieldsFrame, he used name variables classEditFieldsFrame * pceffEditFieldsFrame.  It was bad.  Now, what we'd do would be call the class CEditFieldsFrame and then delcare the variable like this: CEditFieldsFrame * pPlanetViewFrame; or something like that.

It depends on the type of Hungarian used, too. I agree that the example you sited was a bit overboard. The place I currently work at would call the class EDITFIELDSFRAME and the variable declaration would be "EDITFIELDSFRAME * peffPlanet;" or similar. This scheme has grown on me.

It's interesting to see the style guidlines used at Stardock, thanks for this post.

on Jul 31, 2009

Ron Lugge

Well, at least you confirmed my gradually sinking suspicion that my graduation will not mark the end of my educational career.

In IT the learning never stops.  If you stop you're dead.  Probably why I do support now the past decade because I was tired of keeping up and continuing to take classes and studying.  I can't study anymore nearly 20 years removed from college. 

on Jul 31, 2009

In IT the learning never stops.  If you stop you're dead.  Probably why I do support now the past decade because I was tired of keeping up and continuing to take classes and studying.

Just to reinforce this. College will teach you how to code (at least somewhat) and some important concepts, but software is a life long education process. New languages arise, tools change, etc. Plus you need to always steadily improve your non-code skills, which means reading books on software culture, QA books so you understand how QA is going to test your stuff, design books so you can write code that actually has a plan behind it

Consider that almost all fields that morph like software require continuing education classes. Doctors, lawyers, all civil engineering, even insurance brokers, etc. And I mean require in the legal sense, as in your license to practice will get revoked if you don't fulfill the yearly continuing education criteria (which usually means attending training classes a few times a year).

Software doesn't legally require CE (continuing education), but not much stays put and if you're content to simply forget about keeping up to date then you'll find yourself unemployable in your chosen field after about 15 years.

on Jul 31, 2009

At first I thought that this was clearly intended to go up somewhere else and got posted by mistake ;/

on Jul 31, 2009

KellenDunk
At first I thought that this was clearly intended to go up somewhere else and got posted by mistake ;/

I thought so too. It's odd seeing this in an Elemental Dev journal. Ah well...Good read. Nice reminder on what a n00b I am when it comes to programming.

on Jul 31, 2009

Thanks for the insights Cari. I might be able to find some time to digest this information before I have to go back to university this fall.

P.S. I'm really bad at #7. Is there any good tricks to avoid re-inventing the wheel? Its really a waste of my time to try to code something that already exists, and I can't seem to find.

7 Pages1 2 3 4  Last