views:

398

answers:

7

I see little functional difference between using a property

public readonly property foo as string
  get
    return bar
  end get
end property

or a function

public function foo() as string
  return bar
end function

Why would I want to use one form over the other?

Thanks!

+6  A: 

It's purely a matter of appearance. Methods imply doing so action, while properties imply getting some data.

James Curran
From that perspective you could argue that get properties should imply a const of the host object.
DrFloyd5
@DrFloyd: True, in fact I would have said that if I thought of a good way to phrase it.
James Curran
A: 

Besides the semantics mentioned by James (statement of intent), properties play a special role since their value is displayed by the debugger and may be used in visual designers.

As a consequence, make sure that properties' values don't change without some outside action because otherwise, the debugger will screw up your program.

Konrad Rudolph
A: 

As far as I know, it all compiles to the same thing. A property in IL for .NET is really just a getPropertyName and setPropertyName function for a property called PropertyName.

So its really a matter of style in that regard. It just reads better to see the following:

person.Address.Street;

rather than

person.Address().Street();

So, in a way, its really a matter of code asthetics. Unfortunately for me in .NET, its not as elegant as Ruby in that '()' are always optional.

Corey Gaudin
VB.Net does not always require (), just C#
Nescio
Well, it compiles *not* down to the same thing, as you yourself said. The compiler is well aware of the differences between the two and might treat them very differently.
Konrad Rudolph
C# requires () for good reason. Without the parentheses, it means something completely different. See: delegates.
Josh Hinman
+8  A: 

I read an interesting article recently in Visual Studio Magazine that discussed the different between Methods and Properties.

Properties are supposed to return a value and the same value each time unless something else is called in between.

A Method on the other hand is typically expected to do something in the background to get the value, or that the method may change the value each time it is called, like GetNextId() or something.

DateTime.Now is a good example of a Property that should have been a Method since it returns a different value each time it is used.

For those interested- here is the article

Choose Between Methods and Properties

Hugoware
That is an interesting behavioral difference that can be identified using the keyword const in some other languages. Thinks like "supposed" and "should" are places for religion to hide. An explicit definition allows for standards instead of conventions.
DrFloyd5
`DateTime.Now` is excellent as a property. Although its value changes all the time, this doesn't have any influence (since `DateTime`s are immutable and the property is shared!) and also, it doesn't depend on the calling (like a counter or random number) but on some other variant outside the program
Konrad Rudolph
Hey, don't down vote me for it - Find the guy who wrote the article. Besides, a Property shouldn't change itself - if it does, it's not a Property anymore... it's a Method
Hugoware
@Konrad, The implementation of a function is inside the black box. If you had a property "p" that changed most times you called it, should it be a property?@HBoss, you did post someone else's work as an answer. Do you not want the up votes either? ;)
DrFloyd5
I'd give them to him If I could find him... Okay, done looking... I'll just go ahead and keep them - heh heh...
Hugoware
+1  A: 

In my understanding, you cannot databind to methods; only properties. Thus properties carry that added benefit.

John Kraft
A: 

If you are basing yourself upon the Framework Design Guidelines, you must be using a method only when you are actually performing an action or accessing resouces that could be expensive to use(database, network).

The property give the user the impression that the values are stored in memory and that reading a property is fast while calling a method might have further implication than just "get the value".

Brad Abrams actually wrote an article about it and is even posted on MSDN here.

I would highly suggest that you buy the book Framework Design Guidelines. It's a must read for every developer.

Maxim
A: 

My view is that if you look at the words - "property" compared to "method". The word "property" implies "this is some value inherent in the object, like color, size, owner... calling a property would imply a relatively simple operation to return that value. Or, if it isn't a read-only property, setting the property is should also be a relatively simple (and low cost) operation.

On the other hand, a "method" implies "I have to do some real work to perform this task you are asking of me" - while a method may well return a value (even if only to say "Yes, I did that"), it is doing a whole bunch of stuff to get that value for me - and in doing so, it will do some other operations associated with getting that value.

So, it is mainly semantic - a read only property could just as satisfactorily be implemented as a method - but I would look at what is involved in returning that value, and the implications each would send to the person retrieving that property or calling that method.

Ken Ray