Re: Passing a lot of parameters in constructor?
- From: "topmind" <topmind@xxxxxxxxxxxxxxxx>
- Date: 12 Feb 2007 09:30:17 -0800
andrewmcdonagh wrote:
On Feb 11, 5:55 pm, "howa" <howac...@xxxxxxxxx> wrote:
for example, a Person class, I need to create a person using, e.g.
Person peter = new Person('Peter', '....)
the object creation might involved over 30 parameters, the advantage
is this call is atomic, rather than something like, e.g.
Person peter = new Person();
peter.setName('Peter');
peter.setBirthPlace('....
...
Are there any platterns, suggestion or anti-plattern related to this
problem?
thanks.
I find, that when I need to pass more than 6 parameters, the code base
is shouting to me 'Your design sucks'. It sucks because I've negated
to see that there are relationships between two or more of the
parameters, that should be described using a dedicated class, and
therefore a single instance of that class as a parameter. As for an
anti-pattern, I'd say 'primitive obsession' is probably one of the
biggest culprits.
So using your example....
peter = new Person("Peter", 35, 123, "Sycamore Road", "Sometown",
"Some county", "a postcode/zip code", "0044123
991199", ..............)
Here, because we have 'primitive obsession' (i.e. using primitive
types: String & ints) we are forced to send everything through as
individual objects.
Now, if we look for relationships between those parameters, the
obvious one we see is the parameters that make up Peter's address.
Oh hang on, that even gives us the missing classes name 'Address'. So
by moving those 5 address related parameters into their own class, we
can insubstantially reduce the parameter passing needed AND we have
increased the quality and safety of our design.
Address petersAddress = new Address(123, "Sycamore Road", "Sometown",
"Some county", "a postcode/zip code");
peter = new Person("Peter", 35, petersAddress, "0044123
991199", ..............);
So why did I say 'safety' of design?
Looking at the original constructor:
Person(String name, int age, int housenumber, String firstLine, String
secondLine, String thirdline, .....)
Its unsafe from a compile time checking point of view, as there is
nothing to catch errors.... We could safety do..
peter = new Person("a postcode/zip code", 123, 35, "Sycamore Road",
"Sometown", "Some county", "Peter", "0044123 991199", ..............);
This would compile, but then the values would be wrong as 'Peter'
would actaully be called 'a postcode/zip code' and he would be 123
years old instead of 35 and his post/zip code would be 'peter'.
Now we don't need to go overboard with turning every thing into its
own class, like the Address one, but the technique is useful and worth
using when you see large parameter lists.
That is a good suggestion. Larger databases tend to have some form of
a Contact table that stores typical address info, which is common for
vendors, customers, employees, etc. However, there can easily be more
than 7 attributes for a Contact record such that we are still back to
the original problem. Example:
Table: Contact
------------
contactID
title // (Mr. Dr. Etc.)
firstName
lastName
middleName
aliasName
homePhone
workPhone
messagePhone
pagerNumber
otherContact // (for I.M., fax and whatever new tech comes alone)
addressLine1
addressLine2 // (some postal systems require no line longer than 30
chars, so we split)
city
zipCode
country
There are ways to simplify some of this, but still the potential for
more than 7 items is still great.
hth
Andrew
-T-
.
- References:
- Passing a lot of parameters in constructor?
- From: howa
- Re: Passing a lot of parameters in constructor?
- From: andrewmcdonagh
- Passing a lot of parameters in constructor?
- Prev by Date: Re: Passing a lot of parameters in constructor?
- Next by Date: Re: Passing a lot of parameters in constructor?
- Previous by thread: Re: Passing a lot of parameters in constructor?
- Next by thread: Re: Passing a lot of parameters in constructor?
- Index(es):
Relevant Pages
|
Loading