Skip to content
This repository has been archived by the owner on May 28, 2022. It is now read-only.

Java: specify guidelines for argument validation #71

Open
damithc opened this issue Feb 27, 2017 · 1 comment
Open

Java: specify guidelines for argument validation #71

damithc opened this issue Feb 27, 2017 · 1 comment

Comments

@damithc
Copy link
Contributor

damithc commented Feb 27, 2017

Specify when to (and when not to ) validate method arguments

@damithc damithc changed the title Java: specify guidelines for using assertions Java: specify guidelines for argument validation Feb 27, 2017
@damithc
Copy link
Contributor Author

damithc commented Feb 27, 2017

It's quite hard to find established guidelines on this aspect. The best I could find is this extract from Effective Java.

Previous version of the guidelines (discussed halfway, but not merged) can be found here (refer to the deleted lines). Those guidelines are a bit hard to follow/enforce.

Perhaps a simpler (to follow/enforce) approach is to mandate argument validation for all public methods? i.e. for all public methods,

  1. Try to minimize restrictions on arguments. Instead of restricting a specific type of argument (e.g. null), implement a reasonable behavior for that argument.
  2. At the start of the method, validate arguments against all restrictions in force. Use suitable type of exceptions (preferably, built-in exceptions) to indicate violations.
  3. Document all restrictions and the corresponding exceptions in the header comment.

That still leaves the question of validating arguments in non-public methods.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant