Best naming conventions to initialize variables ?

 Posted by Chvrsri on 8/26/2011 | Category: Design Pattern & Practices Interview questions | Views: 7992 | Points: 40
Answer:

Many new developers do not concentrate on prefixes of the variable declarations. But these initialization plays a major role because for large projects a single module will not be handled by a single individual so it must be generic that every one in the team should understand for what purpose a particular variable has been declared by just reading its name itself. So these are the generic steps to initialize variables.

------------------------------------------------------------------------------------------
Data Type---------------Example---------------Prefix
------------------------------------------------------------------------------------------
Boolean ------------------------ blnResult ------------------- bln
Byte ------------------------ bytYes ------------------- byt
Char ------------------------ chrSelect ------------------- chr
Date ------------------------ datStart ------------------- dat
Double ------------------------ dblSalary ------------------- dbl
Decimal ------------------------ decAverage ------------------- dec
Integer ------------------------ intStdid ------------------- int
Long ------------------------ lngValue ------------------- lng
Single ------------------------ sglStock ------------------- sql
Short ------------------------ shoShort ------------------- sho
String ------------------------ strEmpName ------------------- str
Object ------------------------ objSource ------------------- obj


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Kishork80 on: 8/26/2011 | Points: 10
We can also follow Hungarian notation. examples.

bBusy : boolean
chInitial : char
cApples : count of items
dwLightYears : double word (systems)
fBusy : boolean (flag)
nSize : integer (systems) or count (application)
iSize : integer (systems) or index (application)
fpPrice: floating-point
dbPi : double (systems)
pFoo : pointer
rgStudents : array, or range
szLastName : zero-terminated string
u32Identifier : unsigned 32-bit integer (systems)
stTime : clock time structure
fnFunction : function name

Posted by: Vishvvas on: 9/3/2011 | Points: 10
The naming conventions are more characterized by convenience and personal choices, it difficult to say some method to be the best. The hungarian notation where prefix are employes is probably most widely used and accepted conventions. But the the advent of dynamic languages and LINQ vriables like var, the prefix way doesn't find audience nowadays. The naming conventions which are followed in the framework are finding more suitability as it keeps the consistency in the framework implementations and you code. Microsoft has released the coding standards and coding conventions where usage of abbreviations, hypens,underscore, hungarian notation are discouraged. Rather Camel casing for vairbles, Pascal casing for types, classes, properties are recommende. e.g
class HelloWorld
string firstName;

public property BackColor;

Also verbose-ness is preferred over short forms e.g
count vs cnt, index vs idx;

As stated above, there is nothing called best convenions but the criteria for adopting coding conventions could be
1. Established by framework developers i.e. Microsoft
2. Matching with the framework being used
3. Verbose and more readable
4. contraints posed by legacy practices

For more details about Coding standards, conventions

http://csharpguidelines.codeplex.com/releases/view/46280

Hope this helps.
Posted by: Ksuresh on: 9/3/2011 | Points: 10
Thanks

Regards
Suresh
Posted by: Brotherofprimerib on: 12/6/2016 | Points: 10
This question should be removed or updated. Prefixes have no place in OOP and modern software development. Perhaps this can be flagged as "Legacy only", "VB6/VBScript only" or something similar. Anyone that uses prefixes in modern development languages is missing the boat and only creating technical debt for others.

Login to post response