public class SingleBreakOrContinueCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
This check restricts the number of break and continue statements inside cycle body (only one is allowed).
Restricting the number of break and continue statements in a loop is done in the interest of good structured programming.
One break and continue statement is acceptable in a loop, since it facilitates optimal coding. If there is more than one, the code should be refactored to increase readability.
For example: (http://nemo.sonarqube.org/coding_rules#languages=java|q=one%20break)
for (int i = 1; i <= 10; i++)
{ // violation - 2 continue - one might be tempted to add some logic in between
if (i % 2 == 0)
{
continue;
}
if (i % 3 == 0)
{
continue;
}
System.out.println("i = " + i);
}
Please note that Switch statements and inner loops are ignored in this check. This Rule only validate loop structure with depth 0.
For example:
for (int i = 1; i <= 10; i++)// OK - Outer loop
{
while (true) // violation - Inner loop: 1 continue and 1 break
{
if (true)
{
continue;
}
if (true)
{
break;
}
System.out.println("violation - 1 continue and 1 break");
}
}
while (true) // OK - Switch block
{
final char chr = value.charAt(i);
switch (chr) {
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '\"':
sb.append(""");
break;
case '&':
sb.append(chr);
break;
default:
sb.append(chr);
break;
}
}
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
Warning message key.
|
| Constructor and Description |
|---|
SingleBreakOrContinueCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY
public int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.