-
Write in a clear and consistent coding style. You can see some examples of my code here and here; this is not the exact style you need to follow, but pick a style and stick with it- all of your submitted code should be formatted the same way.
- Code should be self-documenting. Variables should be sensibly named, function names descriptive of their purpose. Reserve comments for places where clarification is valuable: if you have a long piece of code and it’s hard to tell what it does, consider splitting it up into several functions whose names will describe what’s going on.
- Keep your headers clean. Put the absolute minimum required in your headers for your interface to be used. Anything that can go in a source (.cpp) file should. Don’t #include any system headers in your .h files that aren’t absolutely required in that file specifically.
- Use const wherever possible. All member functions that do not modify their object should be const. If your function takes a reference to an object and does not modify that object, you should be passing a const reference.
- Write portable code. Don’t use any very compiler-specific features or depend on long or unsigned types being a particular size. Prefer to use size_t for array indexing, especially when dealing with the STL.
- Don’t leak memory. Every heap allocation using new should have a corresponding delete.
- Prefer using nullptr to NULL.
- Prefer using auto as long as it helps clarity. For example, auto dirPtr = Directory::create( "home" ); is probably better than declaring the type of dirPtr.
- Do take advantage of the standard library. Generally don’t try to rewrite data structures and algorithms that have already been implemented well in the standard library unless you have a good reason.
2 Comments
Leave a Reply
Next Post: The Rule of Three and the Rule of Five
Permalink
Thank you sir. These tips really helped me.
Permalink
Indeed.