Using Java, we created a method to determine if it is an integer.
private boolean isInteger(String str){
Integer parsedStr;
try{
parsedStr = Integer.parseInt(str);
} catch(NumberFormatExceptione){
return false;
} catch(NullPointerExceptione){
return false;
}
return true;
}
However, this method seems to violate item 57 written in Effective Java
Exceptions, as the name implies, should only be used for exceptional conditions.It should never be used for normal control flows.
So I came up with a way to use a regular expression to determine if the string of the provisional argument is an integer to avoid a violation of item 57.
private boolean isInteger(String str){
Matcher matcher = PATTERN.matcher(str); // PATTERN shall be defined somewhere
return matcher.matches();
}
However, because I am not used to regular expressions myself, I feel that the processing method of catching exceptions in the parseint method is more efficient and readable than using regular expressions
Generally, it may be a violation of item 57, but I feel that there are more advantages than disadvantages in such an example.
Now, I have a question, should we not capture exceptions in determining such integers (the disadvantages of violating item 57 are significant)?
java
What does the isInteger()
method mean?Consider two patterns.
If it is 1, I don't think there is any need to make any significant changes from the current implementation, because the implementation is exactly what it means.Rather, it is difficult to distinguish between integers that exceed the int
range (simply \A[+-]?0*\d{1,10}\z
, where "2147483647"
also matches "2147483648"
in the range.The regular expression is more complex because
.Integer.parseInt()
can interpret so-called full-width numbers (U+FF10'0'
to U+FF19'9'
).Therefore, using exception handling for Integer.parseInt()
is quite normal considering its readability and ease of implementation.If you try to perform string analysis by force, you will almost end up doing the same thing as implementing Integer.parseInt()
, which will be wasteful.If you dare to fix it, it might be a good idea to return early without catch
NullPointerException
private boolean isInteger(String str){
if(str==null)return false;
try{
Integer.parseInt(str);
} catch(NumberFormatExceptione){
return false;
}
return true;
}
Regardless of the implementation of Integer.parseInt()
, if you want to see if it's an integer decimal representation, regardless of int
, it might mean something like 2.
isInt
.What is the decimal representation of an integer?For example, the following definitions are possible:
'0'
to U+0039'9'
.'+'
or U+002D'-'
.'0'
may be buried on the left.("42"
as well as "00000042"
)With this definition, using regular expressions would be \A[+-]?\d+\z
, so it would look like this:
private boolean isInteger(String str){
return java.util.regex.pattern.matches("\\A[+-]?\\d+\\z", str);
}
You do not necessarily need to use regular expressions.If you are not good at regular expressions, you can do the following:
private static boolean isInteger (String str) {
if(str.isEmpty()) return false;
var first = str.charAt(0);
varskip=first=='+'||first=='-'?1:0;
return str.chars().skip(skip).allMatch(c->c>='0'&c<='9');
}
Of course, you cannot use Integer.parseInt()
to implement this 2.
There are many books and articles in the world, including Effective Java, that say, "I recommend you to write this code," but you don't have to force yourself to follow them.Instead of following the principles, we should consider whether the meaning of the principles applies to the current code, and conversely, whether there is any deterioration in readability or efficiency.There's no answer like this way.It's important to ask for more, but if you ask too much, it can only reduce efficiency, so it may be important to have a sense of balance.
I didn't read Effective Java, but if the quoted text is to the same effect as other best practices for handling exceptions, "You should not use the method of throwing exceptions.It should be rewritten by another means. is not an argument.
The key is whether isInterger throws exceptions and how they are used.What happens to the internal implementation of isInteger is not a point.
If isInteger is a method that throws exceptions when it does not meet the criteria, and it is just like checking parameters for higher methods, it is a bad practice.
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
576 Who developed the "avformat-59.dll" that comes with FFmpeg?
578 Understanding How to Configure Google API Key
584 PHP ssh2_scp_send fails to send files as intended
623 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.