tags:

views:

262

answers:

10

I have several entities that have calculated fields on them such as TotalCost. Right now I have them all as properties but I'm wondering if they should actually be methods. Is there a C# standard for this?

public class WorkOrder
{
    public int LaborHours { get; set; }
    public decimal LaborRate { get; set; }

    // Should this be LaborCost()?
    public decimal LaborCost
    {
        get
        {
            return LaborHours * LaborRate;
        }
    }
}
A: 

In my opinion, it's a preference; it's what you want to do. I do propreties in most cases, unless there is logic involved. Additionally, if you need to pass in parameters to change the functionality then obviously a method would apply...

Brian
+19  A: 

It's OK to use calculated properties rather than methods, as long as the calculation doesn't take a noticeable time

See Property usage guidelines

Thomas Levesque
+1 for looking up the link.
Ed Altorfer
+1 linked straight from the source
John K
@jdk I can't find a way to send you a direct message, so I want to tell you that whenever I see your name, I think of Java.
Ed Altorfer
I'm glad to hear this isn't bad practice. I quite like it after coming from Java land. Thanks for the link!
jcm
+1  A: 

I would leave them as properties. But there's not "standard" reason to do things one way or another. If you're by yourself, do whatever you like best. If you're on a team, then follow conventions the rest of your team are following.

Billy ONeal
+9  A: 

I think methods should perform actions on the object, typically change the state of the object. Properties should reflect the current state of the object even if the property is calculated. So you should keep your properties IMO.

Cellfish
What if the operation involves significant amount of computation?
Will Vousden
Programmers assume property access is constant-time, so Microsoft suggests not to do any complex computation in properties, but instead to use methods when there is significant processing involved.
Ed Altorfer
Doesn't matter. Whether it's a property or method doesn't tell the caller anything.
Billy ONeal
Someone else actually posted about this: http://msdn.microsoft.com/en-us/library/bzwdh01d%28VS.71%29.aspx#cpconpropertyusageguidelinesanchor1
Ed Altorfer
Another case is when a would-be property would return a new mutable instance which is not tied to the object on which a property is called. E.g. if you do `return new int[] { ... }`, then it really should be a method. If you return the same array all the time, then property is okay.
Pavel Minaev
I think it depends on the situation when you're talking about mutable instances...for things like lazy loading, properties can be ok, again assuming that you don't violate the complex computation caveat. Good point, though.
Ed Altorfer
If you return the same instance on repeated calls to (lazily loaded) property, then there's no problem with being a property according to rule above. And if it returns different instances, then it's not really lazily loaded, is it :)
Pavel Minaev
A: 

Depends, if your "properties" become mammoths and require a whole slew of business logic they shouldn't be properties, there should be a method. The example you posted looks ok to be a property. No standard way of doing it, go with your gut instinct; if it looks like it needs to do a lot you probably need a method.

JonH
+2  A: 

If they are a) lightweight and b) have no side effects, I would make them Properties.

Lightweight is a bit fuzzy of course, but the rule of thumb is: If I ever have to worry calling a Property (be it in a loop or anywhere else), it should possibly be a method.

Michael Stum
You could also have a lazy property that can be either forced to recalculate based on some explicit method [such as Refesh()] or implicit event [such as timeout or dirty cache]. This would allow properties for easy binding/serialization and the ability to keep data up-to-date.
Matthew Whited
Very True. Lazy-Loading Properties are rare, but useful.
Michael Stum
+5  A: 

I think they should all be properties. As long as it doesn't change the state of the object, I'm cool with it as a property.

Additionally, if I'm using your class for data binding (WPF, etc.), then I can bind directly to your property without having to modify/extend the class.

Tim Ridgely
+1 for the databinding
ChrisF
+1 for data binding as well! Nicely done.
Ed Altorfer
Agree, though it's important to remember that if you want to bind to the computed property, then the other relevant property setters or mutator methods need to raise the PropertyChanged event for the computed property.
itowlson
A: 

If a property is particularly expensive to calculate, I might change it to a GetWhatever() method. This serves as a hint to whoever uses my class that this value requires some significant work to arrive at, and the caller should cache the value rather than calling the method multiple times.

Trivial calculations are perfectly appropriate inside of properties.

Joel Mueller
A: 

It's largely just syntactic sugar anyway, so do want you is convention in your team, or what you prefer, as long as it is just returning information about the object and not changing it or interacting with other objects.

Paul Creasey
A: 

MSDN gives information about this here

Class library designers often must decide between implementing a class member as a property or a method. In general, methods represent actions and properties represent data.

Which one do you think it is? An action calculate/getLaborCost or data?

WorkOrder workOrder = new WorkOrder();
workOrder.LaborHours = 8;
workOrder.LaborRate = 20;

decimal cost = workOrder.LaborCost; // This is OK here

but if you are going to do this for the same object also:

worOrder.LaborHours = 18;
decimal newCost = workOrder.LaborCost 

Now this cannot be a property. It would be a lot better to be a method.

JCasso