Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: Settle this simple elegant vs. readable disagreement
2 points by PlzSnow on March 24, 2015 | hide | past | favorite | 5 comments
Consider these 2 statements, which identically detect an iPod or iPhone:

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?




In my opinion you should usually not code for DRY when the repetition is "accidental," only when it expresses a principle from your domain (your "business logic"). Sharing "ip" between "iphone" and "ipod" seems like a perfect example of accidentally DRY. I think your colleague is trying too hard. Code should be boring!


I like the second a lot more. It's just easier to read without knowing anything else.


Art is subjective. Structure is forever.

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.


I recommend encouraging your colleague to pursue a career as a Lisp hacker. Their inclination toward parentheses makes them look like a natural.

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.


If we are to be very picky I'd say

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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: