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

BurningOrca

Veteran
Veteran
Joined
Apr 14, 2019
Messages
214
Reaction score
187
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,604
Reaction score
1,960
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,268
Reaction score
1,422
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,513
Reaction score
3,204
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'.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,085
Members
137,584
Latest member
Faustus2501
Top