Ask HN: Settle this simple elegant vs. readable disagreement
if (/ip(hone|od)/i.test(navigator.userAgent))
if (/iphone|ipod/i.test(navigator.userAgent))
Which would you use? I argue for the second, as it is simple, clear, easy for maintainers to quickly grok. I think simplicity is more important.
My colleague argues for the purity and elegance of the first solution. He thinks purity/elegance is most important.
Anyone have an opinion on this?
[+] [-] pjungwir|11 years ago|reply
[+] [-] monroepe|11 years ago|reply
[+] [-] mrcold|11 years ago|reply
The first statement is indeed nice and elegant. A small work of art from a coding perspective. However, maintaining in the future will be a pain in the ass. Because art is the eye of the beholder. And future beholders won't appreciate it as much.
On the other hand, the second statement is structured. You know exactly with what you're dealing with. It's ugly. But it's a building block. Anybody can understand it and use it immediately.
So tell your colleague to make a screenshot of the first one and frame it on his wall. Then use the second statement in production. Code must always be generic. Personalization only leads to technical debt.
[+] [-] brudgers|11 years ago|reply
On the other hand, it may be wise to caution them against a career in the arts: An aesthetic judgement that extra parentheses express purity is of dubious quality.
[+] [-] auganov|11 years ago|reply
if (/ip(od|hone)/i.test(navigator.userAgent))
is easier to figure out than
if (/ip(hone|od)/i.test(navigator.userAgent))
for some reason.