4 ms·
My problem is that regexes are write-only, unreadable once written (to me anyway). And sometimes they do more than you intended. You maybe tested on a few input
by kelafoja 1y ago
My problem is that regexes are write-only, unreadable once written (to me anyway). And sometimes they do more than you intended. You maybe tested on a few inputs and declared it fit for purpose, but there might be more inputs upon which it has unintended effects. I don't mind simple, straight-forward regexes. But when they become more complex, I tend to prefer to write out the procedural code, even if it is (much) longer in terms of lines. I find that generally I can read code better than regexes, and that code I write is more predictable than regexes I write.
- fragmede 1y agoYou know you can write comments in your code where the regexp is, right?
- deleted 1y ago[deleted]
- kelafoja 1y agoYou know that there are more friendly sounding ways to give this suggestion, right?
- latexr 1y ago> unreadable once written (to me anyway). (…) there might be more inputs upon which it has unintended effects. https://regex101.com https://regex101.com can explain your regex back to you, and allows you to test it with more inputs. Though I’m not trying to convince you to always use regular expressions, I agree with GP: > Obviously regexes aren't the right tool for every job, and they can certainly be done poorly; but in the right place at the right time they're the simplest, most robust, easiest to understand solution to the problem.
- bazoom42 1y ago> I tend to prefer to write out the procedural code, even if it is (much) longer in terms of lines. This might work for you, but in general the amount of bugs is proportional to the amount of code. The regex engine is alredy throughly tested by someone else while a custom implementation in procedural code will probably have bugs and be a lot more work to maintain if the pattern changes.
- rerdavies 1y agoIn general, the correctness of the code is proportional to its readability. I also prefer procedural code instead of regexes.
- bazoom42 1y agoSurely complexity is a factor? A procedual implementation will necessarily have the same essential complexity as the regex it replaces, but then it will additionally have a bunch of incidental complexity in matching and looping and backtracking. Regexes can certainly be hard to read - the solution is to use formatting and comments to make them easier to understand - not to drown the logic in reams of boilerplate code.
- kelafoja 1y ago> A procedual implementation will necessarily have the same essential complexity as the regex it replaces I don't think I fully agree with this, and I don't see a basis for why this should be true. If I have a very specific implementation, it could have very little incidental complexity, it could be fully targeted to the use case. Whereas with regular expressions there is incidental complexity of the regex engine itself by definition.
- bazoom42 1y agoComplexity in the standard library is not that relevant. If you make your own custom dictionary implementation, you increase complexity of your code base compared to just using the one in the standard library, even if your own implementaion is simpler. The relevant complexity for using a regex is the complexity of the pattern itself and the complexity of invoking the regex. Any custom procedural solution will be more complex unless it is literally something as simple as checking whether a string contain a given literal string.
- rerdavies 1y agoFor some arbitrary definition of complex.
- jcelerier 1y agoWhat makes them unreadable to you ? 99% of the time you can just read them character by character with maybe some groups and back references
- bluecheese452 1y agoI don’t think this is a particularly useful question. If they could accurately describe what exactly is confusing they wouldn’t be confused.
- bena 1y agoKind of fair. I don't incorporate a lot of regular expressions into my code. But where I do like them is for search and replace. So I do treat them as mostly disposable.
- rusk 1y agoThese are all valid criticisms of regex but they’re not an excuse to avoid regex. Similarly git has many warts but there’s no getting around it. Same with CSS If you want to run with the herd though you need to know these things, even enjoy them. You can rely on tooling and training wheels like Python VERBOSE but you’re never going to get away from the fact that the “rump” of the population works with them. Easier to bite the bullet and get practised. I’ve no doubt you have the intellect - you only need be convinced it’s a good use of your time.