473,569 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2463

"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.Somethi ng == 1)
.. . .
else (myType.Somethi ng == 2)
....

but,

int something = myType.GetSomet hing();
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
2994
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 example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
136
9251
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to...
14
3117
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... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
0
4208
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 Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com....
4
2434
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 MS. Virtually every bit of the site is encapsulated within user controls. The problem begins when you try to have user controls pass information...
10
3426
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 somewhere that each folder under the "web site" is compiled in separate assembly. I however, did not find that the "web site" creation in vs.net...
8
1553
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 want to know the information in the exception.... That being the case do I just not catch it and let the user of the class catch it and get all the...
6
1627
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 of connecting late and disconnecting early. Background: - multi-tier application (code-behind uses properties and methods of the business...
41
2835
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 created and then added to "component" objects to build up the component definition. My dilemma comes in deciding how to read/write data to the...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.