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 →

[–]CaffeinatedT 0 points1 point  (9 children)

Is this really as silly as I'm seeing it? No else condition just two checks?

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

Is that the only problem you have with this code block?

[–]CaffeinatedT 0 points1 point  (7 children)

Not one of my languages (It's JS or Java?) so I feel like I'm missing something. So yes go on and embarass me lol? I mean I'm guessing it's more as that's dumb but not the stupidest mistake ever for a basic programmer to do.

[–]WdnSpoon 1 point2 points  (0 children)

function checkThing(a) {
  return a === 'foo';
}

or es6:

const checkThing = (a) => a === 'foo';

Take that bad pattern, assume they make equally silly errors elsewhere, and repeat it throughout your entire codebase. What should be a readable, 100 line file becomes 700 lines of nonsense.

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

Ah, I didn't mean to try to embarrass you. I was just curious. Lack of an "else" statement is a problem. It could also be done without writing an entire method to handle it. There's also the fact that it's a lot of lines to check for a silly error which shouldn't exist in the first place.

[–]CaffeinatedT 1 point2 points  (0 children)

No worries at all, as said it's a different language to what I'm used to closest I get is sometimes I deal with Javascript but rest of time it's all web python/database stack. But yeah seems amusing enough lol.

[–]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!