Originally posted by: Dhaval00
Originally posted by: presidentender
Originally posted by: Crusty
Which is fine until the password requirements change and somebody else has to maintain that code. Regex's are very easy to maintain, that code isn't so easy to maintain.
Not always. And... if that code's not easy to maintain, I'm really, really sorry.
For me, this was too big a thing to leave hanging in there... you know, programmers and their big egos. Finally gathered some time to run a test (I've come up with many similar ones in the past to prove the point).
Refer to the code:
http://rafb.net/p/shpG7m65.html
Couple of issues before I start discussing my points in detail:
1) I stole the random string generation code from a blogpost, so credit to the original poster. As a side note, there is a bug, but that doesn't matter.
2) The manualMatches collection ends up with more matches (just a few more) - I think this is because the logic is missing something.
On average, the regex method is two to three times faster. I don't think the gap will widen because I have run similar tests in the past, and this scenario is not an exception. Having said that, maybe you can enhance your logic and see if you can reduce the time-to-completion on the manual method. My guess is that the call to TryParse is the most time consuming... I believe it is wrapped within a try a catch which puts extra burden on the CLR.
Why do I emphasize this (using regex)? Primarily, because this scenario lends itself to using regexes. Secondly, I'll try and squeeze out the extra ounce of performance if I have to (needless to say, not at the expense of making the code crappy and convoluted) using the tools I know [sane-fully]. In this case, we're talking about the Framework, not necessarily using something like C++ or Assembly to squeeze the extra ounce of performance.
I don't see what Jeff Atwood's article has to do anything with the discussion that we have been having here. This is a perfect scenario where a regular expression should be used. If you've been coding/programming for a few months, you better know basic Regex syntax... there is no excuse for it. Atwood is arguing about the overuse of regexes. I'd say this is a case where the regex is pretty simple. In fact, so simple, that I was able to pull it out of my head (and that that I have used it many times).
This is not meant to be an ad hominem argument, so again, please ignore any of the statements that sound too 'personal.' I'll back off from promoting the use of a regex if you can still come up with a faster way to check the password using the "if" logic. For me, performance is important, which is the point I should have made instead of asking "who is going to maintain that?" For the OP, performance may be secondary, but even in that case, that isn't reason enough to not use a RegEx.
Edit 1: As a side note, I know I sound hasty, but time is of essence
🙂. Ignore any and all grammatical errors. Also, I remembered the code has a LINQ statement at the end... I was trying to pinpoint the bug that was causing the # of matches in manualMatches to be larger.. ran out of time.
Edit 2: Just observed that the manual method is not even checking the entire string (line 108). Which means, if we modify the logic, chances are that the manual method will be even slower.