Computer Science Canada How to write good C++ code |
Author: | wtd [ 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:
Good:
Blank lines Precede and follow control structures (if/else, loops, switch) with a blank line to distinguish them from following code. Bad:
Good:
Put braces on their own lines. Bad:
Good:
Do not put space between function names and the parentheses surrounding the argument list. Bad:
Good:
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:
Good:
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:
Good:
Do not declare variables before necessary. Bad:
Good:
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:
|
Author: | OneOffDriveByPoster [ 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. |
Author: | btiffin [ 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 |
Author: | OneOffDriveByPoster [ 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:
Good:
|
Author: | md [ 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. |
Author: | Minicar [ 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/ |