Best practices question - how do you name your variables?
-
This is partly with regard to a Lounge thread the other day. FxCop will catch you if you use variables with incorrect spelling. Do you always name variables using standard* English** words? I found that I use only one variable name regularly that is not a standard word,
conn
for connection. 1. Do you make it a point to name variables correctly, liketransactionDate
instead oftrDate
, or, God forbid,trDt
? 2. Where do you make exceptions to the above rule? * Of course, obscure technical terms may, and even should be used in certain cases, but I'm not talking about that here. ** Or any other human language, as appropriate. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
Hmm, looks at code...
PNET_BUFFER_LIST pEthernetBufferList; PNET_BUFFER pEthernetBuffer; PMDL pEthernetBufferMdl; NDIS_WORK_ITEM SendNetBufferFromListWorkItem; PNDIS_OID_REQUEST pOidRequest; PDEVICE_OBJECT pMiniportPDO; PCM_RESOURCE_LIST pResources; LIST_ENTRY SendListHead; KSPIN_LOCK SensListLock; KDPC DoAnAsyncOpenCloseDPC; NDIS_SPIN_LOCK DoAnAsyncOpenCloseDPCSpinLock; PIRP pOpenCloseIRP; BOOLEAN OpenCloseIrpGone; KEVENT OpenCloseFinished; PNDIS_PACKET pCurrTxPacket; IO_STATUS_BLOCK WriteStatusBlock; NDIS_MINIPORT_TIMER ReTxBufferNamedAdapterBufferTimer; PUCHAR ReTxBuffer; SIZE_T ReTxBufferLen;
So, p for Pointers, the usual abbreviations, Len for length, Tx for transmit, Curr for current, then acronyms, DPC, MDl. Apart form that always full descriptive names. Although longer to type, using full names makes detailed comments redundant, it is only necesary to comment a block of code describibg its intended aims. Nunc est bibendum -
Hmm, looks at code...
PNET_BUFFER_LIST pEthernetBufferList; PNET_BUFFER pEthernetBuffer; PMDL pEthernetBufferMdl; NDIS_WORK_ITEM SendNetBufferFromListWorkItem; PNDIS_OID_REQUEST pOidRequest; PDEVICE_OBJECT pMiniportPDO; PCM_RESOURCE_LIST pResources; LIST_ENTRY SendListHead; KSPIN_LOCK SensListLock; KDPC DoAnAsyncOpenCloseDPC; NDIS_SPIN_LOCK DoAnAsyncOpenCloseDPCSpinLock; PIRP pOpenCloseIRP; BOOLEAN OpenCloseIrpGone; KEVENT OpenCloseFinished; PNDIS_PACKET pCurrTxPacket; IO_STATUS_BLOCK WriteStatusBlock; NDIS_MINIPORT_TIMER ReTxBufferNamedAdapterBufferTimer; PUCHAR ReTxBuffer; SIZE_T ReTxBufferLen;
So, p for Pointers, the usual abbreviations, Len for length, Tx for transmit, Curr for current, then acronyms, DPC, MDl. Apart form that always full descriptive names. Although longer to type, using full names makes detailed comments redundant, it is only necesary to comment a block of code describibg its intended aims. Nunc est bibendum -
This is partly with regard to a Lounge thread the other day. FxCop will catch you if you use variables with incorrect spelling. Do you always name variables using standard* English** words? I found that I use only one variable name regularly that is not a standard word,
conn
for connection. 1. Do you make it a point to name variables correctly, liketransactionDate
instead oftrDate
, or, God forbid,trDt
? 2. Where do you make exceptions to the above rule? * Of course, obscure technical terms may, and even should be used in certain cases, but I'm not talking about that here. ** Or any other human language, as appropriate. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
Vikram, Check out http://www.macadamian.com/ There is a code review article too. :) Hmm. I forgot the article URL but that should be searchable from the website though. :( Vasudevan Deepak Kumar Personal Web: http://www.lavanyadeepak.tk/ I Blog At: http://deepak.blogdrive.com/
-
This is partly with regard to a Lounge thread the other day. FxCop will catch you if you use variables with incorrect spelling. Do you always name variables using standard* English** words? I found that I use only one variable name regularly that is not a standard word,
conn
for connection. 1. Do you make it a point to name variables correctly, liketransactionDate
instead oftrDate
, or, God forbid,trDt
? 2. Where do you make exceptions to the above rule? * Of course, obscure technical terms may, and even should be used in certain cases, but I'm not talking about that here. ** Or any other human language, as appropriate. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
I use polish notation to a point. pXxxx for pointers nXxxx for ints and doubles sXxxx for strings aXxxx for arrays bXxxx for booleans tpaXxxx for CTypedPtrArray tplXxxx for CTypedPtrList mXxxx for maps ctrlXxxx for dialog controls I prefix member variables with m_, and global variables with g_, but for local variables in a function, there is no prefix. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
-
ThatsAlok wrote:
Hai you name changes
Long story, but a troll made some outrageous remarks, so I'm getting my revenge tongue in cheek. I'll revert to my real name in a few days. :-D Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
Vikram Shannon wrote:
Long story, but a troll made some outrageous remarks, so I'm getting my revenge tongue in cheek.
Can I get three guesses as to who it was? :laugh: ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
-
This is partly with regard to a Lounge thread the other day. FxCop will catch you if you use variables with incorrect spelling. Do you always name variables using standard* English** words? I found that I use only one variable name regularly that is not a standard word,
conn
for connection. 1. Do you make it a point to name variables correctly, liketransactionDate
instead oftrDate
, or, God forbid,trDt
? 2. Where do you make exceptions to the above rule? * Of course, obscure technical terms may, and even should be used in certain cases, but I'm not talking about that here. ** Or any other human language, as appropriate. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
m_strThisIsHowIDeclareMyStringMemberVariable :cool:
-
Marc Clifton wrote:
I don't use FxCop
But still, how do you name your variables? Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
Vikram Shannon wrote:
But still, how do you name your variables?
Properties and fields I usually don't abbreviate, unless they're ridiculously long. Local variables, I'm fine with short names, even acronyms. So, I might have something like this (this is an example only, and not intended to be an example of any kind of code I'm using!):
public class Foo
{
protected SchemaDomain schemaDomain;public SchemaDomain SchemaDomain
{
get; set;
}public SchemaDomain Clone(SchemaDomain schemaDomainToClone)
{
SchemaDomain sd=schemaDomainToClone.Clone(); // EXAMPLE ONLY!
return sd;
}
}The point being that I try to use informative names for visible aspects of the class, and names for parameters that relate to what is expected, but internally, if we're talking simple functionality, I'll use abbreviations. If the method is more than a few lines (say, 10 or 20) I seem to subconsciously revert back to more descriptive local variable names to keep things a bit clearer. Marc Pensieve -- modified at 8:10 Thursday 19th January, 2006
-
This is partly with regard to a Lounge thread the other day. FxCop will catch you if you use variables with incorrect spelling. Do you always name variables using standard* English** words? I found that I use only one variable name regularly that is not a standard word,
conn
for connection. 1. Do you make it a point to name variables correctly, liketransactionDate
instead oftrDate
, or, God forbid,trDt
? 2. Where do you make exceptions to the above rule? * Of course, obscure technical terms may, and even should be used in certain cases, but I'm not talking about that here. ** Or any other human language, as appropriate. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
FxCop supports a custom dictionary. (I don't know if it supports Unicode.) From FxCop FAQ[^]:
Create a file named CustomDictionary.xml. Add the following XML structure, with the new words (case insensitive) under the <Recognized> node.
<Dictionary> <Words>
<Recognized>
<Word>aNewWord</Word> <Word>AnotherNewWord</Word> </Recognized>
</Words>
</Dictionary>To use the dictionary with all projects, place the file in the FxCop install directory (usually C:\Program Files\Microsoft FxCop). For project-specific dictionaries, place the file in a separate directory along with the project file. For the words to be recognized, you must close and restart FxCop after creating or modifying the custom dictionary.
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
Vikram Shannon wrote:
Long story, but a troll made some outrageous remarks, so I'm getting my revenge tongue in cheek.
Can I get three guesses as to who it was? :laugh: ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
I'll give you one. Go ahead, guess. :-D He's a major PITA. :sigh: Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
-
This is partly with regard to a Lounge thread the other day. FxCop will catch you if you use variables with incorrect spelling. Do you always name variables using standard* English** words? I found that I use only one variable name regularly that is not a standard word,
conn
for connection. 1. Do you make it a point to name variables correctly, liketransactionDate
instead oftrDate
, or, God forbid,trDt
? 2. Where do you make exceptions to the above rule? * Of course, obscure technical terms may, and even should be used in certain cases, but I'm not talking about that here. ** Or any other human language, as appropriate. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
Why do people use for example, strMyName as a variable? Isnt it obvious it is a string? Or how about intCount! Holy cow, its a number of some sort. p for pointers, g for globals everything else just as it is, ie, a full descriptive variable name. Nunc est bibendum
-
Why do people use for example, strMyName as a variable? Isnt it obvious it is a string? Or how about intCount! Holy cow, its a number of some sort. p for pointers, g for globals everything else just as it is, ie, a full descriptive variable name. Nunc est bibendum
fat_boy wrote:
Why do people use for example, strMyName as a variable? Isnt it obvious it is a string?
Because strInteger is a string, and iString a number of strings. What is best:
SetWindowText(Integer); // Integer is text ?? :confused:
Display(Strings[String]); // String as an array index ??? :confused:or
SetWindowText(strInteger);
Display(straStrings[iString]);~RaGE();
-
fat_boy wrote:
Why do people use for example, strMyName as a variable? Isnt it obvious it is a string?
Because strInteger is a string, and iString a number of strings. What is best:
SetWindowText(Integer); // Integer is text ?? :confused:
Display(Strings[String]); // String as an array index ??? :confused:or
SetWindowText(strInteger);
Display(straStrings[iString]);~RaGE();
-
This is partly with regard to a Lounge thread the other day. FxCop will catch you if you use variables with incorrect spelling. Do you always name variables using standard* English** words? I found that I use only one variable name regularly that is not a standard word,
conn
for connection. 1. Do you make it a point to name variables correctly, liketransactionDate
instead oftrDate
, or, God forbid,trDt
? 2. Where do you make exceptions to the above rule? * Of course, obscure technical terms may, and even should be used in certain cases, but I'm not talking about that here. ** Or any other human language, as appropriate. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
Vikram Shannon wrote:
1. Do you make it a point to name variables correctly, like transactionDate instead of trDate, or, God forbid, trDt?
Yes, I'm pretty anal about that.
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
-
This is partly with regard to a Lounge thread the other day. FxCop will catch you if you use variables with incorrect spelling. Do you always name variables using standard* English** words? I found that I use only one variable name regularly that is not a standard word,
conn
for connection. 1. Do you make it a point to name variables correctly, liketransactionDate
instead oftrDate
, or, God forbid,trDt
? 2. Where do you make exceptions to the above rule? * Of course, obscure technical terms may, and even should be used in certain cases, but I'm not talking about that here. ** Or any other human language, as appropriate. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
http://www.codeproject.com/script/survey/detail.asp?survey=438[^]
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
FxCop supports a custom dictionary. (I don't know if it supports Unicode.) From FxCop FAQ[^]:
Create a file named CustomDictionary.xml. Add the following XML structure, with the new words (case insensitive) under the <Recognized> node.
<Dictionary> <Words>
<Recognized>
<Word>aNewWord</Word> <Word>AnotherNewWord</Word> </Recognized>
</Words>
</Dictionary>To use the dictionary with all projects, place the file in the FxCop install directory (usually C:\Program Files\Microsoft FxCop). For project-specific dictionaries, place the file in a separate directory along with the project file. For the words to be recognized, you must close and restart FxCop after creating or modifying the custom dictionary.
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
Yup, I use that. Although, the only 'non-standard' name is conn, which I've added. :-O Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.