473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Best Practices - Coding Convention Question

Ren
Hi All,

I'm still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks
Mar 22 '06 #1
10 2955
Ren wrote:
Hi All,

I'm still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks


Not sure of best practice on this, but from a practice level, if you use
the property it allows you to make changes during the get/set in the
future w/o having to recode rest of the class.

Chris
Mar 22 '06 #2
Ren,

I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.

By the way there is never a Best Practice. Luckily this is always a part of
discussion.

Typical for the VBNet developper is that he/she is human and can change when
it is needed and take the best Practice for that situation.

Cor

"Ren" <no****@nospam.org> schreef in bericht
news:pf********************************@4ax.com...
Hi All,

I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks

Mar 22 '06 #3
"Ren" <no****@nospam.org> schrieb:
I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?


I consider accessing it through the property's 'Get'/'Set' accessor even
inside the class whereever possible good practice, because it will prevent
assignment of invalid values to the property. Execution overhead is minimal
because the JIT compiler can inline property access.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 22 '06 #4
I agree with Cor and Herfried. There is no reason to treat the property
differently within its class. It can only be cause of errors. If you
feel you need it, it probably means you are doing something you should
not, or you are doing it the wrong way.

On the practical side the greatest advantage is that you just write
"me." and you have all the properties there readily available
(intellisense). It's a lot more convenient...

-tom

Mar 22 '06 #5
Cor ,
I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.

funny the parctical guidelines book states that m_VariableName should be
used :-)
By the way there is never a Best Practice. Luckily this is always a part
of discussion.

Typical for the VBNet developper is that he/she is human and can change
when it is needed and take the best Practice for that situation.
Have you ever heard of programming by MSF ?? MSF holds a colection of
practical guidelines and best practices wich includes naming conventions
So these rules do exist ............. however do we want to implement them
as strict as they are,, that is more the question

If we would code by these rules our programs would be more readable and
understandable , however practice learns that most coders prefer there own
conventions ( you still see hungarion notation in .net code for
stance ) or even worse program without anny conventions at all ( 90 % of
the code i saw in my daily work was like this, and i have seen code from
all over the world by all sorts of programmers ) ...

So the guidelines do exist MS has verry nice documentation regarding this ,
however we , as programming comunity do not enforce them or expect them to
be the standard ... that is the problem in my opinion .

see for more info about MSF :

http://www.microsoft.com/technet/its...f/default.mspx

personal recomendation regarding this :

Practical Guidelines and best practices for Microsoft Visual Basic and
visual C# Developers
http://www.amazon.com/gp/product/073...Fencoding=UTF8

Having said this i will start packing my suitcase as my plain leaves
tomorrow to the canary islands :-)
i will have a 2 week holliday before i start my new job

regards

Michel Posseth [MCP]

"Cor Ligthert [MVP]" <no************@planet.nl> schreef in bericht
news:eK**************@TK2MSFTNGP11.phx.gbl... Ren,

I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.

By the way there is never a Best Practice. Luckily this is always a part
of discussion.

Typical for the VBNet developper is that he/she is human and can change
when it is needed and take the best Practice for that situation.

Cor

"Ren" <no****@nospam.org> schreef in bericht
news:pf********************************@4ax.com...
Hi All,

I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks


Mar 22 '06 #6
Lee
Something not addressed in the other posts...

If you always reference the private variable by its property-name and
you need to search for where the property is referenced you only have
to search for one thing -- the property. Otherwise, within the class
wherein it's defined, you have to search for both the property-name and
the variable-name.

--
Lee Silver
Information Concepts Inc.

Mar 23 '06 #7
Ren wrote:
<snip>
I'm still rather new at vb.net and would like to know the proper way to access
private varibables in a class. Do I access the variable directly or do I use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer <snip> end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class


Properties were created to protect, and even hide, the internal
structure of the object from the outside world. Ideally, only the
object itself should know about it's internal fields and how they
relate to the exposed properties.

Besides, properties allow for side-effects to happen when a given field
is accessed. Therefore, a property actually encapsulates a behavior.

Now, it's a very common practice to expose internal fields -- that were
suposed to be public -- through properties, even though these
properties are only getters/setters (I'm talking about just those
properties that are both a getter and a setter for a given field). The
reasoning is: a) you must not expose your internal fields and b)
properties protect you by keeping your interface intact should the
internal workings of the object change.

But It turns out that I see more frequent changes to an in-progress
object interface than to the internals of the object, usually because
of unantecipated needs of the "external world" (from the object's
perspective), even though the internal structure of the object remains
more or less intact.

Considering this, and as a left over from the time when compilers
wouldn't be smart enough to inline the access to properties, my
personal practice goes against (although not religiously) accessing
internal fields via getter/setter properties.

I mean, the object is in a priviledged position to see these fields, so
it should also exercise direct access to them.

Therefore, internally I'd use mVariableName, instead of the related
properties.

Regards,

Branco.

Mar 23 '06 #8
Michel,

Did you ever look at modern Visual Basic samples on MSDN (not from C++ -> C#
converted ones).

Microsoft has showed much of those patterns, it seems for me that it depends
who made it what it look like.

Cor

"Michel Posseth [MCP]" <MS**@posseth.com> schreef in bericht
news:ex**************@TK2MSFTNGP11.phx.gbl...
Cor ,
I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.


funny the parctical guidelines book states that m_VariableName should be
used :-)
By the way there is never a Best Practice. Luckily this is always a part
of discussion.

Typical for the VBNet developper is that he/she is human and can change
when it is needed and take the best Practice for that situation.


Have you ever heard of programming by MSF ?? MSF holds a colection of
practical guidelines and best practices wich includes naming conventions
So these rules do exist ............. however do we want to implement them
as strict as they are,, that is more the question

If we would code by these rules our programs would be more readable and
understandable , however practice learns that most coders prefer there own
conventions ( you still see hungarion notation in .net code for
ance ) or even worse program without anny conventions at all ( 90 % of
the code i saw in my daily work was like this, and i have seen code from
all over the world by all sorts of programmers ) ...

So the guidelines do exist MS has verry nice documentation regarding this
, however we , as programming comunity do not enforce them or expect them
to be the standard ... that is the problem in my opinion .

see for more info about MSF :

http://www.microsoft.com/technet/its...f/default.mspx

personal recomendation regarding this :

Practical Guidelines and best practices for Microsoft Visual Basic and
visual C# Developers

http://www.amazon.com/gp/product/073...Fencoding=UTF8

Having said this i will start packing my suitcase as my plain leaves
tomorrow to the canary islands :-)
i will have a 2 week holliday before i start my new job

regards

Michel Posseth [MCP]

"Cor Ligthert [MVP]" <no************@planet.nl> schreef in bericht
news:eK**************@TK2MSFTNGP11.phx.gbl...
Ren,

I use for the private variable
mVariableName

For the rest as you wrote with _variableName a notation that I hate.

By the way there is never a Best Practice. Luckily this is always a part
of discussion.

Typical for the VBNet developper is that he/she is human and can change
when it is needed and take the best Practice for that situation.

Cor

"Ren" <no****@nospam.org> schreef in bericht
news:pf********************************@4ax.com...
Hi All,

I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do
I use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks



Mar 23 '06 #9
Hi Ren,

It depends on the case. In your example there is no difference, but there
are cases where the Get/Set accessors execute additional code (validation,
caching, etc) and in those cases you need to consider if you want that
additional code to be executed or not.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Ren" <no****@nospam.org> escribió en el mensaje
news:pf********************************@4ax.com...
Hi All,

I'm still rather new at vb.net and would like to know the proper way to
access
private varibables in a class. Do I access the variable directly or do I
use
the public property?

public class MyClass
private _variableName as integer

public property VariableName as integer
get
return _variableName
end get
set(byVal Value as integer)
_variableName = value
end set
end property

public sub DoSomething ()
' do I use _variableName or
' do I use VariableName"
end sub

end class

Thanks

Mar 23 '06 #10
"Branco Medeiros" <br*************@gmail.com> schrieb:
Besides, properties allow for side-effects to happen when a given field
is accessed. Therefore, a property actually encapsulates a behavior.

Now, it's a very common practice to expose internal fields -- that were
suposed to be public -- through properties, even though these
properties are only getters/setters (I'm talking about just those
properties that are both a getter and a setter for a given field). The
reasoning is: a) you must not expose your internal fields and b)
properties protect you by keeping your interface intact should the
internal workings of the object change.

But It turns out that I see more frequent changes to an in-progress
object interface than to the internals of the object, usually because
of unantecipated needs of the "external world" (from the object's
perspective), even though the internal structure of the object remains
more or less intact.

Considering this, and as a left over from the time when compilers
wouldn't be smart enough to inline the access to properties, my
personal practice goes against (although not religiously) accessing
internal fields via getter/setter properties.

I mean, the object is in a priviledged position to see these fields, so
it should also exercise direct access to them.


Consider this sample:

\\\
Private m_Bla As Integer

Public Property Bla() As Integer
Get
Return m_Bla
End Get
Set(...)
If Value < 0 OrElse Value > 10 Then
Throw New ArgumentException(...)
Else
m_Bla = Value
End If
End Set
End Property
///

When assigning the value to 'm_Bla' directly you are bypassing validation
and it's possible that the property value returned by 'Get' is an invalid
value due to bugs in the code which cause an invalid value to be assigned to
the private backing field. I strongly believe that it's better to have only
a single code path to access 'm_Bla' than multiple paths.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 23 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Stuart Gerchick | last post by:
C++ Coding Standards : 101 Rules, Guidelines, and Best Practices by Herb Sutter, Andrei Alexandrescu is now a month or so away from release. What is people's opinion on this...is it going to be a...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
26
by: puzzlecracker | last post by:
It'd be interesting to compare the learning practices of c++ practitioners. I'll start with mine The C++ Programming Language C++ Primer Effective C++ More Effective C++ Effective STL The...
3
by: John Dalberg | last post by:
I am looking for an ASP.NET application on CodePlex which exemplifies best practices for the following: - Use of interfaces - Seperation of the UI, business and data tiers - Data Tier that uses...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
52
by: burgermeister01 | last post by:
First, let me say that this question is a rather general programming question, but the context is PHP, so I figured this group would have the most relevant insight. Anyways, this is also more of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.