Property Get/Let sucks 
Author Message
 Property Get/Let sucks

Why do people put code like this in a VB form?

Public Property Get FirstName() As String
    FirstName = m_sFirstName
End Property
Public Property Let FirstName(sLet As String)
    m_sFirstName = sLet
End Property

Wouldn't it have been easier and more readable just to use the module
level variable m_sFirstName, and not bother with these unecessary 6
lines of code?

I think the programmer had the fool notion that anything
remotely "object oriented" is really "good" and should be used if at
all possible.

Property Let/Get should only be used if something else needs to be done
than merely changing one variable.  And even then, it should probably
be limited to REAL classes that other programmers are going to use and
as such require a higher degree of formality.

Bob

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks


Quote:
> Why do people put code like this in a VB form?

> Public Property Get FirstName() As String
>     FirstName = m_sFirstName
> End Property
> Public Property Let FirstName(sLet As String)
>     m_sFirstName = sLet
> End Property

* because it keeps your code consistent for all properties
* because VB implements Public variables that way anyway
* because it allows later addition of error checking/etc easily
* because it allows for read-only, write-only and write-once options

Public variables are easier for the initial coder but make maintenance
more difficult down the road.  Do yourself a favor and stick to a
single, consistent method of exposing properties from your objects and
in the long run you will be better off.

--
Please reply via the newsgroup only

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks
I wouldn't say "they suck," but they are used in a wasteful manner like this
often.

Your app should be object oriented.  If you don't see why, then you should
take an object oriented class or read a book on the topic.  However,  you
can just make a variable public in a class (Not Module!!) instead of
explicitly writing these simplistic Property Let/Get statements.  The VB
Property Let/Get statements map directly to IDL Put and Get methods.  When
you define a variable as Public in a class, VB automatically does this for
you.  So, in essence, it has the exact same affect with less VB code.

When should you use simple Property Let/Get statements like this....   When
you think that there is a possibility in the future that you may add
functionality or business rules to the let or get and don't want to break
Binary Compatibility.

Gary Chamberlain
http://VBRing.com


Quote:
> Why do people put code like this in a VB form?

> Public Property Get FirstName() As String
>     FirstName = m_sFirstName
> End Property
> Public Property Let FirstName(sLet As String)
>     m_sFirstName = sLet
> End Property

> Wouldn't it have been easier and more readable just to use the module
> level variable m_sFirstName, and not bother with these unecessary 6
> lines of code?

> I think the programmer had the fool notion that anything
> remotely "object oriented" is really "good" and should be used if at
> all possible.

> Property Let/Get should only be used if something else needs to be done
> than merely changing one variable.  And even then, it should probably
> be limited to REAL classes that other programmers are going to use and
> as such require a higher degree of formality.

> Bob

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks
Not to be a ninny Bob, but I think there are some advantages that
encapsulation brings to any class (and a Form is just another class).
Interface consistancy, state management, predictable coding standards.  If
you have a small one off app, then yeah sure, save the 30 seconds it takes
to write the property, but if you or a team are going to maintain an app
over any length of time, you might want to reconsider.
IMHO,
Ed Pinto


Quote:
> Why do people put code like this in a VB form?

> Public Property Get FirstName() As String
>     FirstName = m_sFirstName
> End Property
> Public Property Let FirstName(sLet As String)
>     m_sFirstName = sLet
> End Property

> Wouldn't it have been easier and more readable just to use the module
> level variable m_sFirstName, and not bother with these unecessary 6
> lines of code?

> I think the programmer had the fool notion that anything
> remotely "object oriented" is really "good" and should be used if at
> all possible.

> Property Let/Get should only be used if something else needs to be done
> than merely changing one variable.  And even then, it should probably
> be limited to REAL classes that other programmers are going to use and
> as such require a higher degree of formality.

> Bob

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks
Property Let/Get are required for Encapsulation

A Module level variable will not be available to clients outside of the
class.

A Global Variable would be available to EVERYONE you would have no control
(No Encapsulation)

Further, by using Property Let/Get you expose the property as part of the
classes TypeLib/Interface

Property Let/Get allow us to do things like this.

