+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19

Thread: Naming for Namespace, Class, and Variables

  1. #1
    subaru_sti is offline VB.NET Forum Newbie subaru_sti is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2009
    Posts
    27
    Reputation
    17

    Default Naming for Namespace, Class, and Variables

    Hello.

    I'm new to OOP and am racking my brain over what to name stuff... I have read a lot of different articles on standards and when to use nouns and different case, etc... I'm stumped when it comes to a real world example. In the code below, the namespace is called "Navigation" which I think is correct. Then I have a class called "Heading", which by definition is the direction a person/vehicle is truly pointing towards. I think that is also named correctly. In the code below, I have four things I have named: _WhatToName1, WhatToName2, WhatToName3, and WhatToName4.


    The value that gets passed in and stored is a double between 0 and 360 -- essentially the degree value from a circle. For WhatToName3 and WhatToName4, I have seen a lot of places that just use "value" as the name. Is that standard?

    Code:
    Namespace Navigation
        Public Class Heading
            Private _WhatToName1 As Double
    
            Public Sub New(ByVal WhatToName3 As Double)
                Me.WhatToName2 = WhatToName3
            End Sub
    
            Public Property WhatToName2() As Double
                Get
                    Return _WhatToName1
                End Get
                Set(ByVal WhatToName4 As Double)
                    _WhatToName1 = WhatToName4
                End Set
            End Property
        End Class
    End Namespace
    As I am just learning, I want to make sure that I start naming stuff correctly so that it can be easily reused and understood.

    Thanks in advance for your help,

    Stephen

  2. #2
    Robert_Zenz's Avatar
    Robert_Zenz is offline VB.NET Forum Idol Robert_Zenz master of VB.NET Robert_Zenz master of VB.NET Robert_Zenz master of VB.NET Robert_Zenz master of VB.NET Robert_Zenz master of VB.NET Robert_Zenz master of VB.NET Robert_Zenz master of VB.NET Robert_Zenz master of VB.NET Robert_Zenz master of VB.NET
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2008
    Location
    Vienna, Austria
    Age
    23
    Posts
    503
    Reputation
    296

    Default

    Hello.

    First question, are you developing a FLOSS application, Closed-Source Library (for resale) or a Closed-Source Application?

    In the last case, I'd stick with naming convention YOU can live with...in the other two cases it is most important that the interface of the code is intuitive. In the first two cases make sure to use XML-Documentation.

    Code:
    Navigation.Heading.Direction 'leads me to something what points into a direction
    Value is used whenever it is logical that the Object has properties and a underlaying value.

    Bobby
    Don't give TypeCasting Errors a chance, turn ON Option Strict!
    Greatest Obfuscator ever: EazFuscator (Freeware)
    Greatest Reflection Tool ever: .NET Reflector (Freeware) with Add-Ins
    Greatest Introspection Tool ever: Gendarme (GPL)
    Greatest MySQL FrontEnd ever: MySQL-Front (Shareware), HeidiSQL (GPL)

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    11,043
    Reputation
    1563

    Default

    WhatToName2: Direction as suggested is a good property name for this.
    WhatToName1: _direction would be my choice.
    WhatToName3: direction is the guidelines suggestion in this case.
    WhatToName4: value is put here by the property snippet, it is also commonly used in the .Net library.

    Guidelines for Names

  4. #4
    InertiaM is offline VB.NET Forum Idol InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2007
    Location
    Kent, UK
    Age
    40
    Posts
    567
    Reputation
    176

    Default

    Quote Originally Posted by JohnH View Post
    WhatToName2: Direction as suggested is a good property name for this.
    WhatToName1: _direction would be my choice.
    WhatToName3: direction is the guidelines suggestion in this case.
    WhatToName4: value is put here by the property snippet, it is also commonly used in the .Net library.
    I would agree with JohnH on 2, 1 and 4.

    For 3, I prefer to defy convention and use "whatDirection" as I feel "Me.Direction = direction" is messy. Purely my personal preference though.
    Always parameterize your queries - read more here

    "When people discover the center of the universe, a lot of them will be disappointed to find they are not it."

  5. #5
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    11,043
    Reputation
    1563

    Default

    Quote Originally Posted by InertiaM View Post
    I would agree with JohnH on 2, 1 and 4.

    For 3, I prefer to defy convention and use "whatDirection" as I feel "Me.Direction = direction" is messy. Purely my personal preference though.
    "Me.Direction = direction" may look at bit confusing to a developer, but the key here is what the user sees when using this class. The guideline I had in mind was this from Constructor Design :
    Do use the same name for constructor parameters and a property, if the constructor parameters are used to simply set the property. The only difference between such parameters and the properties should be casing.
    So when consumer uses this constructor it should be obvious that the 'direction' parameter is a way to set the 'Direction' property directly when creating the class instance.

    Whether is was "Me.Direction = direction" (setting value through property setter) or "Me._direction = direction" (setting value to private field) is just a matter of reuse of existing code if necessary, for example when setter does validation.

  6. #6
    subaru_sti is offline VB.NET Forum Newbie subaru_sti is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2009
    Posts
    27
    Reputation
    17

    Default

    Thanks everyone. This is very helpful and I think I am getting the hang of it. I have another one that is exactly the same but is for Bearing ("The direction of one object in relation to another object"). So that one would be Navigation.Bearing.Direction right?

    Last question: I have another class, the same as above, which is called "Speed". It is used to hold meters per second. I am using it for this GPS app but I don't think it should go into the Navigation namespace as it could be related to stuff other than navigation, right? For the property name of this class, would I want to name it something like "MetersPerSecond" or is that a no no?

    Stephen

  7. #7
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    41
    Posts
    6,907
    Reputation
    670

    Default

    Quote Originally Posted by subaru_sti View Post
    Thanks everyone. This is very helpful and I think I am getting the hang of it. I have another one that is exactly the same but is for Bearing ("The direction of one object in relation to another object"). So that one would be Navigation.Bearing.Direction right?

    Last question: I have another class, the same as above, which is called "Speed". It is used to hold meters per second. I am using it for this GPS app but I don't think it should go into the Navigation namespace as it could be related to stuff other than navigation, right? For the property name of this class, would I want to name it something like "MetersPerSecond" or is that a no no?

    Stephen
    I can't see why you would need a class for speed. Surely speed is just a number, so you'd just assign a value to a Double variable or the like. The only reason I can see to have a class for speed would be if you wanted to have a numerical value and a unit as well, e.g. kph or m/s.

  8. #8
    subaru_sti is offline VB.NET Forum Newbie subaru_sti is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2009
    Posts
    27
    Reputation
    17

    Default

    I want to have a class so that in the SET, I can validate that the value is greater than 0.

    Stephen

  9. #9
    subaru_sti is offline VB.NET Forum Newbie subaru_sti is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2009
    Posts
    27
    Reputation
    17

    Default

    Okay. I think this makes sense, to me anyways:

    Code:
    Namespace GPS
        Public Class Speed
            Private _metersPerSecond As Double
    
            Public Sub New(ByVal metersPerSecond As Double)
                Me.MetersPerSecond = metersPerSecond
            End Sub
    
            Public Property MetersPerSecond() As Double
                Get
                    Return _metersPerSecond
                End Get
                Set(ByVal value As Double)
                    If value < 0 Then Throw New ArgumentOutOfRangeException("Speed cannot be less than 0.")
    
                    _metersPerSecond = value
                End Set
            End Property
        End Class
    End Namespace
    Stephen

  10. #10
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    41
    Posts
    6,907
    Reputation
    670

    Default

    That still doesn't make sense. Consider this. If you had a Car class would it make sense to say myCar.Speed.MetersPerSecond = 10? That doesn't make sense to me. What does make sense is myCar.Speed = 10. The Car class would have a Speed property of type Double and you would validate the value inside that property. Having a whole separate class for speed is not appropriate.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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