Does the last statement on a switch need to 'break'?

4

I was watching some tutorials on YouTube and noticed that some programmers leave the last statement in a block switch without the word break . For example:

switch(frutas){
   case "abacaxi": abacaxi++; break;
   case "morango": morango++; break;
   case "pera": pera++; // sem 'break' aqui...
}

I found it interesting, at first I thought it was just Programming style . I always use break even in the last statement, but after seeing the videos I realized that there is no need since it is the last one in the block. As far as I know, break is (correct me if it is wrong) just to interrupt an instruction.

I would like if this practice can lead to some problem.

    
asked by anonymous 02.01.2015 / 01:42

1 answer

4

Directly there is no problem other than confusion during maintenance as bfavaretto put it in his comment.

I do not have much to say about except that it's stylistic coding option. By the way, it's pretty bad if you decide to follow these folks.

The problem of these tutorials and posted information where not given due importance to the quality of the content, as it happens a lot on the internet and on many occasions right here, is that in an attempt to help people with the best of intentions, people who make and defend information without a wide context is doing a disservice to the training of programmers who seek to benefit from this information.

What you can do is not the same as what you should do. Some programmers only care whether the program works or not. Others worry whether it is right or not, whether it will continue working or not, whether it will provide maintenance facilities or not.

Keep doing what you always did, even if it works the way you did. And whenever you have questions, ask in a reliable location. If all goes well, if I said some bullshit, someone will post something better or will correct me. Makes the process more reliable.

Just to be precise, break interrupts an instruction block.

    
02.01.2015 / 05:20