Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


parameters vs properties.

Posted on 2009-05-18
Medium Priority
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




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?



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.
Question by:Mr_Shaw
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 39

Accepted Solution

abel earned 2000 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 --
LVL 16

Expert Comment

ID: 24415121
Good response, Abel.

Author Closing Comment

ID: 31582699
LVL 39

Expert Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

660 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