General discussion

Locked

Designing a method signature

By Ugo Posada ·
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.

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Your Mileage May Very

by David Rawheiser In reply to Designing a method signat ...

It depends on how you are using it (what language, how often published interfaces can be changed, etc..)

I have used both specific get/set functions and some generic functions.

My generic functions tend to be where an entire properties object gets passed (a structure or delimited string with multiple attributes).

You will need to decide if your general function will fail silently or with a bang! if the property being specified is unknown (ie. the programmer passed 'nom' or 'namen' instead of 'name' as the attribute).

Collapse -

You have other options

by raf952 In reply to Designing a method signat ...

Most OO programming languages provide two features that may give you a cleaner solution: encapsulation and method overloading.

If you design your classes to implement an update() method, you would be able to say "aPerson.update(...)", "myCamel.update(...)". By using either inheritance from a base class or through implementing a shared Updatable interface, you can provide a consistent mechanism to have your classes update themselves.

If you're designing a utility class, having overloaded update methods would eliminate the need of passing a string containing the type.

void update(Person aPerson, String aValue);
void update(Camel aCamel, String aValue);
void update(Elf anElf, String aValue);
...etc.

Either of these appear superior to passing a type value, at least to me.

raf

Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums