Results 1 to 10 of 10

Thread: Coding standards and naming conventions?

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Coding standards and naming conventions?

    The more and more of .NET code I see I am beginning to wonder if people have scrapped naming conventions...

    Do .NET coders still use obj, str, bln etc?

    Also. In VB6:
    VB Code:
    1. Dim objWoof As New Object
    The above can be done, but is frowned upon by some users. The following is how it's supposed to be done "correctly"
    VB Code:
    1. Dim objWoof As Object
    2.    'Blah blah blah
    3.    Set objWoof = New Object
    Is this the case for .NET coz all code I have seen uses the Dim As New method...????

    Any advantage in .NET of doing this?

    Woka

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    Hungarian notation is dead in .NET - and nobody cried at the funeral.

    Because there are so many type of thing and because everything is derived from an object you need to include the name of the object in the variable name - except for things like integers where that makes sense.

    e.g.
    VB Code:
    1. Dim HeadOfficePrinterCollection As New PrinterCollection

    Check out the FxCop application which you can download from Microsoft (MSDN) - It reports on your coding style and allows you to add/modify the rules for your own corporate standards. We use it a lot....

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    There is not really an advantage to Dim x As New in .NET other than it takes one less line, but there is also no perference hit like in VB6. So it is now fine to use New in the initialization line.

    PascalCase has pretty much replaced Hungarian (thank God) or whatever in house rules you have.

    PascalCase using CapitalLettersForMultipleWordsWithoutASpace. The following topics in the help explain more about naming 'Visual Basic Naming Conventions' and 'Coding Techniques'. They also suggest event naming conventions like Loading, Loaded and so on.

  4. #4

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    How would you do:
    VB Code:
    1. Private mstrConnString As String
    2.  
    3.     Public Property ConnectionString() As String
    4.         Get
    5.             Return mstrConnString
    6.         End Get
    7.         Set(ByVal Value As String)
    8.             mstrConnString = Value
    9.         End Set
    10.     End Property
    using correct naming convention.

    Woka

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Different people have different conventions for private variables I use the underscore _ but some use m_ or something else.
    VB Code:
    1. 'I would use
    2. Private _ConnectionString As String
    3.  
    4.     Public Property ConnectionString() As String
    5.         Get
    6.             Return _ConnectionString
    7.         End Get
    8.         Set(ByVal Value As String)
    9.             _ConnectionString = Value
    10.         End Set
    11.     End Property

    Here is some more information and a link to the sample Merrion mentioned:
    http://www.irritatedvowel.com/Progra...Standards.aspx
    Last edited by Edneeis; Feb 6th, 2004 at 10:32 AM.

  6. #6

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    bad underscore *slap*

    I have just been critisised for my naming conventions for VB6 at work
    I did:
    VB Code:
    1. Private mlngUsername As String
    2.  
    3. Public Sub SetUsername(ByVal pstrUsername As String)
    4. Dim strTemp     As String
    5. Dim strURL      As String
    6. On Error Goto ErrHandler
    7.     'Code here
    8. ErrHandler:
    9.     'Err code
    10. End Sub
    This apparently should be:
    VB Code:
    1. Private m_lUsername As String
    2.  
    3.  
    4. Public Sub SetUsername(ByVal sUsername As String)
    5. Dim sTemp As String
    6. Dim sURL As String
    7.  
    8.     On Error Goto ErrHandler
    9.    
    10.     'Code here
    11.  
    12.     ErrHandler:
    13.    
    14.     'Err code
    15.  
    16. End Sub
    Not a happy sniffing shark

    Woka

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I like your first way better. Either way, it's helpful to have a common standard when you have to work with other people's code.
    I don't like the underscore because it looks too much like the line continuation character, and the second way leads to confusing names (lpszVar, and such).

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Naming standards should be set by company and you really should have a manual/info document describing them. You cant be crisitsed if your not told .

    Ive carried on with the Hungarian naming as I like it.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by Wokawidget
    bad underscore *slap*

    I have just been critisised for my naming conventions for VB6 at work
    I did:
    VB Code:
    1. Private mlngUsername As String
    2.  
    3. Public Sub SetUsername(ByVal pstrUsername As String)
    4. Dim strTemp     As String
    5. Dim strURL      As String
    6. On Error Goto ErrHandler
    7.     'Code here
    8. ErrHandler:
    9.     'Err code
    10. End Sub
    This apparently should be:
    VB Code:
    1. Private m_lUsername As String
    2.  
    3.  
    4. Public Sub SetUsername(ByVal sUsername As String)
    5. Dim sTemp As String
    6. Dim sURL As String
    7.  
    8.     On Error Goto ErrHandler
    9.    
    10.     'Code here
    11.  
    12.     ErrHandler:
    13.    
    14.     'Err code
    15.  
    16. End Sub
    Not a happy sniffing shark

    Woka
    And even then, that's bogus Woka...
    If I saw, m_lUsername, I'd think it was a long int.... and not a string...



    Originally posted by salvelinus
    I like your first way better. Either way, it's helpful to have a common standard when you have to work with other people's code.
    I don't like the underscore because it looks too much like the line continuation character, and the second way leads to confusing names (lpszVar, and such).
    what's wrong with lpszVar? I can tell instantly what it is: a Long Pointer to a String Variable???? where the confusion?

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by techgnome
    what's wrong with lpszVar? I can tell instantly what it is: a Long Pointer to a String Variable???? where the confusion?

    TG
    Thank God for the IDE


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