public methods

Should public functions check if the passes parameters are what they are supposed to be(int, char, etc) or is the calling class responsible for that ?
[157 byte] By [1compuboy1010] at [2007-9-30 22:26:57]
# 1

int foo;

public void somefunc(int foo)

{ this.foo = foo;

}

The compiler, in any circumstance I can think of, ensures that the parameter passed to somefunc is indeed an int. It shouldn't be something you need to worry about, except maybe in some very obscure cases.

sangretu at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 2

> Should public functions check if the passes parameters

> are what they are supposed to be(int, char, etc) or is

> the calling class responsible for that ?

First: instead of public say "not private"

Second: no need to check for type safety, the compiler does it for you in these cases.

Third: as soon as you take a reference or have to stick to special semantics (like an int can only be 1, 2, 3 or 1000000), your question is relevant again.

In these cases, there are two different strategies.

- defensive programming: the method itself checks the validity of the arguments (nut null, within a certain range, whatever)

- programming by contracts: you define "the method takes an int value between 0 and 100". Whoever calls it has to make sure that the arguments handed to your method are appropriate or it's his problem if something goes wrong.

It's a philosophical discussion which is better. I mostly stick to the second one, because it's much less effort.

CeciNEstPasUnProgrammeur at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 3

> Third: as soon as you take a reference or have to

> stick to special semantics (like an int can only be 1,

> 2, 3 or 1000000), your question is relevant again.

In that case Java has a facility called assertions. You can put in an unlimited number of "saneness" checks which are performed during development but switch them off (they're removed from the code) when the program enters production.

UlrikaJ at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 4
> (they're removed from the code)Well not from the source but from the bytecode version.
UlrikaJ at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 5
Or leave them on.I think, though, that assertions shouldn't be used for validating non-private method inputs.
jverd at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 6

> Or leave them on.

>

> I think, though, that assertions shouldn't be used for

> validating non-private method inputs.

Why not? Seems like a myth. Assertions is targeted as a validation tool to use during the program development phase. I see no reason why the non-privacy of methods should be a restriction. There are some rules for their best use though. They should for example not replace ordinary user input validation.

http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html

UlrikaJ at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 7

> > Or leave them on.

> >

> > I think, though, that assertions shouldn't be used

> for

> > validating non-private method inputs.

>

> Why not? Seems like a myth. Assertions is targeted as

> a validation tool to use during the program

> development phase. I see no reason why the non-privacy

> of methods should be a restriction. There are some

> rules for their best use though.

I think assertions are best suited as a way to test for a condition that you, the developer, don't think can ever occur. An external caller of your method passing you an invalid parameter is an exception, but it's not something you expect never to happen, that's why I wouldn't use assertions in that situation. Not saying i'd never use them inside a public method though.

> They should for

> example not replace ordinary user input validation.

Agreed.

jverd at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 8

> > Or leave them on.

> >

> > I think, though, that assertions shouldn't be used

> for

> > validating non-private method inputs.

>

> Why not? Seems like a myth. Assertions is targeted as

> a validation tool to use during the program

> development phase. I see no reason why the non-privacy

> of methods should be a restriction. There are some

> rules for their best use though. They should for

> example not replace ordinary user input validation.

>

> http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.h

> ml

In which case one could argue, if the input (for a non private method) is importent enough to validate, then why not just put the check in which will also be there in the final version of the class instead having something which will eventually need to be wirtten into something else and which just gives the same kind of 'usability' as an unchecked exception would ;)

Not that I'm critizising thier use, just that for non very complex things, I'll argue they are overkill.

NinjaGnu at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 9

I'd prefer to have a method to throw an IllegalArgumentException rather than an AssertionError... it's no error, but just a wrong argument. Like a FileNotFoundException, it's someting one can surely expect to happen in >>exceptional<< situations - I agree with whoever said this before.

CeciNEstPasUnProgrammeur at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 10

> In which case one could argue, if the input (for a non

> private method) is importent enough to validate, then

> why not just put the check in which will also be there

> in the final version of the class instead having

> something which will eventually need to be wirtten

> into something else and which just gives the same kind

> of 'usability' as an unchecked exception would ;)

>

> Not that I'm critizising thier use, just that for non

> very complex things, I'll argue they are overkill.

Sure, assertions doesn't belong as "parameter validators" in public methods. Since I started using them more I've found lots of good uses for assertions and are lot's of places in the code where they feel natural, like catching a null pointer exception not where it happens but where it originates for example. It's great during development.

UlrikaJ at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 11
Another place I've fould useful for assertions is to validate complex floating point calculations in time critical code.
UlrikaJ at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...
# 12

I agree that assertions are great to help you to do something foolish as you program, but at the delivering stage methods should have well defined and documented behaviour like:

"If null is passed as the file path the method will load the default settings and return" or

"if null is passed as the file path the method will throw a InvalidFileException".

Which one, depends on your design and the target of the code.

May the code be with you.

notivago at 2007-7-7 12:50:11 > top of Java-index,Security,Event Handling...