You are viewing a single comment's thread. Return to all comments →
are you sure that we need the "else if" statement here?
Thank you for communicating respectfully with me as an adult. Yes it is needed.
If your question is in reference to the need for the logical check inside of it (else-if vs else), the reason is that you need to convert 12AM (midnight) to 00; we already know that it's AM because it failed the if(PM) check in the previous line, so we just need to check if the hour is 12.
If your question is in reference to the reason/benefit of using an else-if over if, it's because each input is binary in that it will always be either AM or PM. Having if(PM) = true means that the logical flow completely ignores the else-if code when if(PM) = true, thus eliminating the need to run another redundant logic check if(AM) at every single execution. It essentially removes two unneeded logical comparisons from each execution (compared to original poster's logic) of the code; while it won't make a big difference here, it certainly makes a difference in the real world.
if(PM) = true
Thanks, makes sense now! I was overlooking the fact that 12AM is 00 hours. Thanks for the explanation.
In the real world, if performance isn't affected drastically, readability (even at the expense of redundancy) is always preferred. Its especially helpful with legacy applications. So in this case, I would say that the presence of the additional if is actually a good practice.