Solved

parameters vs properties.

Posted on 2009-05-18
4
1,097 Views
Last Modified: 2012-05-07

I am a little confused regarding when to use parameters to pass variables to methods and when to use properties.

For example

code.test(val1,val2);

or

code.property1=val1;
code.property2=val1;
code.test();

I have read that it always best to use properties, but when I look at the code made by some code generators they use parameters.

Hmmm. so confusing.

Parameters make more sense to me since I would be able to easily identify what values a method would need to work.

But then there is the argument that properties are mode secure/stable.

Can anybody shed some light on this issue?


or

code.property1=val1;
code.property2=val1;

I have read that it always best to use properties, but when I look at the code made by some code generators they use parameters.

Hmmm. so confusing.

Parameters make more sense to me since I would be able to easily identify what values a method would need to work.

Can anybody shed some light on this issue.
0
Comment
Question by:Mr_Shaw
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24414780
(I think you accidentally hit copy/paste twice?)

You hit at the very heart of Object Oriented programming. There are many books that explain you what is good design and what is bad. They don't always agree to one another and you may find quite some heated discussions going on. Yet, there is also some consensus and it is not so hard to take a few rules of thumb for yourself for when to use parameters and when to use properties.

  1. A property says something about an object, for instance it current height or its color. It is part of the "state" of the object and if think of your object without any methods, but only the properties, would it have enough information to regenerate itself? If yes, your properties are well defined.
  2. A parameter is something that is needed for a particular action, and sometimes a parameter can be the same as a property (confusing? hold on). Suppose you have the method Resize(int width, int height). That method takes two parameters, width and height, and should resize your object (form, button, picture, aeroplane). After it is finished, it should (must!) keep the state and change the properties as well, to make sure that they match.
  3. Same example, now your user sets the property Width. The user will now expect that the object will resize. So in the property set code, the Resize method is called with the current height and new Width.
  4. Sometimes you have an action that should not be remembered. For instance, calculate square root. This Sqrt(int number) function takes one parameter, but does not store that in a property. The reason: all it does is come action, calculation, that does not change the state of the object.
  5. Sometimes (often!) you have a property that does not really need any action. It only sits there for being retrieved. For instance, an Address object does nothing more than having properties like Name, Surname, AddressLine, City, PostalCode etc. But no actions whatsoever. There's a term for the kinds of objects that almost only have properties and no behavior: entity objects.
  6. And sometimes all you need is some calculations, you don't need to keep anything from your class. You can place these methods inside a static methods. They just calculate and only take parameters. A class that does not maintain state at all and only has static methods is called a static class.
Now, I promised you some rules of thumb after all this lecturing. The rule of thumb to use goes reverse: if you find yourself programming and passing data on from one method to the other to the other to the other etc, using the parameters, then you must rethink your design and probably add some properties. They prevent you from having complex long methods where state is preserved by passing them on from each function to the other.

The end result is, a clean design and better understanding. The actions become easier (they act on properties and sometimes some extra input through parameters) and the state of the object becomes clear.

Sorry for the long post, but that's what happens with theory questions. I hope I didn't make it too hard on you ;-). A good book that helps you walking through a current design that you think should be bettered (i.e., extract methods and put them into properties), is Refactoring by Martin Fowler, who makes this process a breeze.

-- Abel --
0
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24415121
Good response, Abel.
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31582699
Thanks
0
 
LVL 39

Expert Comment

by:abel
ID: 24425027
You're welcome. Tx for the compliment, ToddBeaulieu :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video teaches viewers about errors in exception handling.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now