Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Optimizers are pretty good at storing multiple variables in the same stack slot when their usages (i.e. "live ranges") are disjoint.

Exactly. Also the true lifetime of the variable might be way longer than what the programmer thinks due to compiler reordering the code to help CPU with dependency chain latency. (I've also seen plenty of cases where CPU out-of-order engine apparently completely fails, but (Intel) compiler saves the day.)

> int x = value;

> if (condition) x = another;

> is just better written as:

> const int x = condition ? value : another;

> It reads better, and it enables x to be const, which is always better.

While const sure is safer, the generated code should be same in both cases.

In this case I agree with you. But not if the ternary operator has any more complexity, because it quickly becomes hard to read. In my experience complicated ternary operators are rather bug prone, especially with large teams.

That said, I guess you agree with me your benchmark being "reads better". :-)



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

Search: