Hacker News new | past | comments | ask | show | jobs | submit login

Even when I have no issues with the precedence order, I often add parens anyway because it helps with mental grouping at a glance, instead of having to scan and mentally parse the line.



A wise practice.

As a young egotist I would often omit parens in complicated C expressions. I did this intentionally and in a very self-satisfied way - writing multi-line conditionals and lining them up neatly without parens with a metaphorical flourish of my pen.

Then one day, chasing a hard-to-find bug, I realised it had happened because I'd mixed up the precedence of && and || in a long conditional. I was an idiot. Since then I've made a point of reminding myself that I know nothing and that there's nothing to be gained from pretending I do, and putting parens in everywhere.

Sometimes, even now, I get those grandiose moments when I think the code I'm writing cannot possibly go wrong. Those are the moments that call for a bit of fresh air and an extra unit test or two.


That's a great observation. Passing on that expertise is what wizened veterans can do to move our capabilities along and not let learning go to waste. I've heard that a big reason for sometimes struggling software engineering quality in countries and companies is places where the only way to grow is to become a manager - you need people to stay in technical paths so they can pass on that learned knowledge.


I heard an aphorism once I've tried to live by since:

It is twice as hard to debug code as it is to write it. If you write code as clever as you can make it, you will never be able to debug it.


Ironically enough, given the article, this is attributed to Brian Kernighan.


I'll try to remember the attribution as well as the aphorism in future, thanks :-)



Exactly this. I add parenthesis not for computer but for myself. Mostly because it is easier to comprehend. I think the reason for this is that I learned precedence order something like 25 years ago in fifth grade maths and still do it to this day.




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

Search: