开发者

Using a lookup table for regex patterns

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 t开发者_高级运维hat 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