Quote:
> Public Property Get FirstName() As String
>     If strUserName = "Bob" Then

            FirstName = m_sFirstName
        Else
            Err.Raise vbObjectError + 1000, "MyComponent", "Sorry You Can't
Access This Property"
        End If
Quote:
> End Property
> Public Property Let FirstName(sLet As String)
>  If strUserName = "Bob" Then

            m_sFirstName = sLet
        Else
            Err.Raise vbObjectError + 1000, "MyComponent", "Sorry You Can't
Change This Property"
        End If

    >End Property

Cheers

Gregory A Jackson MCSD, MCT
Sr. Software Engineer
STEP Technology
PDX, OR


Quote:
> Why do people put code like this in a VB form?

> Public Property Get FirstName() As String
>     FirstName = m_sFirstName
> End Property
> Public Property Let FirstName(sLet As String)
>     m_sFirstName = sLet
> End Property

> Wouldn't it have been easier and more readable just to use the module
> level variable m_sFirstName, and not bother with these unecessary 6
> lines of code?

> I think the programmer had the fool notion that anything
> remotely "object oriented" is really "good" and should be used if at
> all possible.

> Property Let/Get should only be used if something else needs to be done
> than merely changing one variable.  And even then, it should probably
> be limited to REAL classes that other programmers are going to use and
> as such require a higher degree of formality.

> Bob

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks
Ah, more musings from the man who brought us "Stupid Lazy Programmers" (as
opposed to Shiny Happy People?), "Two Tier vs. Three Tier" (where people who
use n-tier architecture are talking 'nonsense').  This is a technical
newsgroup where you can ask and answer technical questions.  If your post
was a genuine request for information then you worded it very very badly
indeed.  This is not a forum for your personal VJ++ evangelism and I'm sure
there is a newsgroup somewhere devoted to people ranting about how VB sucks
and how it isn't object oriented like VJ++ is... *yawn*

If only you put as much effort into helping people as you do tearing down
their code.  So SELECT * from that!



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks

What about validation? Triggers? But more important, to be able to write code against an "interface" is key. What's behind the interface can change at any time, but without concern to who/what it is coding against it (hell, there may not be any code behind it yet). What if you only want to expose the Get or the Set? Do you write some members this way and some members that way?

I say expose your interface ... not your data.

IMO

--------------------------------------------------------------------------------

Monte Hansen
VB Yuk Yuk
http://KillerVb.com


  Why do people put code like this in a VB form?

  Public Property Get FirstName() As String
      FirstName = m_sFirstName
  End Property
  Public Property Let FirstName(sLet As String)
      m_sFirstName = sLet
  End Property

  Wouldn't it have been easier and more readable just to use the module
  level variable m_sFirstName, and not bother with these unecessary 6
  lines of code?

  I think the programmer had the fool notion that anything
  remotely "object oriented" is really "good" and should be used if at
  all possible.

  Property Let/Get should only be used if something else needs to be done
  than merely changing one variable.  And even then, it should probably
  be limited to REAL classes that other programmers are going to use and
  as such require a higher degree of formality.

  Bob

  Sent via Deja.com http://www.deja.com/
  Before you buy.



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks


Quote:
> I wouldn't say "they suck," but they are used in a wasteful manner
like this
> often.

> Your app should be object oriented.  If you don't see why, then you
should
> take an object oriented class or read a book on the topic.  However,
you
> can just make a variable public in a class (Not Module!!) instead of
> explicitly writing these simplistic Property Let/Get statements.  The
VB
> Property Let/Get statements map directly to IDL Put and Get methods.
When
> you define a variable as Public in a class, VB automatically does
this for
> you.  So, in essence, it has the exact same affect with less VB code.

> When should you use simple Property Let/Get statements like
this....   When
> you think that there is a possibility in the future that you may add
> functionality or business rules to the let or get and don't want to
break
> Binary Compatibility.

You're not going to break "binary compatibility" unless we are talking
about a COM object, and for a COM object, yes a higher degree of
formality is required.

I'm talking about wasetfully using the Property Let/Get statements
internally--like I said before I see this overrused because somehow
programmers think it's "better" to be object oriented, but mere use of
these statements does NOT make the program object oriented.
Furthermore, object oriented is overused as well.  A COM object is
inherently an object, but within the COM object you can have non object
oriented code.  Therefore are you programming OOP or non-OOP???  What a
tough question to answer!

Java is supposed to be more object oriented than VB, bu you can easily
write non-OOP programs with Java by using "static methods" (or are
the "member" in Java... I forget).  Only Smalltalk is entirely object
oriented and no one progrmas with that because it sucks.

If you have a public variable in a Form or a Class, and you need to
utilize the greater funcitonality of the Property Let/Get statements,
all you have to do is rename the variable, and then create the Property
Let/Get statements with the old variable name.  It's THAT easy!!!! But
until you need the Property Let/Get, I say keep it out of there and
make your code easier to read and debug.

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks


Quote:
> Ah, more musings from the man who brought us "Stupid Lazy
Programmers" (as
> opposed to Shiny Happy People?), "Two Tier vs. Three Tier" (where
people who
> use n-tier architecture are talking 'nonsense').  This is a technical
> newsgroup where you can ask and answer technical questions.  If your
post
> was a genuine request for information then you worded it very very
badly
> indeed.  This is not a forum for your personal VJ++ evangelism and
I'm sure
> there is a newsgroup somewhere devoted to people ranting about how VB
sucks
> and how it isn't object oriented like VJ++ is... *yawn*

> If only you put as much effort into helping people as you do tearing
down
> their code.  So SELECT * from that!

At least my post on "Stupid Lazy Programmers" didn't attack any
particular programmers.  Actually, I used to use "SELECT *" until I
realized that in the long run it causes a lot more pain than the time
saved initially.  So although the "Stupid Lazy Programmers" thread had
a humorous title, I was sharing my knowledge of database programming
techniques with readers who might not have the benefit of my
experience.  And I guess I was also "ranting" as you put it.

So the personal attack is uncalled for.  But on the other hand, I'd be
geniunely sorry if my posts detrimentally affected this newsgroup.  But
I assumed this newsgroup was for general discussion about VB Enterprise
computing topics, and all of my posts fall within that category.  I
think this group would be really boring to read if the only kind of
posts allowed were "What ADO method do I use to exeute a stored
procedure", or "What API call do I used to read from the registry"
etc.  (Of course, there are legitimate cases of problems that aren't
covered in the online help or in the MSDN.  Undocumented bugs, that
kind of stuff.  These newsgroups are helpful for that stuff.)

And if I've given the impression that I hate VB.. I don't.  I think
that VB is a great development platform for "multi-tiered" database
applications (although I think that developers are too eager to stick
in extra tiers in order to be trendy and bleeding edge).  But VB also
has its flaws, which is true of most software.  Some of VB's flaws are,
in fact very frustrating.  I've seen a really big project that cost
large amounts of money blow up because of VB's flaws.

About VJ++: I think VJ++ is a better product than VB based upon small
work I've done with it, but I don't know how it would behave in a large
scale project.  Just because it's better technically doesn't mean you
should use it.  The advantages of VB is a large base of programmers you
can hire.  If you need a J++ WFC programmer that could be pretty hard
to find.  Microsoft hasn't been pushing J++.  And that's Sun's fault
for suing MS over it.  MS is waiting until the lawsuit completely
clears before they actively promote it.

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks
Hi Bob:

Quote:
> Property Let/Get should only be used if something else needs to be done
> than merely changing one variable.  And even then, it should probably
> be limited to REAL classes that other programmers are going to use and
> as such require a higher degree of formality.

What alternative method would you use to expose a public property?

--
Doug Marquardt (VB MVP)



Tue, 23 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks


Quote:
> What alternative method would you use to expose a public property?

Hi.  I think in the code I posted, the word "Public" was there, but it
was there unnecessarily... the methods weren't being used outside of
the Form it was in.

If you just declare a variable like "Public varname" then it's public
and works EXACTLY the same was as if you used the more cumbersome
Property Let/Property Get statements.

If you want to limit access to the variables, then I agree 100% that
you need to use the Property Let/Property Get statements.  But usually
this isn't the case and usually it's not necessary to so limit access.
And I'm not talking about making objects that other programmers are
going to use.  I'm talking about the many differet modules within that
COM object that aren't exposed to the outside world, and you don't have
to make the internal code more complicated than it has to be.

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 24 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks

Quote:


>If you want to limit access to the variables, then I agree 100% that
>you need to use the Property Let/Property Get statements.  But usually
>this isn't the case and usually it's not necessary to so limit access.

Another reason is where some validation is necessary,
or when a property procedure has to store the state
of the internal variable persistently (in the registry,
for example).

For the simple cases you brought up, where none
of these things apply, the result is, as you said, identical
to a public variable. Whether the one style or the other
is easier to read depends on your point of view.

And there is another downside to using textbook
OOP methods without examining why and when
you should or shouldn't: it can lull you into a false
sense of security.

When VB 4 was still shiny new, another developer and
I were asked to review some code written by an outside
firm. Their programmers, with lots of C++ OOP exerience,
had produced an app that seems to have unpredictable,
intermittent problems as well as regular slow performance.

One big reason for the intermitten performance problems
was the use of Property Get/Set procedures. The
programmers had used these extensively, since
"it's good textbook encapsulation, right?"

Unfortunately they were unaware of the fact that VB
-doesn't- create a copy of all objects passed
back from a function or into a function, as C++
does.

Quote:
>and you don't have
>to make the internal code more complicated than it has to be.

Never a good idea to make the code more complicated
than necessary, but whether property Let/Get procedures
do that is debatable.

I tend to favor property procedures myslef, but I also
tend to put validation or persistence logic in them.



Wed, 24 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks
Hi Bob:

Quote:
> Hi.  I think in the code I posted, the word "Public" was there, but it
> was there unnecessarily... the methods weren't being used outside of
> the Form it was in.

Ok.  Now your commments are more clear to me.

Quote:
> If you just declare a variable like "Public varname" then it's public
> and works EXACTLY the same was as if you used the more cumbersome
> Property Let/Property Get statements.

That was where I was headed... depending on your reply to the above ;)

Quote:
> If you want to limit access to the variables, then I agree 100% that
> you need to use the Property Let/Property Get statements.  But usually
> this isn't the case and usually it's not necessary to so limit access.

I agree with you on this one -- I see no need to use the let/get unless you
must manage what values are assigned to the variables.

--
Doug Marquardt (VB MVP)



Wed, 24 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks


Quote:
> I agree with you on this one -- I see no need to use the let/get
unless you
> must manage what values are assigned to the variables.

The classic book Code Complete recommends using as little code as
possible.  In general, the less code the easier the application is to
follow and debug.  Of course there are exceptions.

For the same reason I like to use the IIf statement.

  x = IIf(a=b, y, z)

is a lot easer to read than

  If a = b Then
    x = y
  Else
    x = z
  End If

Of course by itself either statement appears easy to read.  And if you
are unfamiliar with the IIf syntax, then the first line seems more
complicated.  But, if you have have to have 10 of these kinds of
statements in a row, the 10 line aproach is easier to follow than the
60 line approach (5 lines of code plus one empty line between each If-
Then-Else).

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 25 Jul 2002 03:00:00 GMT  
 Property Get/Let sucks
Hi Bob:

Quote:
> For the same reason I like to use the IIf statement.

>   x = IIf(a=b, y, z)

I *never* use IIf -- because of all the bugs that follow(ed) along with it.
MS may have fixed them since the time IIf was first implemented, but I never
bothered to do all that testing over and over again with each SP to see if
they did <g>.

Also, I find it easier to debug an If/Else statement.

--
Doug Marquardt (VB MVP)



Thu, 25 Jul 2002 03:00:00 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. property get / property let

2. Property Let, Property Get

3. Property Let vs. Property Set

4. property let and get,

5. Property Get, Set Let with Classes

6. Error 451: let property not defined

7. Property Let Procedure Error

8. Property Let problem

9. Using arrays as arguments in Let and Get Properties

10. Class Property Get Let

 

 
Powered by phpBB® Forum Software