Updated Documentation

This commit is contained in:
Konrad Neitzel
2024-05-09 21:04:14 +02:00
parent 230c30bf96
commit aa3e302a32
6 changed files with 273 additions and 4 deletions
+61
View File
@@ -0,0 +1,61 @@
# PMD
## Rules for PMD
In this project, we use PMD to ensure code quality and prevent common
mistakes. The rules for code analysis are defined in the pmd-ruleset.xml
file located in the main directory of the project.
This file contains specific rule sets tailored to the project requirements.
Each rule set within the pmd-ruleset.xml specifies what issues PMD should
look for and report in the code. The configuration includes various
categories such as error prevention, code style, optimization, and many
others.
A typical entry in this file looks like this:
```xml
<rule ref="category/java/bestpractices.xml/UnusedImports">
<priority>5</priority>
</rule>
```
In this example, the UnusedImports rule from the best practices category is
used to identify unused imports in the code. The priority of this rule is set
to 5, which indicates a low priority.
To address specific needs, we can also add custom rules or modify existing
ones. This allows us a flexible adjustment of the code analysis, which helps
to keep our code clean, efficient, and error-free.
For effective use of PMD, it is important that all team members understand
how the pmd-ruleset.xml is structured and how it contributes to improving
code quality. Therefore, every developer should be familiar with this
configuration file and know how to make changes to it.
## Stop build process on rule violations
In the Maven project, we can control whether the build process should be
aborted upon violations of specified rules by setting specific goals (Goals)
of the Maven PMD or Checkstyle plugins. This function is particularly useful
for ensuring code quality and detecting errors early in the development
process.
Depending on the project configuration, these goals can be selected for PMD:
- **pmd**: Violations do not cause the build process to be aborted
- **check**: Stricter verification and the build process is aborted upon
violations.
To enable configuration directly in the properties, the pmd.target property is used:
```xml
<pmd.target>pmd</pmd.target>
```
## Result of Analysis
The result can be found inside target/pmd.xml.
Formatting the file will make it much easier to read.
+73
View File
@@ -0,0 +1,73 @@
# SpotBugs
## Using "null" Annotations
In Java projects, it's important to avoid potential NullPointerExceptions
that can arise from improper handling of null values. JetBrains' Null
Annotations offer a practical solution to detect such issues early in the
code and receive corresponding warnings through tools like SpotBugs.
### Dependency
The necessary annotations are included in the JetBrains library:
```xml
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>${jetbrains.annotations.version}</version>
<scope>compile</scope>
</dependency>
```
### Using the Annotations in Code
Once the library is available in your project, you can use the **@Nullable**
and **@NotNull** annotations to indicate whether a method or variable can
accept null values.
#### @NotNull
The @NotNull annotation is used to specify that a parameter, local variable,
field, or method should never accept a null value. This is especially
important in methods that cannot process null values, as it could lead to a
NullPointerException. By using this annotation, your development environment
or a static code analysis tool like SpotBugs or IntelliJ IDEA's integrated
inspector can warn you if there's a potential violation of this condition.
**Examples of using @NotNull:**
- Method parameters: Ensures that passed arguments are not null.
- Method return types: Ensures that the method does not return a null value.
- Fields: Prevents class fields from being set to null.
#### @Nullable
The @Nullable annotation is used to indicate that a variable, parameter,
field, or method can return or accept null. This annotation is useful to
clarify that methods and variables are safe for handling null values. If a
variable or method is marked as @Nullable, developers should perform
appropriate null checks to avoid NullPointerExceptions.
**Examples of using @Nullable:**
- Method parameters: Explicitly allows passed arguments to be null.
- Method return types: Indicates that the method may return null, prompting the caller to check.
- Fields: Allows class fields to be set to null.
### General Guidelines
**Consistent Use**: It's important to use these annotations consistently
throughout the code to maximize their effectiveness. Inconsistencies can lead
to misinterpretations and errors.
**Integration with Tools**: Use development tools and static code analysis
tools that can recognize and respect these annotations to enhance your code's
safety.
**Documentation and Team Communication**: Ensure that all team members
understand the importance and correct use of these annotations. This improves
code quality and prevents errors in collaboration.
By thoughtfully using @NotNull and @Nullable, you can significantly enhance
the robustness of your code and ensure that your applications are less
susceptible to runtime errors caused by unexpected null values.
## Result of Analysis
The result can be found inside target/spotbugsXml.xml.
Formatting the file will make it much easier to read.
+2 -2
View File
@@ -8,8 +8,8 @@
- Checking of Updates
## [Static Code Analysis](StaticCodeAnalysis.md)
- PMD
- FindBugs
- [PMD](PMD.md)
- [SpotBugs](SpotBugs.md)
## Other Topics
- Lombok