This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Validate arguments to a public method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Validate arguments to a public method" Watch "Validate arguments to a public method" New topic
Author

Validate arguments to a public method

Preeti Yarla
Ranch Hand

Joined: Sep 08, 2007
Posts: 69
So far I have been using if conditions to validate the user input for public method arguments. Is that a standard way to validate such data or are there some other ways? (like using assert for validating private method arguments)

thanks


SCJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Yes, "if" is probably the best way to validate arguments.

Use "assert" while you are developing and testing the application; it will show where it is possible to "break" the class' invariants.Wherever you can "break" the invariant in testing, you have a vulnerability which should be converted to a precondition and then to a guard.The javadoc comments declare the precondition, and the if . . . throw converts it to a "guard" and enforces it.
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Asserts assert that a condition should never occur
so it's illegitimate to validate args to public methods and command line arguments. You as a programmer should ensure that any public methods that can be called via an API will verify the integrity of the arguments to the public methods. Remember that asserts are enabled by default by the Java 5 compiler but disabled by default by the JAVA command.
Since your class can only call it's private methods from within the same class, you yourself will only be invoking the private methods so you have exclusive control over the arguments to the private methods and therefore if you use asserts to verify the integrity of the arguments to a private method you know that they will always be legit, because you invoke them!
Best of luck!


SCJP 5.0
Preeti Yarla
Ranch Hand

Joined: Sep 08, 2007
Posts: 69
Thanks Campbell! Thanks Keith!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Only too pleased to help
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You can also avoid some extra lines of code by creating a method which clarifies your intent:

And I'll borrow Campbell's example to show its use:


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Preeti Yarla
Ranch Hand

Joined: Sep 08, 2007
Posts: 69
Garret, That's really neat (compared to my code)

But what if the requirement is only one small condition, like (temperature >= -273) ? Wouldn't it be efficient to write the code than to call a function since calling the function involves some overhead?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Preeti Yarla:
But what if the requirement is only one small condition, like (temperature >= -273) ? Wouldn't it be efficient to write the code than to call a function since calling the function involves some overhead?


The difference is likely to be unnoticeable. It's definitely not enough to make the code inefficient. We're talking about fractions of milliseconds here. You wouldn't put all your code into a main() method just to avoid the overhead of method calls would you?
[ July 07, 2008: Message edited by: Garrett Rowe ]
Preeti Yarla
Ranch Hand

Joined: Sep 08, 2007
Posts: 69
I usually try to put each specific functionality into a different method but sometimes I am not sure how to choose between code reuse and efficiency. Like cases where the called function only does one small computation or evaluates one condition. If there are 10 methods that need to validate their arguments it would really look redundant to write the validation code in each of them. But again wouldn't all the milliseconds add up when we call a function to do that?

Do I even need to think about all these?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Originally posted by Preeti Yarla:
I usually try to put each specific functionality into a different method but sometimes I am not sure how to choose between code reuse and efficiency.
Always make "fast code" your bottom priority. Write correct code, simple code, maintainable code, stylish code (meaning conforming to your style guide) and the compiler will do all the optimising you need. Read this interview with Brian Goetz.

99% of the time the only optimisation the programmer can do which actually helps is to avoid inefficient algorithms.
Preeti Yarla
Ranch Hand

Joined: Sep 08, 2007
Posts: 69
OK.

Looking at one point Brian Goetz says - that performance problems are not because of coding inefficiency but are result of architectural designs - I should ignore these minor things and concentrate on good OO principles for coding.

Thanks again, Garrett and Campbell!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Originally posted by Preeti Yarla:
OK.
Thanks again, Garrett and Campbell!
You're welcome
 
GeeCON Prague 2014
 
subject: Validate arguments to a public method