Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex or Exception Handling?

Tags:

java

regex

Which one of the following is a better practice to check if a string is float?

try{
 Double.parseDouble(strVal);
}catch(NumberFormatException e){
 //My Logic
}

or

if(!strVal.matches("[-+]?\\d*\\.?\\d+")){
 //My Logic
}

In terms of performace, maintainence and readability?

And yeah, I would like to know which one is good coding practice?

like image 809
Chandra Avatar asked Mar 08 '11 02:03

Chandra


3 Answers

Personal opinion - of the code I've seen, I would expect that most developers would tend towards the try - catch blocks. The try catch is in a sense also more readable and makes the assumption that for most cases the string will contain a valid number. But there are a number of things to consider with you examples which may effect which you choose.

  1. How often do you expect the string to not contain a valid number.
  2. Note that for bulk processing you should create a Pattern object outside of the loop. This will stop the code from having to recompile the pattern every time.
  3. As a general rule you should never use expectations as logic flow. Your try - catch indicates logic if it's not a string, where as your regex indicates logic if it is a number. So it wasn't obvious what the context of the code is.
  4. If you choose the regex technique, you are still probably going to have to convert at some point, so in effect, it may be a waste of effort.
  5. And finally, is the performance requirements of the application important enough to warrant analysis at this level. Again generally speaking I'd recommend keeping things as simple as possible, making it work, then if there are performance problems, use some code analysis tools to find the bottle necks and tune them out.
like image 101
drekka Avatar answered Oct 27 '22 18:10

drekka


  1. Performance: Exceptions are slow, and so is exception-based logic, so second would be faster.
  2. Maintenance / Reliability: The first one is crystal clear and will stay updated with updates to the Java Framework.

That being said, I would personally prefer the first. Performance is something you want to consider as a whole in your architecture, your data structure design, etc. not line by line. Measure for performance and optimize what is actually slow, not what you think might be slow.

like image 44
mellamokb Avatar answered Oct 27 '22 17:10

mellamokb


The first one is going to perform better than the regex when the string matches the double. For one it's very fast to parse it when the recognizer is hard coded as it would be with Double.parse. Also there's nothing to maintain it's whatever Java defines as the Double is as a string. Not to mention Double.parseDouble() is easier to read.

The other solution isn't going to be compiled so the first thing that the regex has to do is compile and parse the regex expression, then it has to run that expression, then you'll have to execute Double.parseDouble() to get it into a double. And that's going to be done for every number passed to it. You might be able to optimize it with Pattern.compile(), but executing the expression is going to be slower. Especially when you have to run a Double.doubleParse to get the value into a double.

Yes exceptions are not super fast, but you'll only have to pay that price when you parse an error. If you don't plan on seeing lots of errors then I don't think you'll notice the slow down from gathering the stacktrace on the throw (which is why exceptions perform poorly). If you're only going to encounter a handful of exceptions then performance isn't going be a problem. The problem is you expected a double and it wasn't so probably some configuration mistake so tell the user and quit, or pick a suitable default and continue. That's all you can do in those cases.

like image 38
chubbsondubs Avatar answered Oct 27 '22 18:10

chubbsondubs