parameters vs properties.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

739 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