General tips:
1) Be consistent and correct with your indentation. This is absolutely the single most important thing ever when it comes to code readability.
2) Don't be afraid of whitespace. Adding a blank line here and there to separate 'ideas' in your code is very helpful. Much like how you'd make a separate paragraph in English text.
3) Try to keep functions small and with a singular purpose. If a function tries to do too many things it becomes difficult to follow. If doing multiple things in a function is unavoidable (or is impractical), then at least separate the concepts with whitespace and comments.
4) Write "good" comments. By "good" I mean a comment should not describe a single line of code. The comment should describe the overall process of what you're doing (only if it's not obvious) and/or why you're doing it. Example of a bad comment:
|
int foo = 5; // assign 5 to foo
|
It's obvious to anyone that that's what that code is doing, so that comment is worthless.
5) Try to make the code "self-documenting". That is.. function names reflect what the function actually does. Variable names reflect what the variable represents, etc.
6) Avoid the comma operator.
7) Don't try to cram too much on too few lines. Compact code is often difficult to read code. Spread it out.
UNLESS there are a bunch of one-liners that are straightforward... in which case cramming them together
with pretty print (vertically aligned) is acceptable... and arguably even preferred. Example:
1 2 3 4 5 6 7 8 9 10 11
|
class Clock
{
//...
inline bool operator < (time rhs) const { return timestamp < rhs; }
inline bool operator > (time rhs) const { return timestamp > rhs; }
inline bool operator <= (time rhs) const { return timestamp <= rhs; }
inline bool operator >= (time rhs) const { return timestamp >= rhs; }
inline bool operator == (time rhs) const { return timestamp == rhs; }
inline bool operator != (time rhs) const { return timestamp != rhs; }
//...
};
|
That's about all I can think of as far as rules go. Really the best thing you can do is just look at your code and figure out whether or not it's hard to read. If it is, clean it up so it's easier to read.