NGen
|
The ngen
project will be using Doxygen for documentation generation. Initially, we'll follow the Javadoc C-style standard. This means we'll see documentation like:
Each class should have comments detailing what it's used for and why. Each function and method should also have what those are used for, why, and descriptions of their parameters. Comments should be dotted throughout functions for complex lines describing intent rather than implementation.
Variable names should be descriptive, yet concise nouns. While a lay person may not understand that what and why of the source, they should be able to glean a rough idea as to what the implementation is.
Control structures should also have descriptive names, even though variables like i
, j
, k
, and l
are fairly standard. Algorithms based on matrix interpretation become very difficult to read with short names. Take a longest common subsequence example:
On first glance, it is difficult to make out:
a) What the function is supposed to be doing (what does lcs
stand for?) b) How exactly X
and Y
are meant to be used c) What i
is supposed to be indexing d) What j
is supposed to be indexing c) What is supposed to be stored in L
When in doubt, follow the example given by C++ and Boost (though it would be prudent to stray away from some of their names like point_t
that might hide intent). That means that most class
, variable
, function
, and parameter
names will be in snake case. Examples of snake case are regex_search
, forward_list
, and variable_name
. enum
s, however, should be represented with a leading upper case, like Web_color
. Macros and template parameter names should always be defined as ALL_UPPER_CASE
.
In the end, err on the side of readability; code that strays from the standard, yet is easy to read is more valuable than code that sticks to the letter of the law, yet is hard to work with.
ALWAYS make sure that you guard your headers as such:
#ifndef EXAMPLE_H #define EXAMPLE_H . . . #endif // EXAMPLE_H
Make sure to leave the comment at the end if the file is long, just as a common courtesy.
Use spaces in lieu of tabs. Tabs are easier to type, but may cause code to become very difficult to read on systems with odd spacing. If you tab your code a couple times, it may look fine on your machine, but code like
might look like the following when moved to a different machine:
\t
, as entered by the 'tab' key, is generally rendered as four consecutive spaces. Before you start writing, you should ensure that your editor of choice interprets the 'tab' key as four spaces. Most, if not all, editors support this.
In VIM, for example, you can set the tab key to insert spaces via:
set tabstop=4 shiftwidth=4 expandtab
Editors like Visual Studio Code are already set to convert tabs to a language appropriate number of spaces.
Bracing should follow either the classical Kernighan and Ritchie style or it's popuplar offshoot, the 'One True Brace Style'.
A block of K&R will look like:
The opening brace for the outer level element lies on a newline while the rest of the opening braces appear on the same line. All closing braces appear on a new line at the same indention as its parent. Braces around single line functions in control statements are optional.
That same block in 'One True Brace Style' looks like:
This is identical to K&R in all but two respects: opening braces are always on the same line and control statements always have braces.
Lines should be kept at or less than 120 characters.