Naming for Namespace, Class, and Variables

subaru_sti

Active member
Joined
Jul 4, 2009
Messages
34
Programming Experience
Beginner
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?

VB.NET:
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
 
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.

VB.NET:
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
 
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
 
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.
 
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.
 
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
 
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.
 
Okay. I think this makes sense, to me anyways:

VB.NET:
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
 
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.
 
Good point... This is confusing.

So since I am dealing with GPS data, should I name the class GPS and the property Speed?

Stephen
 
That sounds reasonable. Given the situation though, does it even make sense to be able to set that property? Presumably you're calculating speed based on the last two locations and the time between them, so it really doesn't make sense for you to be setting that value from the outside. It's value should always be calculated from the inside. In that case it should be a ReadOnly property and have only a Get method.
 
This value actually gets passed in by the GPS device so I do want to do some basic validation on it.

Thanks for all of your help!

Stephen
 
For Speed, it doesn't make sense to have MetersPerSecond as the property because you could be passing KM/H or MPH, etc. Just jumping back to Heading... I am adding another function that determines your direction (north, south, east, and west) based on a degree value from 0 to 360. Would it be wrong to change the property for the Heading class from "Direction" to "Degree"? I know degree is the type but I am going to be passing the "degree" value to the direction function to determine N, S, E, or W as well.

It is just not making sense to me...

Stephen
 
You shouldn't be naming properties after units. The property returns a value measured in those units. It doesn't return the unit. Would not Angle be a suitable name for a value that's measured in degrees?

I'd also be inclined to use a ReadOnly property to expose the N, S, E & W values and calculate them on the fly. You should also define an enumeration for the values:
VB.NET:
Public Enum Direction
    North
    South
    East
    West
End Enum
VB.NET:
Private _angle As Double

Public Property Angle() As Double
    Get
        Return Me._angle
    End Get
    Set(ByVal value As Double)
        Me._angle = value Mod 360
    End Set
End Property

Public ReadOnly Property Direction() As Direction
    Get
        Dim value As Direction

        Select Case Me.Angle
            Case Is < 45
                value = Direction.North
            Case Is < 135
                value = Direction.East
            Case Is < 225
                value = Direction.South
            Case Is < 315
                value = Direction.West
            Case Else
                value = Direction.North
        End Select

        Return value
    End Get
End Property
 
Back
Top