One thing that I'm thinking of is the old subject of coding style. Every software house tends to have a house style, so at work you just adopt that - but at home, on personal projects I find myself drifting around stylistically, at least with naming conventions.
There's a few main styles that I tend to consider ok for me:
.NET Style
Microsoft have a standard set of style guides for .NET / C# programs which for the most part people adopt (although not always, even within Microsoft public code).
The .NET style is simple:
- PascalCase for class names
- PascalCase for public methods (all methods, in fact)
- Interface classes start with an "I"
- Public properties/variables are PascalCase
- camelCase for variable names
- Namespaces are PascalCase
- Assembly Names are PascalCase
Code would look something like this:
namespace MyProject{ public interface IBar { } public class Foo : IBar { public void DoSomething(int parameterName); public int SomeProperty { get; set; } }}
It's worth noting that the get/set property functions end up becoming something like "SomeProperty_get"/"SomeProperty_set" under the hood.
Java Style
Java programs also have a common style.
- PascalCase for class names
- Interface classes start with an "I" (but not always)
- camellCase for public methods
- Public properties/variables are camelCase, prefixed with getXX() or setXX() (as Java doesn't have properties)
- camelCase for variable names
- Namespaces are lowercase
- Package Names are lowercase
In Java, the above example looks something like:
package myproject;public interface IBar { }public class Foo implements IBar{ public void doSomething(int parameterName); public int getSomeProperty(); public void setSomeProperty(int value);}
C Style / C++ Standard Library Style
C++ seems to have a tonne of styles. One of the first ones you'll come across is that of the standard library, which appears to have adopted the C style convention, largely due to remaining consistent with the old code from yesteryear.
Here, we have:
- lowercase_delimited for class names
- Interface (abstract) classes aren't special
- lowercase_delimited for public methods
- Public properties/variables are lowercase_delimited, there doesn't seem to be a get/set style (?)
- lowercase_delimitedfor variable names
- Namespaces are lowercase
Back to our example:
namespace myproject{ class bar { public: virtual ~bar() { } }; class foo : public bar { public: void do_something(int parameter_name); int get_some_property(); void set_some_property(int value); };}
Looking through some other sources and the Google C++ guide seem to lean to a blend of the Java Style with C++/Standard library style.
eg:
- PascalCase for class names
- Interface classes start with an "I" (but not always)
- PamellCase for public methods
- Public properties/variables are lowercase_delimited, prefixed with my_property() or set_my_property()
- camelCase for variable names
- Namespaces are lowercase_delimited
This leads to:
namespace my_project{ class Bar { public: virtual ~Bar() { } }; class Foo : public Bar { public: void DoSomething(int parameterName); int some_property(); void set_some_property(int value); };}
With all this, the Google version seems to make a lot of sense.
What's your style and how did you pick it?
Honestly, I have one simple rule for my own code and for any code I have control over, and that's "Do as the standard library does".
It may not be perfect, but it has two great big benefits -- 1) everyone knows it and can at least read it. 2) Frankly, rather than trying to think about all the corner cases and having to justify your decisions to everyone who disagrees, I can just get all hand-wavey and say "If its good enough for the standard, its good enough for you."
The second part isn't even a lie -- its just such a blunt instrument that the shear weight of it will bludgeon any decentor -- so I don't feel bad about it. At all.
The standard library of any language, essentially out of necessity, exercises all kinds of odd cases your coding standard will miss; its been there, done that, and got the tee shirt. Furthermore, using the same naming convention as the standard forces you to resolve any ambiguous names or collisions explicitly -- sure you might not have had them under a different naming convention, maybe, but I prefer to resolve these kinds of issues now, rather than unknowing leaving them to lie in wait due to a bit of serendipity.
Besides all that, think of the trees you'll save, and CO2 (read: hot air) you'll keep out of the atmosphere by not having to design, share, debate, and defend yet another naming convention. Think of all the braincycles you've spared yourself by deferring to the 100s of man-years of expertise that the standard library designers represent, and all the hours of your life you can have back and use to, you know, write code.