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

median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid];

In my opinion the ternary operator is misused here when an if/else would have been clearer.



Well, you omitted the all important const qualifier which would be a decent reason to use the ternary operator.


I actually like using the ternary operator in this sort of assignment, so I only need to type the variable name once instead of 3 times. The only real problem in your example is that everything is in one line. It woulf be better to indent that and to add some extra parenthesis to make the precedence clearer.


Ah yeah, that is one beefy line of code. I can agree with that.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: