SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • Azure (0)
  • LINQ (5)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box

Naming Conventions and Clarity

(May 01 2007 - 08:46:42 PM by Timothy Khouri) - [print article]

Development Standards

Just like a programming language has a standard set of rules for how to accomplish a desired task, so does development and design in general. While syntax errors are quickly brought to your attention thanks to your compiler, most developers go for years before learning about proper development standards. This article will briefly discuss the small but significant area of naming conventions and clarity.

Bad Coding Habits

One of the first things you may notice when you join a team of developers that you have not worked with before is their "coding style." Usually this is accompanied with bad coding habits that come from bygone days. They may not realize that their code is hideous, but you recognize it right away. How do you know it's bad code? Because you have no idea what it's doing.

A code file strewn with hungarian notation and rediculous prefixes will slow down your development and will nearly kill the progress of anyone else trying to work in your code. We realize that you can't spend all of your time going back to fix your previous work, but here are a few things to avoid when starting your next bit of code.

objHabit to Avoid

Hungarian notation is highly un-semntic because instead of a variable's name representing data, it represents the "type" of data that is contained there in. Example: a variable containing a person's whole name might be "FullName", but using Hungarian notation, you could call it "strFullName." Who is questioning the fact that "Full Name" is a string? No one! But what you have now done is pointlessly named this variable something weird so that the next developer behind you (or even yourself) is going to have to hunt to find what he needs.

// Hungarian notation offers no benefits.
public string strFullName = "Timothy Khouri";

// This is easier to read and makes more sense.

public string FullName = "Timothy Khouri";

wHaT cAsE dO i UsE?

The next common standard you can use is that of casing. All public methods, properties and fields should be ProperCased (also known as PascalCased). All private or internal fields should be camalCased where the first letter is lower cased and all subsequent words are ProperCased. Abbreviations follow an interesting rule: If the abbreviation is two or less letters, then they may all be upper cased (the first would still be lower if it is private); if there are three or more letters, it is treated as a normal word. Examples:

private string fullName;
public string FullName
{
   get
   {
       return this.fullName;
   }
}

private int iD;
public int ID
{
   get
   {
       return this.ID;
   }
}

public string GetHtmlData()
{
   // Notice that "HTML" is written as "Html" even though

   // it is an abbreviation. This is because it is longer

   // than 2 characters in length.

}

Clearly Define Your Objects

The last point that you want to be sure to follow is that of being clear with your variable and function names. Even inside your function body, you should heavily avoid throwing together a meaningless variable name, but should try to define each object as to what it is. This will make your code more readable, and you will be thinking in terms of objects as apposed to thinking in terms of specific object types.

