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

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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries email authority 6 75
Reccomended programming language for client-server applications 12 129
Problem to Alipay 10 44
Formula for calculating ROI on training 6 49
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.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

838 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