Clean code is everything
The most important thing I learn in 2015 is that clean code is everything. You can have bad performance or logical errors or even security issues.
Without clean code you cannot fix anything above, because you don't understand the code.
Regexp versus clean code
The regular expressions were always dramatic for fast reading. Unless there was a variable which describes it well, I need a lot of time to understand what exactly is going on.
Take a look on example from StackOverflow question - Regular expression for matching account numbers:
^Acc(?:oun)?t(?:\s+Number)?.+[\d-]+$
Reading it through gives me an idea that string should contain:
- some parts of
Account Number
- digits in the end of the string.
But can you answer which of lines in below requirement match above expression. The requirement was:
I need a regex which checks each line starting with “Acc”, ignore white space and any special character and then ending with numeric.
Moreover is should:
However, in addition to account numbers it is also matching other patterns such as "Bank ID : 12345"; "Account Name : ABC" . ; "Account Address" etc. Any way to exclude these?
Nowadays there are tools like Jex to visualize regular expressions and make them readable:
How about code?
There is a problem with above solution. We need additional tools or plugins to see what is going on in one line in code. Which make this solution useless until we are debugging an error.
Example solution is to provide few test to above line, but still I doesn't make code more readable. It just make it easier to investigate.
Can you image that above code will look like:
var tester = VerEx()
.startOfLine()
.then( "Acc" )
.maybe("oun")
.then("t")
.maybe(VerEx().add("\\s+").then("Number"))
.anything()
.then(VerEx().then("-").or().range(0,9)).add("+")
.endOfLine();
which produces expression like:
The above code is using [VerbalExpressions] (https://github.com/VerbalExpressions/JSVerbalExpressions) library. There are also ports to other languages like C#, Java, Ruby or Objective-C on http://verbalexpressions.github.io/
Above code is much more readable but I miss two function (and I was cheating above):
oneOrMore()
instead of add("+")
. Existing function repeatPrevious
only allows to repeat item exactly number of times. It produces {x}
or {x,y}
expression
whitespace()
- above I used add("\\s+")
which is a second cheat. They implemented functions like word()
or tab()
or even lineBreak()
With this two functions we can make more clear code like:
var tester = VerEx()
.startOfLine()
.then( "Acc" )
.maybe("oun")
.then("t")
.maybe(VerEx().whitespace().oneOrMore().then("Number"))
.anything()
.then(VerEx().then("-").or().range(0,9)).oneOrMore()
.endOfLine();
After reading above code I start to feel that requirements were strange and it is possible to make them simpler.
Update: pull request is pending merged on github
@cover from from http://wall.alphacoders.com/big.php?i=157189