Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to correctly design Java method - code critique about signature, exceptions, naming, etc
    text
    copied!<p>Possible duplicate(s) : </p> <p><a href="https://stackoverflow.com/questions/4512492/method-return-values-and-exceptions">Method return values and exceptions</a></p> <p><a href="https://stackoverflow.com/questions/674317/exceptions-vs-special-return-values">Exceptions vs Special return values</a></p> <p>I'm a new Java user and I have stumbled upon a problem of correctly specifying a method. Basically, this method gets a TableInfo record and an array of String parameters and then makes some sanity checks. I have several questions:</p> <ol> <li><p>Does the name of method fully correctly reflects the essence of method?</p></li> <li><p>For me, this method is naturally a boolean returning method. However, the way I coded it, it never returns a false value! If it fails, it throws an exception with relevant message. Should it not a boolean function or what shall I change to make it a sound one?</p></li> <li><p>Should I not throw exceptions altogether?</p></li> <li><p>If you say "go on with exceptions", are my choice and usages of exceptions right?</p></li> </ol> <p>Besides these, any constructive critiques are more than welcome!</p> <p>Thanks!</p> <pre><code>public static boolean validParameters(TableInfo ti, String[] params) { if (params.length != ti.getNumberOfFields()) throw new IllegalArgumentException("Number of given arguments is not equal to number of fields(" + ti.getNumberOfFields() + ") in table!"); byte[] fieldLengths = ti.getFieldLengths(); String[] fieldNames = ti.getFieldNames(); for (int i = 0; i &lt; params.length; ++i) { String param = params[i]; if (param.length() &lt;= 0 || param.length() &gt; fieldLengths[i]) throw new IllegalArgumentException("Argument length is out of bounds for the field " + fieldNames[i] + "(Min:1,Max:" + fieldLengths[i] + ")"); } //Check if the first parameter(primary key) is integer. Otherwise, throw an exception. try { Integer.parseInt(params[0]); } catch (NumberFormatException e) { throw new IllegalArgumentException("First argument(primary key) is not in integer form"); } return true; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload