Geeks With Blogs


Google My Blog

Catch me at: The List!

My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Chris G. Williams Beware: I mix tech and personal interests here.

I'm looking through someone else's code, and I see the strangest thing I've seen in... well, a long time.

Instead of using a Public Function, the developer decided to create a Public Property that accepts 3 values as parameters, does some stuff, and returns a single value.

So instead of:

Public Function myFunc(byval A as integer, byval B as integer, byval C as integer) as integer
    blah blah pseudocode
    Return D
End Function

We have this:

Public ReadOnly Property myProp(byval A as integer, byval B as integer, byval C as integer) as integer
      blah blah pseudocode
      return D
   End Get
End Property

Yes I know the Property syntax isn't perfect, but that's not the issue, I'm paraphrasing.

So... am I totally missing the point here? Is there a reason to do this? Ever?

Posted on Wednesday, April 11, 2007 7:10 PM | Back to top

Comments on this post: why? just tell me why...

# re: why? just tell me why...
Requesting Gravatar...
I see your problem.. you are using VB.Net...

Seriously though this is crazy and only works because properties are really syntactic sugar for method calls anyway. I don't even think you can have parameter list if you try to do the same thing in c# so I think its just a side effect of the way you declare properties in VB.Net

Presumably calling it looks just like a method call.
Left by Zman on Apr 11, 2007 8:18 PM

# re: why? just tell me why...
Requesting Gravatar...
It could be too that those values should maybe be passed into the constructor so that the property can act as intended...maybe the Dev didn't think of that and did it this way (which in no way condones doing it this way).

But if its supposed to be accessed multiple times throughout the objects life with different values passed in, the method makes obviously more sense then a property.

To put a philisophical spin to it: Can a read only property truly be read only if it accepts parameters that alter its output? Hmmmm...

Left by D'Arcy from Winnipeg on Apr 12, 2007 7:13 AM

# re: why? just tell me why...
Requesting Gravatar...
Maybe there is no reason. Maybe the DEV is trying to screw with our minds. Maybe that's how they program in the dimension of not only sight and sound but also of mind???
Left by codesailor on Apr 12, 2007 12:31 PM

# re: why? just tell me why...
Requesting Gravatar...
A more rational set of possibilities:

1). Properties reflect differently than functions; for example, some OR/M frameworks look at properties but not methods (I'm not sure why an OR/M framework would use the method as you describe, but my point is that reflection sees the property differently).

2). For some reason or another, the property grew out of its original scope. For example, say you have Property X(index as integer) as string. I business requirement later is that the property needs to have the element returned modified by Y so the definition changes to Property X(index as integer, Y as double) as string. Then the requirement changes again so that the property is no longer modifiable. At this point in the project, dozens of methods call the property. Rather than change the implementation the developer simply removes the set portion of the statment. My point with this long winded explanation is that often business requirements change the implementation and it's just easier (and faster) to fiddle with what works than to write it correctly.

3). The developer is a (typically) screwed up average VB developer (in the interest of full disclosure, I develop in both C# and VB.NET; I like the VB.NET language better and the C# community better (except for the bigoted elitists out there).
Left by OwenG on Apr 12, 2007 6:42 PM

# re: why? just tell me why...
Requesting Gravatar...
One would have to look at what was happening at "blah blah..." Idealy, a property returns state information on the object. It could take a parameter to properly locate or present the state info, but it should just be current state of the object. A method conceptually performs a transform on state. So if the "property" above alters state or performs a transform of state, then it should be a method.
Left by Noah Coad on Apr 15, 2007 4:47 PM

# re: why? just tell me why...
Requesting Gravatar...
I agree with Zman. It's just bad form. A function should be used.

If for no other reason, then for portability and good code reviews when their company gets bought out by a C# shop. :)
Left by Michael Earls on Apr 25, 2007 8:52 AM

Your comment:
 (will show your gravatar)

Copyright © Chris G. Williams | Powered by: