RMMV Why do so many people add a break after return in switch statements

BurningOrca

Veteran
Veteran
Joined
Apr 14, 2019
Messages
218
Reaction score
195
First Language
German
Primarily Uses
RMMV
I've seen something like this example so often now, that I just need to ask:

JavaScript:
MyPlugin.doConvertNumberToSomething = function(value)
{
    switch(MyPlugin.Param.ConversionOption)
    {
        case "Option 1":
            return MyPlugin.convertOption1(value);
            break;
        case "Option 2":
            return MyPlugin.convertOption2(value);
            break;
        // And so on
    }
}

Is this a convention to add a break statement to any case statement, so that fallthroughs are really avoided?
I ask, because in this case they are completely unnecessary, as the return statement will already break the control follow of the whole function and not just the switch statement by returning to the calling function.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,616
Reaction score
1,978
First Language
English
Primarily Uses
RMMV
I think it's just force of habit, really. Convention is that every case end with a break to avoid fallthrough, so people still do it even if the case has a return in it.
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,275
Reaction score
1,439
First Language
English
Primarily Uses
RMXP
The fall through language design feature is well known for causing errors.
There is a reason why modern languages try to guard against it.

In Swift you must explicitly state your fallthrough: https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID130
Kotlin went a step further and did not feature a fallthrough method: https://kotlinlang.org/docs/reference/control-flow.html#when-expression

As for Javascript.
Yes, always add break statements (or return statements I guess) to all your case statements. You can then remove the break statements where you want the fallthrough. This is simply a safer way of coding.

Edit: In a show of my reading skills I didn't pay attention to there being dead code. It's just a mistake, it happens. If it was caught the break statement would be removed as return breaks the flow just as well.

*hugs*
 
Last edited:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,297
First Language
Binary
Primarily Uses
RMMZ
As you noted, the break statement in your example will never get called. its a useless addition.
1598619603654.png

Notice that my syntax highlighting has shaded/opaqued the break statements. When you hover over them it tells you 'unreachable code detected'.
 

Latest Threads

Latest Posts

Latest Profile Posts

Making Houses and Outside on the same map with RPG Maker MV!
Do people actually use buff/debuff function?
Ami


Imagine you (as Miner) Sleep in the Cave,the other side is a Bunch of Golds,how d'you feel?
finally got footage of my main character running around his corrupted street...
I finally got the ships and water to flow like I wanted to~ the canals are a success. Now to loop the animation for the kites. So many kites.

Forum statistics

Threads
107,702
Messages
1,031,532
Members
139,835
Latest member
dangkykubetim
Top