문제

I recently encountered a suggestion for the regex patterns utilised across our application code base to be pooled together into a lookup table, and retrieved from there rather than explicitly hard-coded within the program logic.

Can anyone share any thoughts as to the wisdom of this?

My initial thoughts were that it makes the patterns easier to change, which carries both advantages and disadvantages. But, fundamentally, I see a regex string itself as an entity of logic which is (effectively) executed, so storing them in a lookup table just makes me feel a little uneasy.

Thoughts anyone?

Thanks

도움이 되었습니까?

해결책

I think this is an excellent idea, If you manage to improve on a regex pattern (or even fix a problem with it), you're going to change it in a single place and not everywhere (probably the very motive for doing this).

This is going to reduce the number of potential bugs in the program (or increase them, if you change it incorrectly. However, this 'should' manifest itself almost immediately in testing and you still only have to fix it in one place, as opposed to everywhere... once again, more error-prone).

With regards to seeing it as an entity of logic, would it help if you saw it as a set of instructions that are kept in a reference library, rather than the logic itself? The logic itself is performed by the regex processor rather than the template that you have typed in code, so I see it as a fine candidate to be stored in a lookup table.

다른 팁

If it's going to result in otherwise unnecessary SQL connections, I would say, no. Don't do it. Flee to the hills.

But otherwise, if they're being often repeated, think about how your code can be refactored. Should you be using them in functions or classes which are included in several pages? If the answer's no, then go ahead, I don't see a problem with it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top