6

I am a Java developer who is starting to pick up more and more C++/C (yes I know they're different, bear with me). One thing that struck me as odd was the different naming conventions used by these languages. In Java, is is very common to see something like this:

int someVariable = someFunction(someParameterToPass);

However, in C++/C, it would look a bit different:

int some_variable = some_function(some_parameter_to_pass);

The reason I ask is because I (alone, no team) have just started a large project using C++, and right now am using Java naming conventions in my code (it's what I'm used to). But then I add in some external libraries to my code and it mixes the naming conventions together. I understand the code easily, and use the differences to tell my code apart from library code.

Should I switch my naming convention for the sake of readability to others (while the LOC is relatively low), or stick with what I'm doing now (since I am the only developer)?

Note - my code has been peer reviewed, however, the reviewers mainly focused on the function of the code. They didn't mention mixed conventions, and I did not specifically ask them about this. They also didn't have experience with switching naming conventions.

syb0rg
  • 181
  • 1
  • 11
  • possible duplicate of [How would you know if you've written readable and easily maintainable code?](http://programmers.stackexchange.com/questions/141005/how-would-you-know-if-youve-written-readable-and-easily-maintainable-code) – gnat Jul 20 '13 at 21:59
  • 3
    @gnat My code is readable and easily maintainable... I would say that link is applicable, but not a duplicate. – syb0rg Jul 20 '13 at 22:08
  • @sybOrg _"readable and easily maintainable"_ - who told you so? That certainly have to be someone else but me. I _flipped conventions_ like you describe few times, and every time there was a period of _mixed things_ while I was getting used to _new order_, and it always felt painful – gnat Jul 20 '13 at 23:42
  • 1
    @gnat My code has been peer reviewed. You should put some of your experiences into an answer, since you have encountered this situation before. – syb0rg Jul 20 '13 at 23:55
  • @sybOrg were your reviewers happy about such a mix? – gnat Jul 20 '13 at 23:57
  • @gnat They didn't mention it, they mainly focused on the function of the code. So I figured I would come here to ask about it. – syb0rg Jul 21 '13 at 00:19
  • @sybOrg why didn't you ask them instead of / prior to coming here? – gnat Jul 21 '13 at 00:20
  • 2
    @gnat Because I would consider this a more "official" place to get an answer for this. My reviewers have not had some of the experiences that some people here have had (switching naming conventions is one of them). – syb0rg Jul 21 '13 at 00:24
  • 2
    I am a heavy C++ users and I prefer version one: `int someVariable = someFunction(someParameterToPass);` All my code looks like that. I don't think that it is style in naming conventions that is going to trip you up. – Martin York Jul 21 '13 at 17:15

2 Answers2

12

This is where you find the big issue with coding standards based on style - if your team doesn't write the entire codebase, then you're going to find you have mismatches with the other code's standard.

So, my advice is not to sweat it. As long as your code is clear, it really doesn't matter whether you use camel case, pascal case, or underscore style. Its more important that the code is readable.

You should never alter the style of the 3rd parties as that will make comparison with new versions impossible, so you have to stick with them. If you have 2 different-style libraries, you have no choice but to follow a standard that ignores code style. Its not that bad, if you're a good coder you can read any code style. If you're not, having a solitary style won't help you at all.

gbjbaanb
  • 48,354
  • 6
  • 102
  • 172
  • 2
    You are *currently* the only developer. It may not be likely, but things could change. – Dan Pichelman Jul 20 '13 at 23:47
  • @DanPichelman True, plus I suppose he is writing in scope of a larger audience that do have teams of developers. – syb0rg Jul 20 '13 at 23:51
  • _"you have no choice but..."_ -- hmm [Anti-Corruption layer](http://programmers.stackexchange.com/q/184464/31260) provides "a way to transition/work around legacy code or bad APIs..." – gnat Jul 21 '13 at 08:04
  • @gnat - you assume the lib is poor quality, which is what the anti-corruption layer is there for. However, using a facade (of any flavour) just to change the way the words look is where you need to stop and take a good look at why you'd want to do something like that. A facade to wrap the way you access an API is one thing, just to change its coding style is quite another. – gbjbaanb Jul 21 '13 at 21:14
  • @gbjbaanb well, here I agree. Thing is though, where to draw the line between _just changing words_ and addressing substantial issues. Which, I believe, ultimately brings us back to topics covered in: [How would you know if you've written readable and easily maintainable code?](http://programmers.stackexchange.com/questions/141005/how-would-you-know-if-youve-written-readable-and-easily-maintainable-code) – gnat Jul 21 '13 at 21:22
3

The naming convention that you cite is from the standard library. The naming convention is practical rather than prescriptive. That is, the standard library must introduce symbols in order to use the library. The standard library wants to limit those symbols to a fixed set so that developers using the library are not surprised when there may be a name collision.

The standard library has chosen to use all lower case names with underscore separators.

Your choice as a developer is to use that convention or another convention determined by you and your team. Using the same convention may read nicer, but can also cause name collisions (which are often resolved using namespaces, which can add to the noise in the code.)

The common C++ (non-standard library) convention is quite close to Java.

From your example, a C++ naming convention would typically use a capitalized function name and a capitalized class name. To extend your example:

int someVariable = SomeClass.SomeMethod(someParameterToPass);

Another difference is the JavaBeans convention of preceding method names with get, set, or is prefixes. Many C++ conventions use a similar convention. However, some groups prefer to use the ability of C++ to overload method names so that get and set have the same name but different signatures.

class Foo
{
public:
    Bar GetBar() const;
    void SetBar(const Bar& bar);
};

vs

class Foo
{
public:
    Bar Bar() const;
    void Bar(const Bar& bar);
};

Some groups prefer to not use overloading, preferring the former convention.

Bill Door
  • 1,090
  • 8
  • 8
  • Many people have function names that start with a lowercase letter (maybe that's just Java, I'm not completely sure). Class names usually start with an uppercase, but a function name with an uppercase adds a bit of confusion, and usually you will get called out on it (at least on StackOverflow). – syb0rg Jul 22 '13 at 03:03
  • Curious. Not that Google is the authority, but my experience and the Google style guide suggest capitalized function names. I often say that naming is the hardest part of programing. :) – Bill Door Jul 22 '13 at 14:55