

Ask HN: Settle this simple elegant vs. readable disagreement - PlzSnow

Consider these 2 statements, which identically detect an iPod or iPhone:<p>if (&#x2F;ip(hone|od)&#x2F;i.test(navigator.userAgent))<p>if (&#x2F;iphone|ipod&#x2F;i.test(navigator.userAgent))<p>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.<p>My colleague argues for the purity and elegance of the first solution. He thinks purity&#x2F;elegance is most important.<p>Anyone have an opinion on this?
======
pjungwir
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!

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

------
mrcold
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.

------
brudgers
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.

------
auganov
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.

