tags:

views:

217

answers:

5

When I was programming a Form Validator in PHP, when creating new methods, I needed to increase the number of arguments in old methods.

When I was learning Java, when I read that extends is to not touch previously tested, working code, I thought I shouldn't have increased the number of arguments in the old methods, but overridden the old methods with the new methods.

Imagine if you are to verify if a field is empty in one part of the form, in an other and in yet an other.

If the arguments are different, you'll overload isEmpty, but, if the arguments are equal, is it right to use isEmpty, isEmpty2, isEmpty3, three classes and one isEmpty per class or, if both are wrong, what should I have done?

A: 
  1. Since java 5 you can use variable list of arguments as in void foo(Object ... params)
  2. You will need to come up with creative names for your methods since you can't overload methods that have same type and number of arguments (or based on return type). I actually personally prefer this to overloading anyway. So you can have isEmpty and isEmptyWhenFoo and isEmptyWhenIHaveTheseArguments (well meybe not the last one :)
DroidIn.net
A: 

Not sure if this actually answers your question, but the best way to think about OO in "real life" is to think of the Nygaard Classification:

ObjectOrientedProgramming. A program execution is regarded as a physical model, simulating the behavior of either a real or imaginary part of the world.

So how would you build a physical device to do what you are trying to do in code? You'd probably have some kind of "Form" object, and the form object would have little tabs or bits connected to it to represent the different Form variables, and then you would build a Validator object that would take the Form object in a slot and then flash one light if the form was valid and another if it was invalid. Or your Validator could take a Form object in one slot and return a Form object out (possibly the same one), but modified in various ways (that only the Validator understood) to make it "valid". Or maybe a Validator is part of a Form, and so the Form has this Validator thingy sticking out of it...

My point is, try to imagine what such a machine would look like and how it would work. Then think of all of the parts of that machine, and make each one an object. That's how "object-oriented" things work in "real life", right?

With that said, what is meant by "extending" a class? Well, a class is a "template" for objects -- each object instance is made by building it from a class. A subclass is simply a class that "inherits" from a parent class. In Java at least, there are two kinds of inheritance: interface inheritance and implementation inheritance. In Java, you are allowed to inherit implementation (actual method code) from at most one class at a time, but you can inherit many interfaces -- which are basically just collections of attributes that someone can see from outside your class.

Additionally, a common way of thinking about OO programming is to think about "messages" instead of "method calls" (in fact, this is the original term invented by Alan Kay for Smalltalk, which was the first language to actually be called "object-oriented"). So when you send an isEmpty message to the object, how do you want it to respond? Do you want to be able to send different arguments with the isEmpty message and have it respond differently? Or do you want to send the isEmpty message to different objects and have them respond differently? Either are appropriate answers, depending on the design of your code.

Daniel Pryden
+1  A: 

So the question is:

If I need different behaviors for a method isEmpty which receives the same number arguments, what should I do?

  1. Use different names? ( isEmpty, isEmpty2, isEmpty3 )
  2. Have three classes with a single isEmpty method?
  3. Other?

If that's the question then I think you should use:

  1. When they belong to the same logical unit ( they are of the same sort of validation ) but don't use numbers as version, better is to name them after what they do: isEmptyUser, isEmptyAddress, isEmptyWhatever

  2. When the validator object could be computed in one place and passed around during the program lifecycle. Let's say: Validator v = Validator.getInstance( ... ); and then use it as : validator.isEmpty() and let polymorphism to it's job.

  3. Alternatively you could pack the arguments in one class and pass it to the isEmpty method, although you'll end up with pretty much the same problem of the name. Still it's easier to refactor from there and have the new class doing the validation for you.

    isEmpty( new Arguments(a,b,c ) );  => arguments.isEmpty();
    
OscarRyz
"which receives the number arguments what should I do?" -> "which receives the same number of arguments", no?
Delirium tremens
+1  A: 

The Open/Closed Principle [usually attributed to Bertrand Meyer] says that "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification". This might be the principle that you came across in your Java days. In real life this applies to completed code where the cost of modification, re-testing and re-certification outweighs the benefit of the simplicity gained by making a direct change.

If you are changing a method because it needs an additional argument, you might choose to use the following steps:

  1. Copy the old method.
  2. Remove the implementation from the copy.
  3. Change the signature of the original method to add the new argument.
  4. Update the implementation of the original method to use the new argument.
  5. Implement the copy in terms of the new method with a default value for the argument.

If your implementation language doesn't support method overloading then the principle is the same but you need to find a new name for the new method signature.

The advantage of this approach is that you have added the new argument to the method, and your existing client code will continue to compile and run.

This works well if there is an obvious default for the new argument, and less well if there isn't.

richj
A: 

Instead having one class providing multiple versions of isEmpty with differing names, try breaking down your model into a finer grained pieces the could be put together in more flexible ways.

  • Create an interface called Empty with one method isEmpty(String value);
  • Create implemntations of this interface like EmptyIgnoreWhiteSpace and EmptyIgnoreZero
  • Create FormField class that have validation methods which delegate to implementations of Empty.
  • Your Form object will have instances of FormField which will know how to validate themselves.

Now you have a lot of flexibility, you can combine your Empty implemenation classes to make new classes like EmptyIgnoreWhiteSpaceAndZero. You can use them in other places that have nothing to do with form field validation. You don't have have have multple similarly named methods polluting your object model.

BeWarned