CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2012
    Posts
    3

    Naming convention for objects?

    I keep looking for this but can't find it. What naming convention is standard for objects that are instantiated?

  2. #2
    Join Date
    Jun 1999
    Posts
    153

    Re: Naming convention for objects?

    Kevin

  3. #3
    Join Date
    Feb 2012
    Posts
    3

    Re: Naming convention for objects?

    Quote Originally Posted by Kevin McFarlane View Post
    Maybe I'm missing it, but it looks like they don't mention a naming convention for objects created from a class. I've been using camel casing, but I've also seen an "o" prefix used. For example:


    XmlDocument() xmlDoc = new XmlDocument();

    XmlDocument oXmlDoc = new XmlDocument();

  4. #4
    Join Date
    Feb 2012
    Posts
    3

    Re: Naming convention for objects?

    Quote Originally Posted by User376 View Post
    Maybe I'm missing it, but it looks like they don't mention a naming convention for objects created from a class. I've been using camel casing, but I've also seen an "o" prefix used. For example:


    XmlDocument xmlDoc = new XmlDocument();

    XmlDocument oXmlDoc = new XmlDocument();
    Typo in first example fixed. No editing posts here? Strange.

  5. #5
    Join Date
    Jun 1999
    Posts
    153

    Re: Naming convention for objects?

    The convention is to use Camel Case for all local variables and method parameters, so your first example would be OK.

    The second example is Hungarian notation and is specifically not recommended in .NET (it says this in the guidelines). See here.

    For member variables the conventions may be more relaxed so it is quite common to use a leading _.

    You'll find that there are various naming conventions elsewhere on the web but the basics tend to be similar to Microsoft's recommendations.
    Kevin

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Naming convention for objects?

    Quote Originally Posted by Kevin McFarlane View Post
    For member variables the conventions may be more relaxed so it is quite common to use a leading _.
    I've seen people do this all over the place, but I've never quite understood why. What's the rationale for using the leading underscore? Is it a historical convention or something...?
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Jun 1999
    Posts
    153

    Re: Naming convention for objects?

    It's probably due to the fact that a lot of C# devs were previously Visual C++ devs and MFC apps. had an m_ naming convention for member variables.

    We don't really need it but some sort of convention is just a convenience for distinguishing between the use of a local variable and a member variable (or in .NET's terminology a field).

    In Java it's more common to write this.x = x for example if you're assigning to a field within a method. Strictly-speaking this is preferred but not often favoured in .NET circles. Personally it's not something I'm hung up on. Adherence to the general Camel and Pascal naming conventions are more important.
    Kevin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured