473,388 Members | 1,209 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,388 software developers and data experts.

Best practices on calculated properties

What are peoples views on using readonly properties to return the sum of
a calculation?

For instance you might have a invoice class that has itemprice and
quantity properties.
Would the total be implemented as readonly prop Total to return
itemprice * quantity or method CalculateTotal which returns the same thing.

A readonly prop is, IMHO, more intuitive as it is still an attribute of
this class but what about when you have a longer calculation that
affects performance and as such should be stored by the consumer instead
of running the calc every time they wanted to access the 'property'.

I dont think there is a 'right' way to do this but I need to define a
clear rule for programming standards to be implemented in a company.

Thanks for your input.

JB :)
Nov 16 '05 #1
1 2453

"The Last Gunslinger" <jb******@yahoo.com> wrote in message
news:7W***************@news-server.bigpond.net.au...
What are peoples views on using readonly properties to return the sum of
a calculation?

For instance you might have a invoice class that has itemprice and
quantity properties.
Would the total be implemented as readonly prop Total to return
itemprice * quantity or method CalculateTotal which returns the same thing.
A readonly prop is, IMHO, more intuitive as it is still an attribute of
this class but what about when you have a longer calculation that
affects performance and as such should be stored by the consumer instead
of running the calc every time they wanted to access the 'property'.

I dont think there is a 'right' way to do this but I need to define a
clear rule for programming standards to be implemented in a company.


I think that your intuition is correct that a property is appropriate for a
simple calculation, and you should expect a property to be accessed multiple
times in quick succession. For a more involved computation I would suggest
either caching the result in a private instance field (and adding
appropriate logic to invalidate it), or using a member function instead of a
property. A function called CalculateTotal suggests a degree of work to do
the Calculating.

Then you can have a simple guideline for both creators and consumers of
types: Allow (Expect) multiple invocations of properties, but not of member
functions.

eg

if (myType.Something == 1)
.. . .
else (myType.Something == 2)
....

but,

int something = myType.GetSomething();
if (something == 1)
.. . .
else (something == 2)
....
David
Nov 16 '05 #2

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

Similar topics

16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
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...
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......
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
4
by: Josh Harris | last post by:
Here is my question: It is common to have many pieces of business logic encapsulated within asp.net user controls. This can be found in high visibility projects such as the iBuySpy portal from...
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...
8
by: SStory | last post by:
When I right a class, I am wondering what are the best practices for error handling? Do I try..catch and trap the error and if so what do I do with it? Because most likely the class user will...
6
by: Nate | last post by:
I am in a slight predicament trying to determine the most efficient and effective way to connect/disconnect from a database within a business object (c# dll). I'm also keeping in mind the concept...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.