The Artima Developer Community
Sponsored Link •

Design Forum
Designing a function

4 replies on 1 page. Most recent reply: Jan 27, 2003 10:49 PM by Tj

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Designing a function Posted: Dec 29, 2002 5:29 PM
Reply to this message Reply
Advertisement
I designed an application a couple of months ago and I came up with the following signature for a method:

void updateSomething(String type, String something)

The kind of update depends on the string value passed in the type parameter, what I remenber vividly is that I bragged a lot because there was the possibility to pass an empty string giving new meaning to the method, for instance, updating all types or whatever. I guess one gains a lot of cohesion because the method itself gives a single entry point to accomplish plenty of different things, but, at what cost. I imagine one has to document the method very strictly to explain every possible use of it, giving a hard time to the programmer for he has to examine the documentation every time he has to use it to prevent misuse.

Would it be better to have different signatures for every type, I mean:

void updatePerson(String a)
void updateCamel(String a)
void updateElf(String a)
void updateGorgeousSpaceLady(String a)

I guess it is more intuitive, but wastes a lot of space. I like to hear your thoughts about it, I would even like to hear if you approach the same problem in a more elegant way, see ya.


Ramzi Ben Yahia

Posts: 23
Nickname: miccheck
Registered: Jul, 2002

Re: Designing a function Posted: Dec 30, 2002 5:35 AM
Reply to this message Reply
> I designed an application a couple of months ago and I
> came up with the following signature for a method:
>
> void updateSomething(String type, String something)
>
> The kind of update depends on the string value passed in
> the type parameter, what I remenber vividly is that I
> bragged a lot because there was the possibility to pass an
> empty string giving new meaning to the method, for
> instance, updating all types or whatever. I guess one
> gains a lot of cohesion because the method itself gives a
> single entry point to accomplish plenty of different
> things, but, at what cost. I imagine one has to document
> the method very strictly to explain every possible use of
> it, giving a hard time to the programmer for he has to
> examine the documentation every time he has to use it to
> prevent misuse.
>
> Would it be better to have different signatures for every
> type, I mean:
>
> void updatePerson(String a)
> void updateCamel(String a)
> void updateElf(String a)
> void updateGorgeousSpaceLady(String a)
>
> I guess it is more intuitive, but wastes a lot of space. I
> like to hear your thoughts about it, I would even like to
> hear if you approach the same problem in a more elegant
> way, see ya.

Hi,
in fact what you're trying to do is replacing the setXXX javabeans standard with an updateXXX.
this gonna break the introspection feature, do you really want to do that?. but if you think that your class code is getting very huge because of the number of getter/setter methods maybe you'll better try to do some design work using a composition pattern for example.
for more informations have a look on the java.beans package I think it will be very helpful about this subject.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Designing a function Posted: Jan 6, 2003 7:06 PM
Reply to this message Reply
You could also have : setName (String[] keyValue);
or setNames (Hastable keyValue);
You'd then have defined on an interface all th possible values authorized for the "key". Caller would then do a :
zoo.setName (new String [] {Type.CAMEL, "The Camel's name"});
or for a more enghty setXXX
Hashtable ht = new Hashtable ();
ht.put (Type.CAMEL, "The Camel's name");
ht.put (Type.LION, "Lion's name");
// ... a few more of those
zoo.setNames (ht);
None of these are good for you but they may inspire you to find smth that fits your needs.

Thomas,

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: Designing a function Posted: Jan 8, 2003 5:27 AM
Reply to this message Reply
Well guys thanks for your interest but I guess I didn?t make myself clear.

I was trying to split my design ideas in two:

1. Create chunky functions, where the functionality of it is decided by parameters passed or not, for instance, the function can create a user or a guest depending on a parameter that I pass.

2. Create a function for each task I want to solve, in the latter example I would have a createUser and a createGuest functions.

What do you guys think?

Tj

Posts: 7
Nickname: tj
Registered: Jan, 2003

Re: Designing a function Posted: Jan 27, 2003 10:49 PM
Reply to this message Reply
Your original design sounds fine to me, in fact it's the most natural way to do it. In Java, functions and methods are "hard" things; they don't do much at run-time. So you want to be dynamic, using methods as little as sensible.

Flat View: This topic has 4 replies on 1 page
Topic: rfe.request Java feature. enforce call to super.bar() with 'super' keyword Previous Topic   Next Topic Topic: about fingerprint identification system for personal computer security

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use