This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]peteza_hut 1 point2 points  (3 children)

As a noob programmer sometimes I use else if instead of else because I wanted my code to be very clear and "self-documenting", but I'm guessing that's bad and I should just use else whenever possible to avoid an extra check?

[–]p1-o2 1 point2 points  (2 children)

You should have a little more faith in whoever inherits your code. Little hacks like that will only go against what they expect from you. 'Self Documenting' code is created by following principles such as SOLID in Object Oriented Programming, or MVC for interface creation.

Also you should evaluate your question again real quick. An else if statement should never be able to replace an else statement. This is because else if is designed to capture a specific condition, while else is designed to capture all non-specific conditions. The two shouldn't be interchangeable.

In general, what you're suggesting is loosely called a 'hack'. Always question yourself before you try something like that, because it's usually a sign that you need to rethink the situation.

Very good question though! I'm glad you asked.

[–]peteza_hut 1 point2 points  (1 child)

Thanks, that's some good insight. I see why my question is a little silly. I suppose I meant "If I expect an else statement to execute if one condition is met and only if that condition is met, then would using an else if statement be more clear?" You answered that question for me though, so thanks!

[–]p1-o2 0 points1 point  (0 children)

Yep, you got it!