Bloodman wrote:What conventions are you using and why?
My goal when writing code is to have code the is easily understood later then I read it. My style in writing code is not designed for the ease of putting code down. I spend much more time reading code than I do writing it -- even my own code. Consider it this way, I write a block of code once and then every other time that code is on my screen, I am reading it. So, my coding style will likely be a bit different than SpyderTL's.
I write most of my code in C and occasionally C++. If I can write code in 2 lines or 5 lines, and the 5 line version has a more apparent meaning, I use the 5 line version (with higher level languages, I leave the optimization to the compiler).
I start variables and members with a lower case. I start methods and functions with an upper case letter. I prefer not to use underlines or all lower case:
Anecdote from school wrote:A programmer was writing a driver for a plotter. He wanted to have a variable to track the state of the pen against the paper, and decided to assign it 2 possible values: penisup and penisdown.
Since my code is not only read by me, I try to avoid such ambiguities.
I do not mix unary operators with other operations; they will typically appear on a line by themselves.
I surround operators with spaces:
When defining a function (not an inline function) I put the brackets on a different line than the function declaration:
Code: Select all
void foo(int bar)
{
return GetSomeValue();
}
I keep parenthesis close to the functions (since that are part of the signature, esp in C++), but keyword and parenthesis are separated by a space:
Code: Select all
if (check == True) {
DoSomething();
} else {
DoSomethingElse();
}
Notice, I have the opening brace on the same line as the compound statement. I only skip the braces when the entire compound statement is on a single line:
Yes, this is a valid exception to the above rule on unary operators.
I do not put spaces between class and member names when separated by the "::" operator (again since the whole name is part of the signature):
Code: Select all
class Foo {
public:
static int bar;
};
int Foo::bar = 259085;
Recently, I also started having my editor save tabs as spaces. The reason is that GitHub and other such CVS servers that display code will mangle the tab characters and make the code difficult to follow.
I hope this helps. I am not writing a lot of C code these days, so I'm sure I have forgotten more than I have written.