Operator Precedence and Parantheses

It occurred to me last night that I never really remember the order of operator precedence in any language. For example: 2+3*4 will work out as 14 rather than 20 because most languages will evaluate the multiplication before the addition. If we wanted it to work the other way round we’d add some parentheses like this: (2+3)*5

What I’ve noticed is that even if I want the precedence to work as described I’ll add unnecessary brackets like this: 2+(3*4)

I’m using simple examples here so it might seem a little more ridiculous but for more complex examples I think it increases readability. When I scan through the code later, I want to see right away what is happening and in what order. I can’t decide if this is just a side-effect though, and the real cause is that I’m too lazy to remember the precedence.

Leave a Reply