What is naming standard for creating object?

4.5k views Asked by At

As per coding standard object name for class is eg SqlConnection sqlConnection=new SqlConnection();

but when we have class like IPAddress (first 2 char are capital), what will be name of object?

IPAddress iPAddress=new IPAddress();
IPAddress ipAddress=new IPAddress();
IPAddress IPAddress=new IPAddress();

What is best way according to standard.

5

There are 5 answers

0
Mårten Wikström On

I don't like iPAddress. Looks like PAddress is the name of something and that i is just a prefix for that.

I don't like IPAddress. Looks like a type name.

I do like ipAddress. Easy to read. Makes sense.

0
Arun Aravind On

Actually there is no standard for naming. People vouch for Hungarian notation, but its outdated and tedious and a little bit confusing. The only rule is that you should know its purpose on seeing the variable even after a long time. It should be maintainable and understandable to other people referring your code.

That said, the one Daniele proposed is the best you could use in this situation. And sure camelCasing is more readable for variables. pascal for ClassNames or others things in the global scope.

5
Ojonugwa Jude Ochalifu On

You can name the objects with something more meaningful like:

IPAddress IPAddressAccountingDept = new IPAddress();

and so on. It's all your choice.The compiler doesn't care how they are named as long as they do not clash with keywords etc.

0
Martin Liversage On

Naming conventions are just that, e.g. conventions and not rules. However, Microsoft have some design guidelines for developing class libraries and among those conventions are capitalization conventions.

In your case IP is an acronym and because it contains only two letters it is considered a short acronym. The naming rules for short acronymes are

Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier.

So the type should be named IPAddress and if you decide to create a variable with the same name it should be ipAddress.

Note that these are guidelines that Microsoft tries to (and also sometimes fails to) follow when creating the .NET base class library. You do not have to follow these guidelines but doing that will make your code consistent with .NET.

0
daniele3004 On

The best way is to use meaningful names to instances of objects. Start with lower case letters and follow with upper case.

For example:

IPAddress ipHome = new IPAddress();
IPAddress ipWork = new IPAddress();
IPAddress ipGirlFirend =new IPAddress();

or

IPAddress ipAdressHome = new IPAddress();
IPAddress ipAdressWork = new IPAddress();
IPAddress ipAdressGirlFirend =new IPAddress();

The standard is the rule that you choose to follow in your development according to those who work with you.

This could be interesting http://en.wikipedia.org/wiki/CamelCase