Whitepaper on Software Development (Part 1)
From the original Xaede archive. This is historical writing, preserved in a new reading edition. Technical advice, tools and project status reflect the time of publication.
Having worked as the lead-geek on a number of disparate projects over the last decade ranging from small one-off applications to multi-million-lines-of-code major enterprise applications and multiplayer games, I have developed a realistic and rather hard-nosed approach to software development revolving around the following principles, namely:
- Maintainability
- First refer to rule number one.
I name maintainability as the primary (and in the above list, sole) principle to software development for a number of highly-practical reasons, which include:
If the code is easy to read, well organized and does not include sections of “clever” code, then an at time I need to I can go back and optimize it for performance, fix bugs and add new functionality. Through a rigorous practice of unit-test-driven development, and time I want to analyze a bug in a given package, add a new function, hand-off the code to another developer, etc. I can simply use the existing unit-test as a baseline sanity-check to make sure that I have not done something with the code that will make it unusable to other coders or layers in the application hierarchy.
Organization
The organizational principles I follow are largely derive from long years of using Unix and Linux systems, and also many years of intensive development using Java. I like to start by defining a source directory in my project root that then contains a list of directories that match the set of languages I utilize for the project. For example, if I wanted to write a search engine in Java with various data-access and performance-optimization extensions in a language such as C or C++, I would start by creating a “src” directory, from which I would create “src/java” and “src/c” or “src/cpp”. That way I can at-a-glance begin to navigate through my source hierarchy. Additionally, this makes searching, especially command-line tools on a Unix or Linux platform very convenient.
From the root location, I might also define a number of other directories based upon the needs of the project, such as a “bin” directory for compiled binaries, a “conf” directory for configuration files, a “data” directory for application-specific data-files, an “assets” directory for game-engine assets, a “docs” directory for project-documentation, a “plugin” directory for external libraries, a “log” directory for application logs, a “dist” directory for JAR files compiled from Java class files, and a “unit” directory to contain compiled unit-tests.
In a typical project, I usually have a few handy command-line bash-shell scripts in the “bin” directory for various convenience routines I might need, along with symbolic-links to Perl scripts from the “src/perl/tools” directory. In the “conf” directory, I will have my default Doxygen and JavaDoc configuration files, in the “docs/doxygen/[project package name]” and “docs/javadoc/[project package name]” directories I can browse the current source-code documentation. I typically also have business plans, technical design documents and project notes in the “docs/business-plans”, “docs/design” and “docs/notes” directories. The root of the project directory (which also is the root of the CVS or SVN repository for the project) I will have the appropriate Make, Ant or Microsoft Visual studio project files I need to compile and execute the source code, unit tests and applications contained within the project.
Since all of my projects are organized around this same simplified layout, I always know where to go to find anything I might need at any given moment during the software development lifecycle.
Coding Standards
As I mentioned previously, a good coding standard has several primary goals which make your life as a programmer much easier:
following a consistent standard across all source in a given project makes the project much easier to maintain, enhance and hand-off
a good coding standard, by definition should outline practices that enable a programmer to naturally implement policies that cut down on common mistakes
All code should be prefixed with a multi-line set of comments that define the following set of information: the name of the source-code file (which at-a-glance will tell you if you run into a copy-paste nightmare), the appropriate copyright notice, a succinct description of the functionality provided by the code, and the name of the original author (so you can immediately go to him or her with questions without having to check your versioning-system’s history).
When writing code, the use of hard to read or understand – i.e. clever code – should almost always be avoided (there are of course exceptions to every rule, however they should be few and far between and made only for mission-critical reasons, e.g. embedding assembly to optimize a math function for a game engine, etc.).
The source code itself should be self-documented by the use of good naming conventions, including consistent syntax, verb-noun (i.e. English-language-style function, class and variable names) naming, and standardized indentation. Therefore the use of comments should be kept to an absolute minimum. This is because comments often disagree with the actual program logic due to the nature of programmers who over time update the code but not the comments. Then, when it comes time to fix a bug or make an enhancement, even the original author might not know if they should follow the comments or follow the code.
It does not matter if you like variables to be named color_highlight_value or colorHighlightValue as long as they are consistent across the entire project. Additionally, abbreviations, single-character and abstract naming conventions should be entirely avoided. It is very, very difficult to read code where all of the variables are named like “a1”, “c12”, “b3c7”, etc. instead of something that is self-describing. Additionally, it is much easier in the long run to have a variable named “isEnabled” rather than “isEnbld” (or worse, “b12”) - you only have to type two additional characters to make it easier to read.
When it comes to single-character variable or function names, the primary reason to avoid them is to make all of your source easily searchable in your text editor of choice. For example, it is common to use the letter “i” to denote a iterator value in a for-loop. However trying opening up a source-file with a thousand lines of code and search on the letter “i”. It is much easier if the iterator variable is either properly named, or it is at least two characters, such as “ii”. e.g.
void exampleForLoopFunction()
{
int ii = 0;
for(ii = 0; ii < 16; ii++) {
// see how searchable this is
}
}
…additionally, always define variables at the top of a function or method so that the person coming along behind you can read the set of variables the function is dealing with before attempting to understand the logic of the function. Also, it is very important to always initialize variables when they are defined, where possible, even in a language like Java which does this for you order to always clearly indicate to another programmer what you are doing with the state of your set of variables. This also cuts down on common mistakes related to variable initialization.
In order to clearly define your program logic intention, you should always denote the scope of the code you are writing in consistent fashion. For example, if you prefer a newline after a function definition and the curly-brace, always do so. Never use statements such as “if”, “for”, “while”, etc. without following it with a scope definition. Skipping this may easily lead to making simple mistakes which could have been fixed by typing the two extra characters.
Here is an example, copied verbatim from an undisclosed open-source C project intended to return the name of the a state represented by an integer:
const char *
chk_st_str(int st) {
switch(st) {
case 1: return "good";
case 3: return "bad";
case 7: return "unknown";
}
return "???";
}
…here is what I did after a quick refactor pass:
typedef enum _State {
STATE_GOOD = 1,
STATE_BAD = 3,
STATE_UNKNOWN = 7,
STATE_ERROR = -1
} State;
const char *stateToString(State state)
{
char *result = "Error";
switch(state) {
case STATE_GOOD:
result = "Good";
break;
case STATE_BAD:
result = "Bad";
break;
case STATE_UNKNOWN:
result = "Unknown";
break;
}
return result;
}
I’m out of time to continue this post, so here ends part 1 of my thoughts on good practices in software development.