Be careful. In my experience, when people come up with a list or a process or a convention, it quickly becomes more important than the code itself. That's because it's easier to fuss over the convention. What really matters is that you write clear, correct code. The way you name your variables is secondary. Also, when there are many people involved, stuff like this turns into a holy war.
I wrote a document on coding conventions at work because our off-shore developers were asking for it. There are only two "required" conventions: (1) put the copyright notice in every file and (2) comment your code. The document addresses many of the other things you've mentioned, but more from a "this is why you might want to do something like this" standpoint.
Prepend local variables with loc_.
Prepend function arguments with arg_. |
I don't think this is necessary. It's common to prefix class members, but the local vars and args are declared right near the usage, so why prefix them?
All data members in a class must be private, and accessed through accessor functions. |
Why stop at data members? Why not make all variables accessible only through accessor functions....? Don't blindly use accessors. They get in the way and frequently aren't needed. You should evaluate each case to decide if an accessor is warranted.
always place [variables] at the top of the function |
As LB pointed out, it's common to put them as close to their use as possible. To me this is a judgement call. The advantage of putting them at the top is that you know where to look to find the declaration. If they are declared as needed, then they will likely be right on screen and you'll save on stack space (when a variable goes out of scope in a code block, its memory becomes available for a new var.
But I think the overriding concern here is the time required to construct/destroy the variable. Consider
1 2 3 4
|
MyClass x; // MyClass constructor takes a long time
for (int i=0; i < 1000000; ++i) {
x.doSomething();
}
|
vs.
1 2 3 4
|
for (int i=0; i < 1000000; ++i) {
MyClass x; // MyClass constructor takes a long time
x.doSomething();
}
|
The second example will run much slower because x is constructed and destroyed a million times instead of just once. True, the meaning is different and you may need to construct a new x each time, but if you don't it would be a shame to waste the time.
All prototypes that are not part of a class must go into a .h file name "Prototypes.h". |
This seems arbitrary. It will be handy to have miscellaneous prototypes in a common place, but frequently you want or need different header files.
Variable names should not be abbreviated, the name should be as descriptive as possible within reason. When looking at a variable or function name, you should know just by looking at it what its purpose is. |
I prefer short names and comments at the declaration to say what they are. That way the code is short and clean but you can still tell what a variable really is.
In my opinion, what you put in your comments is far more important than all the other stuff you've mentioned.
Comment the declaration of each non-trivial function and method, describing the parameters, functionality, return value and error conditions. I like to put the same comment in both the declaration and definition. At the declaration you want to say what the function does, but probably not how it does it. Leave that for the definition.
Comments in the code should say
how and
why you're doing something. The code itself usually says
what you're doing, but you might want to add brief descriptions before each block of code. Sometimes I'll write a description of the steps that the code must perform, put that into comments, and then write the actual code around these comments.
Line length should not be longer than half the width of the editing area. |
As your manager I see that the right side of your screen is always blank, so I'm giving you a monitor that's half as wide.... :) Seriously though, I don't see the reasoning here. To me, you don't want code lines that wrap around the edit window. Also, if the code is indented 17 levels, then it's a pretty good sign that you need to change something.
Re-reading this post, I see that all of the above comments are critical. I want to say that it's a very good thing to think about these issues and come up with some sort of conventions. Much of what you've written is quite reasonable and a good idea.