C++ Style

There are plenty of resources concerning C++ coding style, or coding style more generally.

Here I describe some of my own programming style preferences that I have not seen described elsewhere.

Delimiting Comments with Blocks

Sometimes it is possible to surround the scope of a comment with braces:

//here's a comment
{
  statement one;
  statement two;
}

However, this is not always possible. It may not be possible, for instance, if the sequence of statements includes declarations.

Limiting the Scope of Variables

Code might be clearer if the scope of variables is kept as short as possible. This is particularly applicable to temporary variables containing intermediate values.

int dist2;
{
  int temp_x2 = x * x;
  int temp_y2 = y * y;
  dist2 = temp_x2 + temp_y2;
  // [scope of temp_x2 and temp_y2 ends here]
}

Again, this is not always possible. For example, if the main variable is a constant, it can not be declared and defined separately in C++.

Multiple Exit Points

Usually, I prefer to have only a single return statement in a function.

Sometimes it is convenient also to have a return statement at the beginning of a function (perhaps checking some pre-conditions). In that case it is still clear what is happening in the code, and in this case the return might be preferable to a long conditional with its additional indentation.

On rare occasions, it may be genuinely clearer and simpler to have multiple exit points throughout the body of a function. In this case, the presence of these should be stated at each end of the function body:

int my_function (int x)
{
  // <multiple exit points>
  // ...
  return 0;
  // ...
  return f(x);
  // ...
  // <multiple exit points>
}

These comments should be included whether or not the return statements have an expression (i.e. whether or not the function has void return type).

Similarly, if a loop body contains exit or skip points, that should also be clearly marked:

for (int i = 0; i < x; ++ i)
{
  // <has break>
  // ...
  if (condition)
  {
    break;
  }
  // ...
  // <has break>
}

Likewise for ‘continue’, or even ‘return’ from inside a loop within a function body.

Switch Fall-through

Usually, each case of a switch statement will have its own break.

I like to ensure that even the final case has a break. If another case is appended, it is then less likely that the required break will be forgotten.

If it is intended that the body of one case should be executed after that of the previous case, then this should be highlighted by a comment. This will clarify the intent, and will prevent ‘hyper-correction’ of the code (when someone adds a break that they assume was forgotten).

switch (var)
{
case 1:
  // ...
break;
case 2:
  // ...
// <fall-through>
case 3:
  // ...
break;
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: