0

Say you have a console application with:

Main.java and ClassA.java

Your main class contains the public static void main(String[] args) method where you run your program, and it uses user input as arguments to call on methods in ClassA.

Do you put user input verification in the main method before sending the arguments to ClassA methods?

OR

Do you put user input verification in the ClassA methods which would relay a message back indicating successful input or not? Or throw a exception?

i.e. Integer.Parse(...) throws NumberFormatException when given an invalid input.

NoName
  • 9,824
  • 5
  • 32
  • 52
  • Does the input come from the console or from a graphical user interface that is provided in "ClassA"? If you imagine the classes are persons... who would "own" those arguments? Can you imagine to use ClassA in another context with exactly the same arguments? Is there only ClassA or also ClassB and ClassC using those arguments? – Stefan Oct 11 '17 at 17:03
  • @Stefan Input come from the console (non-graphical user interface) – NoName Oct 11 '17 at 17:05
  • And ClassA is only being used by the 'main' method. – NoName Oct 11 '17 at 17:11
  • I don't think there's a definitive answer. But taking a MVC (Model View Controller) design approach the validation (in my opinion) should be conducted by a Controller to ensure the Model (ClassA) does not receive invalid requests. Personally I wouldn't consider input validation to be business logic, and so doesn't belong in the Model. If you introduced a Controller, you would then be able to switch to a graphical View (rather than console) without the need to reimplement the vaidstion logic. As this may not be a concern, I'd introduce the validation logic to your Main class. – d.j.brown Oct 11 '17 at 17:11

1 Answers1

1

You might want to throw meaningful exceptions in classA and let your main method decide if/how to handle those exceptions. When you reuse class A, the new caller might want to make other decisions.

I recommend the chapter "ErrorHandling" of the Book CleanCode (Robert C. Martin). It says for example "Define the normal flow", "Use Exceptions rather than return codes", "Separate your business logic and your error handling".

In some cases it is important to get fast feedback... not wait two hours for a calculation to be finished and get an exception at 90 %. In other cases it is more important to get detailed feedback. Also see Data validation: fail fast, fail early vs. complete validation

Check if it easy to read your code and to understand the normal flow. Each function should only do one thing/model one level of abstraction. Exception handling is one level ob abstraction.

Stefan
  • 10,010
  • 7
  • 61
  • 117
  • Thanks, I've decided at the UI level to make sure that the input is in the correct data type to be passsed to the class methods (i.e. the credit card number is actually a number). And in the class method, validate business logic (i.e. if the credit card number is actually a credit card number). – NoName Oct 12 '17 at 15:04
  • Mabe commons cli is of interest to you: http://commons.apache.org/proper/commons-cli/ Further options for parsing command line arguments: https://stackoverflow.com/questions/367706/how-to-parse-command-line-arguments-in-java – Stefan Oct 12 '17 at 17:25