Regex is a pain.
Don& #39;t force people to learn it every time they read your code.
Here& #39;s how I regex with empathy:
Don& #39;t force people to learn it every time they read your code.
Here& #39;s how I regex with empathy:
So much regex passion! You love to see it. Tech twitter doesn’t disappoint.
Attempting to respond to frequent replies below.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
Attempting to respond to frequent replies below.
I haven’t met anybody that’s learned regex once and remembered it forever. I find I have to re-teach myself regex every time I see it.
This example is simple, yes. But regex gets complex.
Perhaps your memory is better than mine. More power to you.
Be cautious with this. Your company might have strict policies about sharing confidential source code with a third party. Even for a small regex.
The example here is common enough, but other regular expressions are sensitive.
Fair. I& #39;d recommend linking the specific tool in the source code. Some are better than others.
Again, be cautious. Though unlikely, it’s within that tool& #39;s power to start POST-ing the regex back to their servers.
This is a simple example. For other regex you might have trouble coming up with a name.
Yes, there’s a risk the code can be changed without the comment. It’s a tradeoff.
You should unit test. Unit tests and documentation need not be mutually exclusive.
But using regex often results in a large number of tests to cover edge cases. It& #39;s difficult to deduce what the regex does with unit tests alone.
I agree. The comment is intended to describe the function of ^ and $ within the expression, not their literal definition in regex.
By definition those match the start and end of the string. The comment can be improved.
Relax. It’s just code. There are tradeoffs and this won’t work for everyone in all use cases.
Here’s a picture of me to cheer you up.