Naming conventions

  • Thread starter catharticmomentusenet
  • Start date
C

catharticmomentusenet

I apologise because I am aware there have already been a large number
of posts on this topic, but I couldn't find one making precisely the
points I wanted to make :)

I am a senior developer at a company that looking at adopting coding
standards for .NET. Many of the standards available on the net are
based around Microsoft's guidelines, and recommend that, since .NET is
strongly typed, hungarian notation is unnecessary and should be
avoided.

To a large extent I agree with this - as long as variable names are
chosen properly then I find concerns over .NET variable types to be
very rare - definitely not worth the decrease in readability that
hungarian creates.

However, in one respect I differ from the guidelines - naming of Gui
controls. It is quite common to have several controls that are
conceptually related on a form, and for them all to have similar names.
I find hungarian notation to be a very useful shorthand in this case.

For example, consider a label "Please enter your name" and an
associated text box. I have seen code that would declare these as:

Label userNameLabel;
TextBox userNameTextBox;

This seems to me to be unnecessarily wordy compared to the hungarian
alternative:

Label lblUserName;
Textbox txtUserName;

When the shorter notation is regularly employed and everyone is used to
it, then the hungarian variant is more readable - since often the
operations being performed are the most important part of the code, and
excessively long variable names can cause these operations to become
obscured.

In many cases, the type of a variable is often either irrelevant or
obvious from context. For example if text is assigned from a user name
control then the important thing is that a user name has been entered,
not the type of the control. Hence it doesn't make sense to me for the
type to take up so much of the variable's name. The only reason to
include the type at all is to distinguish the two closely related
controls. If anyone has a better method for distinguishing user
controls with similar purposes then please post it.

Let me know your opinions.

Thanks,

Chris Williamson
 
M

Mattias Sjögren

Chris,
Let me know your opinions.

Since readable code is more important than reducing typing to me, I
don't buy the "shorter name is better" argument. If you use a decent
code editor with code completion, you don't have to type it all in
anyway.

Specifically for the case with Labels used to label other controls, I
rarely have to modify the label control in my code so the label
control's name doesn't really matter. With VS2005 I set GenerateMember
to false. Another approach is to encapsulate the label and the other
labeled control in a UserControl.


Mattias
 
C

catharticmomentusenet

Since readable code is more important than reducing typing to me, I
don't buy the "shorter name is better" argument. If you use a decent
code editor with code completion, you don't have to type it all in
anyway.

I would agree completely that readable code is more important than
reducing typing. However, length is not a virtue in and off itself, and
my point is that in this particular case the longer variable names do
nothing to convey the intentions of the author and, if anything,
distract attention from the role the variable plays in a form, and
towards it's type (which is much less important) .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top