Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# naming convention for variables with same data but different types

I've consulted a few msdn articles that address c# coding conventions and naming guidelines (C# Coding Conventions and Naming Guidelines) as well as a similar question on stack overflow from a few months back.

I'm not certain how to handle two variables which are in scope at the same time, that hold the [conceptually] same data in different types.

An example which would help illustrate the issue would be an identifier that is initially held as a string, but is then cast/parsed to an integer.

I've come up with 3 possible courses of action, and I'm sure that I missed plausible options.

COA #1:

int iRecordId;
string sRecordId;

Where one or both of the variables prefix a type abbreviation. This violates the MS coding guidelines stating not to prefix parameter names with Hungarian type notation.

COA #2:

int recordId;
string recordIdString;

Where one or both of the variables explicitly state the type in the name. This just seems klunky and while it doesn't use Hungarian notation, it seems to violate the spirit of the previous COA.

COA #3:

int recordIdForDatabase;
string recordIdFromUrl;

Where each variable is further qualified by where the data came from, or is going.

Proposal

My thought is that I ultimately want to distinguish between two variables that only differ by type, so while there are guidelines that explicitly state not to prefix variables with type information, I'm leaning towards using a Hungarian prefix. Since it is in stark contrast with the naming conventions in the remainder of the code, it seems that it will highlight the extenuating circumstance. Is this a reasonable approach?

Questions, comments, and cries of outrage are all welcomed.

like image 455
xelco52 Avatar asked Dec 03 '12 19:12

xelco52


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

Why store the data twice? Could you put the record id into an int, then call ToString() when you want to transform it?

By only storing the data once, you leave yourself less susceptible to errors in your code.

If the variables each store different values, then name them logically. E.g,

int socialSecurityID;
string driversLicenceID;
like image 188
Nick Vaccaro Avatar answered Oct 12 '22 10:10

Nick Vaccaro