Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to write good C++ code
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Nov 10, 2008 10:33 pm   Post subject: How to write good C++ code

We seem to have a lot of interest in C++. Here're a few tips on writing code that doesn't suck.

Formatting

Indentation

Use indentation, and use it consistently.

Bad:

code:
void foo()
{
for (int bar = 0; bar < 10; bar++)
{
    cout << bar << endl;

}


Good:

code:
void foo()
{
    for (int bar = 0; bar < 10; bar++)
    {
        cout << bar << endl;
    } 
}


Blank lines

Precede and follow control structures (if/else, loops, switch) with a blank line to distinguish them from following code.

Bad:

code:
void foo()
{
    int baz = 42;
    for (int bar = 0; bar < 10; bar++)
    {
        cout << bar << endl;
    } 
    cout << baz << endl;
}


Good:

code:
void foo()
{
    int baz = 42;

    for (int bar = 0; bar < 10; bar++)
    {
        cout << bar << endl;
    } 

    cout << baz << endl;
}


Put braces on their own lines.

Bad:

code:
void foo() {
    // ...
}


Good:

code:
void foo()
{
    // ...
}
Spaces

Do not put space between function names and the parentheses surrounding the argument list.

Bad:

code:
void foo ()
{
    // ...
}


Good:

code:
void foo()
{
    // ...
}


Do put spaces between arguments in that list, and as well around operators, excluding the dot operator (.) and the indirect access operator (->).

Spaces should not come between * and the variable name when declaring pointers.

Braces

Aside from putting them on their own lines, every control structure that can use braces should have them. This ensures consistency.

Naming

Use meaningful names. Do not be afraid to use lengthy names.

Bad:

code:
int a = 8;


Good:

code:
int acceleration = 8;


Naming schemes:

Variables (and member variables) as well as functions and member functions should be either like_this or likeThis.

Classes should be LikeThis.

Constants should be LIKE_THIS.

Whichever naming scheme you choose, be consistent with it. If the libraries you're using have a consistent naming scheme, use that one to avoid confusion.

Semantics

Declarations

Do not separately declare and initialize variables unless absolutely necessary.

Bad:

code:
int foo;
foo = 42;


Good:

code:
int foo = 42;


Do not declare variables before necessary.

Bad:

code:
int foo = 42;

// Do a bunch of stuff that has nothing to do with foo

cout << foo;


Good:

code:
// Do a bunch of stuff that has nothing to do with foo

int foo = 42;

cout << foo;


Scoping

Scope variables as locally as possible.

Function calls

Do not nest function calls more than two levels deep. Assign results of functions to intermediate variables.

Command/query separation

Where possible, functions (or member functions) which return some useful value, should not modify variables in the program. Make functions or member functions which modify the state of the program explicit about doing so.

Makefiles

Use makefiles. No program is too simple that it isn't worth writing a makefile. Remember, practice makes perfect.

It's not C...

I/O

Do not use C input and output libraries unless absolutely necessary. If it is necessary, explain why in comments.

Strings

Use the C++ string type, rather than char arrays unless absolutely necessary.

Arrays

Use Standard Template Library data structures like vectors or lists rather than arrays, unless absolutely necessary.

Header file guards

Always use header file guards using #ifndef, #define and #endif.

Use header files

Use header files to define interfaces for classes. Implement classes in *.cpp files.

Use the right header files

Don't use legacy "iostream.h" type includes. Use:

code:
#include <iostream>
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Tue Nov 11, 2008 9:09 am   Post subject: Re: How to write good C++ code

wtd @ Mon Nov 10, 2008 10:33 pm wrote:
Put braces on their own lines.
Controversial.
btiffin




PostPosted: Tue Nov 11, 2008 9:57 am   Post subject: Re: How to write good C++ code

OneOffDriveByPoster @ Tue Nov 11, 2008 9:09 am wrote:
wtd @ Mon Nov 10, 2008 10:33 pm wrote:
Put braces on their own lines.
Controversial.

Ditto. Not my preference, and I'm too old to change habits.

Other than that, well done wtd. Keep up the good fight.

Cheers
OneOffDriveByPoster




PostPosted: Tue Nov 11, 2008 11:57 am   Post subject: Re: How to write good C++ code

wtd @ Mon Nov 10, 2008 10:33 pm wrote:
Spaces should not come between * and the variable name when declaring pointers.
May I also suggest:
Qualifiers should be consistently placed. The easiest way to maintain that is to place the qualifier after the rest of the type.

Bad:
c++:
const int *const p;

Good:
c++:
int const *const p;
md




PostPosted: Tue Nov 11, 2008 12:07 pm   Post subject: RE:How to write good C++ code

Braces on their own line greatly improves readability; as does indenting things betwwen then but not the braces. I've seen people do that and it's horrid.
Minicar




PostPosted: Wed Apr 18, 2018 10:24 pm   Post subject: Re: How to write good C++ code

Thank you for writing a very good way to share.


http://www.thaicasinoonline.net/
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: