0

As far as I know, declaring an interface is like so:

public interface Roots_Squares {
    public double square_root( double value );
}

Now... how do you enforce value to have non-negative values? Such a function should avoid negative values right? If someone creates a class that implements this, and that implementation does not handle negative values, and then I use their class with a negative value... well, things break.

It then becomes my responsibility to check for negatives when I'm pretty sure the implementor should do the checking.

4

4 Answers 4

3

As explained a bit more thoroughly in this answer, that's (part of the) price you pay for an interface. If you want to be able to use new implementations of your abstraction at any time, you have no way of forcing those implementations to be correct.

10
  • There is, of course, validation. Granted, you have to implement that in the class, not the interface. Apr 4, 2014 at 19:20
  • @RobertHarvey But someone always has the option of not using your class and producing a totally new and bogus instance of the interface.
    – Doval
    Apr 4, 2014 at 19:22
  • So how about, instead of double, a custom, non-negative derived class of doubles? So the function becomes "public double square_root( Positive_Double value );" or would that be too much of stretch? Apr 4, 2014 at 19:29
  • @user2738698 There's a couple of issues. 1) I wouldn't make it a subclass of the double class because it doesn't behave like a double. Particularly, doubles are closed under subtraction - subtracting two doubles results in another double. For positive doubles though, 3.0 - 5.0 blows up because -2.0 is not a positive value.
    – Doval
    Apr 4, 2014 at 19:37
  • 2
    "There's no mechanical way to ensure a program won't generate a negative number during the course of its execution without actually executing it." – Only if the language is Turing-complete. In dependently-typed languages, types such as "non-negative integer" or "list of length n, where n is a runtime value" can be expressed, and can be statically type-checked. Apr 5, 2014 at 0:32
0

The comments on Doval's answer are right: create and use a PositiveDouble. Also, consider using a design by contract tool. A design by contract tool like cofoja might solve your problem. I have not used 1 but I know that there are several design by contract tools in Java.

1
  • The other comments are right, however, in that there is no realistic way of creating such a type in a traditional object-oriented language like Java. It requires much better type inference than is available in such languages in order to not be a nightmare to use such a type.
    – Jules
    Nov 27, 2014 at 7:58
0

Your interface should document how it handles bogus input, and can declare Exceptions it will throw. In this case, a simple

@throws IllegalArgumentException would do the trick.

-1

Doval's answer is outdated now. You can use an enforce parameter properties(check arguments) in the interface:

public interface Math {
    /**
     * @throws IllegalArgumentException if value <= 0
     */
    default public void sqrt(Double value) {
        if(value <= 0) throw new IllegalArgumentException("value must be greater than 0");
        squareRoot(value);
    }

    public void squareRoot(Double value);
}
1
  • Programmers is about conceptual questions and answers are expected to explain things. Throwing code dumps instead of explanation is like copying code from IDE to whiteboard: it may look familiar and even sometimes be understandable, but it feels weird... just weird. Whiteboard doesn't have compiler
    – gnat
    Dec 2, 2014 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.