Method run
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
@Override
public void run() {
while (isRunning) {
if (!isProcessing) {
if (!inputQueue.isEmpty()) {
- Read upRead up
- Create a ticketCreate a ticket
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Merge this if statement with the enclosing one. Open
if (!inputQueue.isEmpty()) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if (file != null) { if (file.isFile() || file.isDirectory()) { /* ... */ } }
Compliant Solution
if (file != null && isFileOrDirectory(file)) { /* ... */ } private static boolean isFileOrDirectory(File file) { return file.isFile() || file.isDirectory(); }
Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here. Open
} catch (InterruptedException e) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
InterruptedExceptions
should never be ignored in the code, and simply logging the exception counts in this case as "ignoring". The
throwing of the InterruptedException
clears the interrupted state of the Thread, so if the exception is not handled properly the fact
that the thread was interrupted will be lost. Instead, InterruptedExceptions
should either be rethrown - immediately or after cleaning up
the method's state - or the thread should be re-interrupted by calling Thread.interrupt()
even if this is supposed to be a
single-threaded application. Any other course of action risks delaying thread shutdown and loses the information that the thread was interrupted -
probably without finishing its task.
Similarly, the ThreadDeath
exception should also be propagated. According to its JavaDoc:
If
ThreadDeath
is caught by a method, it is important that it be rethrown so that the thread actually dies.
Noncompliant Code Example
public void run () { try { while (true) { // do stuff } }catch (InterruptedException e) { // Noncompliant; logging is not enough LOGGER.log(Level.WARN, "Interrupted!", e); } }
Compliant Solution
public void run () { try { while (true) { // do stuff } }catch (InterruptedException e) { LOGGER.log(Level.WARN, "Interrupted!", e); // Restore interrupted state... Thread.currentThread().interrupt(); } }
See
- MITRE, CWE-391 - Unchecked Error Condition
- Dealing with InterruptedException
At-clause should have a non-empty description. Open
* @param event
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the block tag is followed by description.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
At-clause should have a non-empty description. Open
* @param protocol
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the block tag is followed by description.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Unused @param tag for 'sessionId'. Open
* @param sessionId
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks the Javadoc of a method or constructor.
Violates parameters and type parametersfor which no param tags arepresent can be suppressed by defining propertyallowMissingParamTags
.
Violates methods which return non-void but for which no return tag ispresent can be suppressed by defining propertyallowMissingReturnTag
.
Violates exceptions which are declared to be thrown (by throws
in the methodsignature or by throw new
in the method body), but for which no throws tag ispresent by activation of property validateThrows
.Note that throw new
is not checked in the following places:
- Inside a try block (with catch). It is not possible to determine if the thrownexception can be caught by the catch block as there is no knowledge of theinheritance hierarchy, so the try block is ignored entirely. However, catchand finally blocks, as well as try blocks without catch, are still checked.
- Local classes, anonymous classes and lambda expressions. It is not known when thethrow statements inside such classes are going to be evaluated, so they are ignored.
ATTENTION: Checkstyle does not have information about hierarchy of exception typesso usage of base class is considered as separate exception type.As workaround you need to specify both types in javadoc (parent and exact type).
Javadoc is not required on a method that is tagged with the@Override
annotation. However underJava 5 it is not possible to mark a method required for aninterface (this was corrected under Java 6). HenceCheckstyle supports using the convention of using a single{@inheritDoc}
tag instead of all theother tags.
Note that only inheritable items will allow the{@inheritDoc}
tag to be used in placeof comments. Static methods at all visibilities, private non-staticmethods and constructors are not inheritable.
For example, if the following method isimplementing a method required by an interface, then theJavadoc could be done as:
<source><br>/** {@inheritDoc} */<br>public int checkReturnTag(final int aTagIndex,<br> JavadocTag[] aTags,<br> int aLineNo)<br> </source>This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
At-clause should have a non-empty description. Open
* @param analyzer
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the block tag is followed by description.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
First sentence of Javadoc is missing an ending period. Open
/**
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks thatJavadoc summary sentence does not contain phrases that are not recommended to use.Summaries that contain only the {@inheritDoc}
tag are skipped. Check alsoviolate Javadoc that does not contain first sentence.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
First sentence of Javadoc is missing an ending period. Open
/**
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks thatJavadoc summary sentence does not contain phrases that are not recommended to use.Summaries that contain only the {@inheritDoc}
tag are skipped. Check alsoviolate Javadoc that does not contain first sentence.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Wrong lexicographical order for 'lombok.extern.log4j.Log4j' import. Should be before 'org.springframework.stereotype.Component'. Open
import lombok.extern.log4j.Log4j;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Wrong lexicographical order for 'lombok.Setter' import. Should be before 'org.springframework.stereotype.Component'. Open
import lombok.Setter;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Summary javadoc is missing. Open
/**
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks thatJavadoc summary sentence does not contain phrases that are not recommended to use.Summaries that contain only the {@inheritDoc}
tag are skipped. Check alsoviolate Javadoc that does not contain first sentence.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
At-clause should have a non-empty description. Open
* @param analyzer
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the block tag is followed by description.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
At-clause should have a non-empty description. Open
* @param destinationCell
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the block tag is followed by description.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Extra separation in import group before 'in.ac.bits.protocolanalyzer.analyzer.event.EndAnalysisEvent' Open
import in.ac.bits.protocolanalyzer.analyzer.event.EndAnalysisEvent;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Wrong lexicographical order for 'in.ac.bits.protocolanalyzer.analyzer.event.PacketTypeDetectionEvent' import. Should be before 'org.springframework.stereotype.Component'. Open
import in.ac.bits.protocolanalyzer.analyzer.event.PacketTypeDetectionEvent;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
At-clause should have a non-empty description. Open
* @param sessionId
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the block tag is followed by description.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Wrong lexicographical order for 'com.google.common.eventbus.Subscribe' import. Should be before 'org.springframework.stereotype.Component'. Open
import com.google.common.eventbus.Subscribe;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Extra separation in import group before 'org.springframework.beans.factory.annotation.Autowired' Open
import org.springframework.beans.factory.annotation.Autowired;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Wrong lexicographical order for 'com.google.common.eventbus.EventBus' import. Should be before 'org.springframework.stereotype.Component'. Open
import com.google.common.eventbus.EventBus;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
At-clause should have a non-empty description. Open
* @param cellID
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the block tag is followed by description.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Wrong lexicographical order for 'lombok.Getter' import. Should be before 'org.springframework.stereotype.Component'. Open
import lombok.Getter;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
At-clause should have a non-empty description. Open
* @param packet
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the block tag is followed by description.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Extra separation in import group before 'com.google.common.eventbus.EventBus' Open
import com.google.common.eventbus.EventBus;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Wrong lexicographical order for 'in.ac.bits.protocolanalyzer.analyzer.event.EndAnalysisEvent' import. Should be before 'org.springframework.stereotype.Component'. Open
import in.ac.bits.protocolanalyzer.analyzer.event.EndAnalysisEvent;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
Wrong lexicographical order for 'in.ac.bits.protocolanalyzer.persistence.repository.AnalysisRepository' import. Should be before 'org.springframework.stereotype.Component'. Open
import in.ac.bits.protocolanalyzer.persistence.repository.AnalysisRepository;
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks that the groups of import declarations appear in the order specifiedby the user. If there is an import but its group is not specified in theconfiguration such an import should be placed at the end of the import list.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
First sentence of Javadoc is missing an ending period. Open
/**
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks thatJavadoc summary sentence does not contain phrases that are not recommended to use.Summaries that contain only the {@inheritDoc}
tag are skipped. Check alsoviolate Javadoc that does not contain first sentence.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.
These nested if statements could be combined Open
if (!inputQueue.isEmpty()) {
isProcessing = true;
process(inputQueue.poll());
}
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
CollapsibleIfStatements
Since: PMD 3.1
Priority: Medium
Categories: Style
Remediation Points: 50000
Sometimes two consecutive 'if' statements can be consolidated by separating their conditions with a boolean short-circuit operator.
Example:
void bar() {
if (x) { // original implementation
if (y) {
// do stuff
}
}
}
void bar() {
if (x && y) { // optimized implementation
// do stuff
}
}