Naming Conventions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ted

    Naming Conventions


    I'm trying to come up with naming conventions for my
    company as we move to C#. I'm looking through the Naming
    Guidelines section on MSDN, but I'm unable to find a
    recommendation for class scope variables.

    Working with WinForms, I believe it would be helpful to
    have some type of prefix. In Visual Basic we used
    m_variableName. But I can't find any suggestions on this.

    Any help on this would be great. Thanks,
    Ted
  • Rob Windsor [MVP]

    #2
    Re: Naming Conventions

    Hi Ted

    Microsoft suggests the following:

    Public methods or properties are pascal cased. (mixed case - first letter
    upper case)
    Private or protected methods are camel cased. (mixed case - first letter
    lower case)
    Private or protected fields (variables) are camel cased and prefixed with an
    underscore.
    Variable names do not use Hungarian notation.
    Parameters are camel cased.

    Public Class TestClass
    Private _stringField1 As String
    Protected _stringField2 As String

    ' public fields are not recommended
    ' use properties instead
    Public StringField3 As String

    Public Property StringField1() As String
    Get
    Return _stringField1
    End Get
    Set(ByVal Value As String)
    _stringField1 = Value
    End Set
    End Property

    Public Sub CoolRoutine1(By Val param1 As String)
    End Sub

    Private Sub coolRoutine2()
    End Sub
    End Class

    There's a MSDN Webcast on .NET Best Pratices that discusses this over a few
    slides.

    --
    Rob Windsor [MVP-VB]
    G6 Consulting
    Toronto, Canada



    "Ted" <anonymous@disc ussions.microso ft.com> wrote in message
    news:1455f01c3f 8b7$bcac06c0$a4 01280a@phx.gbl. ..[color=blue]
    >
    > I'm trying to come up with naming conventions for my
    > company as we move to C#. I'm looking through the Naming
    > Guidelines section on MSDN, but I'm unable to find a
    > recommendation for class scope variables.
    >
    > Working with WinForms, I believe it would be helpful to
    > have some type of prefix. In Visual Basic we used
    > m_variableName. But I can't find any suggestions on this.
    >
    > Any help on this would be great. Thanks,
    > Ted[/color]


    Comment

    • Cor

      #3
      Re: Naming Conventions

      Hi Rob,

      I have checked for it in the VB.resourcekit and there is this what you show
      absolute not used in the best practise sample, so you make me curious.

      Also because the type of a object is very good to recognise in dotnet it is
      advice to add that not in your dataname, so I am very curious.

      Can you give me the link to that documentation.

      Cor
      [color=blue]
      > Hi Ted
      >
      > Microsoft suggests the following:
      >
      > Public methods or properties are pascal cased. (mixed case - first letter
      > upper case)
      > Private or protected methods are camel cased. (mixed case - first letter
      > lower case)
      > Private or protected fields (variables) are camel cased and prefixed with[/color]
      an[color=blue]
      > underscore.
      > Variable names do not use Hungarian notation.
      > Parameters are camel cased.
      >
      > Public Class TestClass
      > Private _stringField1 As String
      > Protected _stringField2 As String
      >
      > ' public fields are not recommended
      > ' use properties instead
      > Public StringField3 As String
      >
      > Public Property StringField1() As String
      > Get
      > Return _stringField1
      > End Get
      > Set(ByVal Value As String)
      > _stringField1 = Value
      > End Set
      > End Property
      >
      > Public Sub CoolRoutine1(By Val param1 As String)
      > End Sub
      >
      > Private Sub coolRoutine2()
      > End Sub
      > End Class
      >
      > There's a MSDN Webcast on .NET Best Pratices that discusses this over a[/color]
      few[color=blue]
      > slides.[/color]


      Comment

      • Ted

        #4
        Re: Naming Conventions


        Rob,

        Can you send me a link where this is suggested?

        In the Design Guidelines for Class Library Developers
        microsoft is very inconsistent with naming conventions
        while suggesting proper naming conventions.

        For example, at http://msdn.microsoft.com/library/en-
        us/cpgenref/html/cpconpropertyus ageguidelines.a sp?
        frame=true

        Microsoft lists the example:

        [Visual Basic]
        Public Class TextBox
        Private m_dataSource As String
        Private m_dataField As String
        Private m_active As Boolean

        Public Property DataSource() As String
        Get
        Return m_dataSource
        End Get
        Set (...more)

        What is the "m_" prefix? If it's for Module, then module
        has a different meaning in .net.

        The C# example:

        public class TextBox
        {
        string dataSource;
        string dataField;
        bool active;

        public string DataSource
        {
        get
        {
        return dataSource;
        }
        set (...more)

        Now for C# they do not use the prefix.

        But what if the constructor accepted the dataSource
        variable in the C# example, what would the variable name
        for the constructor be? ie. localDataSource or
        thisDataSource?

        Rob, I like your example of prefixing with the underscore
        and will probably go with this, but I just can not find
        that recommendation on Microsoft's website in the
        guidelines section.

        Thank you,
        Ted


        [color=blue]
        >-----Original Message-----
        >Hi Ted
        >
        >Microsoft suggests the following:
        >
        >Public methods or properties are pascal cased. (mixed[/color]
        case - first letter[color=blue]
        >upper case)
        >Private or protected methods are camel cased. (mixed[/color]
        case - first letter[color=blue]
        >lower case)
        >Private or protected fields (variables) are camel cased[/color]
        and prefixed with an[color=blue]
        >underscore.
        >Variable names do not use Hungarian notation.
        >Parameters are camel cased.
        >
        >Public Class TestClass
        > Private _stringField1 As String
        > Protected _stringField2 As String
        >
        > ' public fields are not recommended
        > ' use properties instead
        > Public StringField3 As String
        >
        > Public Property StringField1() As String
        > Get
        > Return _stringField1
        > End Get
        > Set(ByVal Value As String)
        > _stringField1 = Value
        > End Set
        > End Property
        >
        > Public Sub CoolRoutine1(By Val param1 As String)
        > End Sub
        >
        > Private Sub coolRoutine2()
        > End Sub
        >End Class
        >
        >There's a MSDN Webcast on .NET Best Pratices that[/color]
        discusses this over a few[color=blue]
        >slides.
        >
        >--
        >Rob Windsor [MVP-VB]
        >G6 Consulting
        >Toronto, Canada
        >
        >
        >
        >"Ted" <anonymous@disc ussions.microso ft.com> wrote in[/color]
        message[color=blue]
        >news:1455f01c3 f8b7$bcac06c0$a 401280a@phx.gbl ...[color=green]
        >>
        >> I'm trying to come up with naming conventions for my
        >> company as we move to C#. I'm looking through the[/color][/color]
        Naming[color=blue][color=green]
        >> Guidelines section on MSDN, but I'm unable to find a
        >> recommendation for class scope variables.
        >>
        >> Working with WinForms, I believe it would be helpful to
        >> have some type of prefix. In Visual Basic we used
        >> m_variableName. But I can't find any suggestions on[/color][/color]
        this.[color=blue][color=green]
        >>
        >> Any help on this would be great. Thanks,
        >> Ted[/color]
        >
        >
        >.
        >[/color]

        Comment

        • Jaswinder_Singh

          #5
          Re: Naming Conventions

          Hi,
          The convention suggested by Rob has been defined in the book "Inside C#" by Tom Archer, Microsoft Press.

          The link you mentioned seems to be going along with the conventions listed in the book, and by Rob. Only exception being the private variables, which are prefixed by 'm_'.

          So clases have names like: ClassName
          Methods: MethodName
          Properties: PropertyName
          Private Attributes: m_attributeName
          Other Attributes: attributeName

          Regards,
          Jaswinder

          Comment

          • Cor

            #6
            Re: Naming Conventions

            Hi Jaswinder,

            I follow this thread also, and it is in my opinion not true what you write.

            Rob is giving to stringvalues very clear the name
            stringName

            In my opinion Name is enough

            Cor


            Comment

            Working...