There is much more to standards than these simple guildlines, but starting here will help you to advance as a developer. You will begin to quesiton why you did something a certain way and will discover how to do things the right way.

  • Jul 10 2007 - 03:30:45 PM Josh Stodola

    I prefer to use the Hungarian 3 letter abbreviation, and especially when laying out classes. It makes it far easier to distinct between private variable and their public properties. Another point, say you have a person's age as a property in the class. You really can't be 100% sure its an integer. It could be string becuase you wouldn't normally anticipate any Math to be done on it. It could be integer becuase, well, it's a number. The hungarian notation will immediately tell you. In most cases (like last name), you can easily identify the data type. However, that's not to say that Hungarian notation is a horrible habit and it certainly does not deem it as un-semantic or benefitless. I know that personally I have ran into several experiences in which that 3 letter abbreviation prevented me from having to scroll all the way to the top (or press Ctrl+J and scour the intellisense). Sure it only saved me a few seconds, but I still undoubtedly consider that a benefit.

    Oh, and it's habit, not habbit :)

  • Jul 10 2007 - 07:08:00 PM Timothy Khouri

    Fixed the bad spelling (that's a bat habbit of mine :P)... As for determining private from public fields, the casing would give it away (some people prefix private fields with an underscore which would have the same result). And the "age" property would be an integer as you brought out :) It's likely that people would do calculations such as "years until you're 21". Also, in Visual Studio 2008 (Orcas) you can now create properties just by using the public property's name, which would be named nicely like all properties in the .NET framework.

  • Jul 11 2007 - 12:05:34 PM Jonathan Carter

    Josh: If you've ever read the .NET Guidelines you'd clearly see that Hungarian notation is not recommended for anything. While it doesn't cover private members specifically, the principal is easily applied. If you prefer to use that naming convention that's fine, but you certainly shouldn't be preaching it to people who prefer to follow Microsoft's standards.

  • Jul 12 2007 - 10:13:36 AM Josh Stodola

    Well, Jonathan, I certainly wasn't preaching, I was inputting my feedback on the topic at hand. I think that is why Timothy implemented the comments system in the first place. If you can't handle my/anybody else's opinion, I am sorry for you.

    There are pros to using Hungarian notation. I don't use it religiously, so don't get me wrong. As a matter of fact, I only use it in classes private variables and so-called "temporary" variables used throughout the app. And it can be a time saver, I have concluded this through experience, not stubborness. I forgot to mention in my original comment that hungarian notation is EXTREMELY helpful when naming your controls. txtUsername, txtPassword for textboxes, btnSubmit for buttons, etc. You really can't disagree with this, unless you are so smart that you can easily memorize the ids of many controls. Use intellisense and with this prefix it groups all your controls together. Sooo beneficial!

    I realize there are cons too, but I have *never* had any complaints from others being bothered by my use of the prefixes!!

    Oh, and your comment about .NET guidelines... give me a break! Microsoft changes their mind so much on these ridiculous guidelines that I have learned to ignore them. Seriously, it was only a few years ago that I read a Microsoft Guideline insisting on using the Variant data type for *all* variables. Are you kidding me?!

  • Jul 13 2007 - 08:13:48 AM Jonathan Carter

    I most certainly can handle someone else's opinion, when it is a good one. Hungarian notation is beyond stale, and responsible developers need to make an effort to stop talking about it, as most have.

    "unless you are so smart that you can easily memorize the ids of many controls" - Josh

    I by no means considered myself to be a genius, but I can honestly say that I've never once had an issue remembering what a control of mine is called, and thanks to good conventions, neither has anyone else on my development team, because I name them after what they semantically represent. I share the same convention that most developers I actually respect use, that is controlNameControlType. So for instance: passwordTextBox, stateDropDownList, headerLiteral. While it isn't perfect, it is consistent. If you have two people on the same project, and they both follow that convention, then either one can easily deduce the name of controls without having to give it much thought. In my experience it is much easier to follow someone else's code and not having to be concerned with what type it is. Using the above example, I would have to know that the state control was a DropDownList to find it in intellisense when using Hungarian notation, whereas using the demonstrated convention, I can navigate to the state control and see that it is a DropDownList and work from there.

    Regardless what approach you take to naming conventions, you need to be consistent. If you're using Hungarian notation for control names, but not for everything, then the convention obviously doesn't satisfy enough situations to merit its use.

    As far as your comment about Microsoft's Guidelines changing over time...isn't that exactly what progressive learning is all about? If you can look at your skill level and say you haven't drastically changed in the last year, than that is a problem, so why wouldn't you allow Microsoft the same exception. Following Microsoft's Guidelines is just an extra step in being aware of the current state of the community, as they do a good job gauging developers across the world to get input on what they're doing and using. I don't follow it infallably, but I take it into account and adopt good ideas.

  • Jul 16 2007 - 10:35:43 AM Josh Stodola

    Your points are very valid, and I agree that consistency is the real key. I dont feel I will ever be able to get away from prefixing my control IDs, though. Having them organized within Intellisense by their type is priceless to me.

  • Aug 31 2007 - 07:14:38 AM jimibt

    maybe it'd be nice if you could 'filter' intellisense by classtype (or control type), then the naming convetion prefix/suffix debate would be a moot argument (like a popout menu/list from the right-hand side of the intellisense list containing base class names).

    i think no matter the convention, consistancy is key as noted.

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Check Out Dev++

Test your development skills, give proof to recruiters and employers at dev++

Related Blogs

These are the most recent blog posts related to this article.

  • Implement Custom Printing Logic & Enhanced Word Documents Printing
  • Access & Read Embedded Email Attachments From Existing Email Message
  • Reading HTML/ODS Files & More Enhanced Options for Copying Worksheets
  • Aspose.BarCode for Java and .NET Now Contains Similar Features
  • Export Lists, List Items & Wiki Pages from SharePoint Library to PDF

Related Ads

SingingEels.com as of Feb 04 2012 - 12:38:21 AM - (0.078125)