• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validate arguments to a public method

 
Preeti Yarla
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Preeti Yarla
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell! Thanks Keith!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only too pleased to help
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Preeti Yarla
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Preeti Yarla:
OK.
Thanks again, Garrett and Campbell!
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic