Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > VB.NET General Discussion

VB.NET General Discussion VB.NET general discussion area

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-07-2009, 2:56 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jul 2009
Posts: 27
Reputation: 10
subaru_sti is on a distinguished programming path ahead
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-07-2009, 4:10 AM
Robert_Zenz's Avatar
VB.NET Forum Idol
.NET Framework: .NET 2.0
 
Join Date: Jun 2008
Location: Vienna, Austria
Age: 22
Posts: 503
Reputation: 289
Robert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NET
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-07-2009, 6:08 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,173
Reputation: 1273
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
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
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-07-2009, 8:07 AM
VB.NET Forum Miyagee
.NET Framework: .NET 2.0
 
Join Date: Nov 2007
Location: Kent, UK
Age: 39
Posts: 497
Reputation: 166
InertiaM I'm the one to askInertiaM I'm the one to askInertiaM I'm the one to askInertiaM I'm the one to askInertiaM I'm the one to askInertiaM I'm the one to ask
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-07-2009, 9:23 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,173
Reputation: 1273
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
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 :
Quote:
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.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-07-2009, 9:57 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jul 2009
Posts: 27
Reputation: 10
subaru_sti is on a distinguished programming path ahead
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-07-2009, 11:02 PM
jmcilhinney's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2004
Location: Sydney, Australia
Age: 40
Posts: 6,006
Reputation: 538
jmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalist
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-07-2009, 11:14 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jul 2009
Posts: 27
Reputation: 10
subaru_sti is on a distinguished programming path ahead
Default

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

Stephen
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-08-2009, 12:14 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jul 2009
Posts: 27
Reputation: 10
subaru_sti is on a distinguished programming path ahead
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-08-2009, 1:10 AM
jmcilhinney's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2004
Location: Sydney, Australia
Age: 40
Posts: 6,006
Reputation: 538
jmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalist
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 12:18 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.