What Is a Good Naming Convention for Vars, Methods, etc in C++

What is a good naming convention for vars, methods, etc in C++?

Do whatever you want as long as its minimal, consistent, and doesn't break any rules.

Personally, I find the Boost style easiest; it matches the standard library (giving a uniform look to code) and is simple. I personally tack on m and p prefixes to members and parameters, respectively, giving:

#ifndef NAMESPACE_NAMES_THEN_PRIMARY_CLASS_OR_FUNCTION_THEN_HPP
#define NAMESPACE_NAMES_THEN_PRIMARY_CLASS_OR_FUNCTION_THEN_HPP

#include <boost/headers/go/first>
#include <boost/in_alphabetical/order>
#include <then_standard_headers>
#include <in_alphabetical_order>

#include "then/any/detail/headers"
#include "in/alphabetical/order"
#include "then/any/remaining/headers/in"
// (you'll never guess)
#include "alphabetical/order/duh"

#define NAMESPACE_NAMES_THEN_MACRO_NAME(pMacroNames) ARE_ALL_CAPS

namespace lowercase_identifers
{
class separated_by_underscores
{
public:
void because_underscores_are() const
{
volatile int mostLikeSpaces = 0; // but local names are condensed

while (!mostLikeSpaces)
single_statements(); // don't need braces

for (size_t i = 0; i < 100; ++i)
{
but_multiple(i);
statements_do();
}
}

const complex_type& value() const
{
return mValue; // no conflict with value here
}

void value(const complex_type& pValue)
{
mValue = pValue ; // or here
}

protected:
// the more public it is, the more important it is,
// so order: public on top, then protected then private

template <typename Template, typename Parameters>
void are_upper_camel_case()
{
// gman was here
}

private:
complex_type mValue;
};
}

#endif

That.
(And like I've said in comments, do not adopt the Google Style Guide for your code, unless it's for something as inconsequential as naming convention.)

What are the naming conventions in C#?

There is the All-In-One Code Framework Coding Standards from Microsoft which contains a complete set of rules and guidelines. (also used to be available here)

This document describes the coding style guideline for native C++ and .NET (C# and VB.NET) programming used by the Microsoft All-In-One Code Framework project team.

Naming functions, methods, pointers, variables, arrays etc in C++

Example, int* house should be named
int* house_p, so that when someone
reads the code, he doesn't need to
scroll all the time wondering if a
thing is a pointer, array, matrix, or
whatever...

But what if its type changes - are you going to go through all your code and change the names of all the variables. And what if the variable is an instance of a complex type:

ComplicatedDerivativeFinancialInstrument x;

What suffix will you use?

What you are asking about is known as Hungarian notation - its use in C++ is almost universally considered to be A Bad Idea.

What are the preferred conventions in naming attributes, methods and classes in different languages?

As others have said, things vary a lot, but here's a rough overview of the most commonly used naming conventions in various languages:

lowercase, lowercase_with_underscores:

Commonly used for local variables and function names (typical C syntax).

UPPERCASE, UPPERCASE_WITH_UNDERSCORES:

Commonly used for constants and variables that never change. Some (older) languages like BASIC also have a convention for using all upper case for all variable names.

CamelCase, javaCamelCase:

Typically used for function names and variable names. Some use it only for functions and combine it with lowercase or lowercase_with_underscores for variables. When javaCamelCase is used, it's typically used both for functions and variables.

This syntax is also quite common for external APIs, since this is how the Win32 and Java APIs do it. (Even if a library uses a different convention internally they typically export with the (java)CamelCase syntax for function names.)

prefix_CamelCase, prefix_lowercase, prefix_lowercase_with_underscores:

Commonly used in languages that don't support namespaces (i.e. C). The prefix will usually denote the library or module to which the function or variable belongs. Usually reserved to global variables and global functions. Prefix can also be in UPPERCASE. Some conventions use lowercase prefix for internal functions and variables and UPPERCASE prefix for exported ones.

There are of course many other ways to name things, but most conventions are based on one of the ones mentioned above or a variety on those.

BTW: I forgot to mention Hungarian notation on purpose.

What is the correct naming notation for classes, functions, variables etc in c#?

Classes should be in camel notation with the first letter capitalized

public class MyClass

Functions and Methods in C# should act in a similar fashion except for private methods

public void MyMethod()
private void myPrivateMethod()

Variables I tend to do a little differently:

Member Variables

private int _count;

Local variables

int count;

Naming convention - underscore in C++ and C# variables

The underscore is simply a convention; nothing more. As such, its use is always somewhat different to each person. Here's how I understand them for the two languages in question:

In C++, an underscore usually indicates a private member variable.

In C#, I usually see it used only when defining the underlying private member variable for a public property. Other private member variables would not have an underscore. This usage has largely gone to the wayside with the advent of automatic properties though.

Before:

private string _name;
public string Name
{
get { return this._name; }
set { this._name = value; }
}

After:

public string Name { get; set; }

C++ Naming Convention

I don't think there is a "widely accepted" naming convention in C++. Stroustrup's books and papers generally has the following coding style:

class Shape {
public: // interface to users of Shapes
virtual void draw() const;
virtual void rotate(int degrees);
// ...
protected: // common data (for implementers of Shapes)
Point center;
Color col;
// ...
};

You also may want to look at popular style guides like The Google C++ Style Guide.



Related Topics



Leave a reply



Submit