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.

9
  • 3
    @gnat My code is readable and easily maintainable... I would say that link is applicable, but not a duplicate.
    – syb0rg
    Jul 20, 2013 at 22:08
  • 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, 2013 at 23:55
  • 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, 2013 at 0: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. Jul 21, 2013 at 17:15

2 Answers 2

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.

5
  • 2
    You are currently the only developer. It may not be likely, but things could change. Jul 20, 2013 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, 2013 at 23:51
  • "you have no choice but..." -- hmm Anti-Corruption layer provides "a way to transition/work around legacy code or bad APIs..."
    – gnat
    Jul 21, 2013 at 8: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, 2013 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?
    – gnat
    Jul 21, 2013 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.

2
  • 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, 2013 at 3: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, 2013 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.