Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
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
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
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 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. –  Loki Astari Jul 21 '13 at 17:15
show 4 more comments

2 Answers 2

up vote 12 down vote accepted

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.

share|improve this answer
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 provides "a way to transition/work around legacy code or bad APIs..." –  gnat Jul 21 '13 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 '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? –  gnat Jul 21 '13 at 21:22
add comment

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.

share|improve this answer
    
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 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 '13 at 14:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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