You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi Prof,
I understand from the style guide that switch statements should look like this.
String test = "abc";
switch (test) {
case "abc":
# doStuff
break;
case "bcd":
# doOtherStuff
break;
default:
throw new Exception();
}
May I know what the style should be when the switch statement uses scopes in their cases.
Should it be like this?
String test = "abc";
switch (test) {
case "abc": {
# doStuff
break;
}
case "bcd": {
# doOtherStuff
break;
}
default: {
throw new Exception();
}
}
The rationale for having curly brackets in switch statements is to provide a scope for the case's variables.
Consider this scenario:
It will fail to compile because the variable foo is being declared twice.
switch (test) {
case "abc":
String foo = "bar";
break;
case "bcd":
String foo = "baz";
break;
}
The text was updated successfully, but these errors were encountered:
May I know what the style should be when the switch statement uses scopes in their cases.
Should it be like this?
String test = "abc";
switch (test) {
case "abc": {
# doStuff
break;
}
case "bcd": {
# doOtherStuff
break;
}
default: {
throw new Exception();
}
}
Yes, I supposed so, if one follows the current coding standard. Yes, it does look ugly. It's rare to use braces within a switch statement. In the rare case braces are used, one can deviate from the standard with an explanation for the deviation.
Hi Prof,
I understand from the style guide that switch statements should look like this.
May I know what the style should be when the switch statement uses scopes in their cases.
Should it be like this?
The rationale for having curly brackets in switch statements is to provide a scope for the case's variables.
Consider this scenario:
It will fail to compile because the variable foo is being declared twice.
The text was updated successfully, but these errors were encountered: