gianluca.aguzzi@unibo.it
angelo.filaseta@unibo.it
Questo materiale è ampiamente basato su quello realizzato dai Prof. Mirko Viroli e Roberto Casadei, che ringrazio.
Ogni errore riscontratovi è esclusiva responsabilità degli autori di questo documento.
// MIGLIORABILE
void m() {
// indentazione di 8 caratteri, da evitare, in quanto i sorgenti crescono facilmente orizzontalmente
for(int i : new int[] {1, 2, 3}) {
if(i % 2 == 0){
System.out.println(i);
}
}
System.out.println("Questa è una stringa veramente lunga che potrebbe portare ad infrangere la regola")
}
// OK
void m() {
for(int i : new int[] {1, 2, 3}) {
if(i % 2 == 0){
System.out.println(i);
}
}
System.out.println("Questa è una stringa veramente lunga " +
"che potrebbe portare ad infrangere la regola")
}
// ...
su una linea/* ... */
su più linee per commentare sezioni/** ... */
su più linee per commenti che generano documentazione/**
* (Commento di documentazione)
* Questa classe modella un generico dispositivo elettronico.
*/
class Device {
/*
* Campi di istanza per modellare lo stato del dispositivo.
*/
boolean on;
/*
* Metodi per accensione e spegnimento.
*/
void switchOn() {
if(!this.on){
// Se non acceso, allora accendiamo
this.on = true;
}
}
void switchOff() { ... }
}
// MIGLIORABILE
int a, b, c;
b = c = 5;
if(a > b) { System.out.println("a = " + a); } else { System.out.println("b = " + b); }
// OK
int a = 0;
int b = 5;
int c = 5;
if(a > b) {
System.out.println("a = " + a);
} else {
System.out.println("b = " + b);
}
// MIGLIORABILE
void m()
{
if(this.disabled ? true : this.unavailable ? true : this.urgent) return;
}
void f()
{
// ...
}
// OK
void m() {
if(this.disabled ? true : (this.unavailable ? true : this.urgent)) {
return;
}
}
void f() {
// ...
}
CamelCasing
camelCasing
_
// MIGLIORABILE
package IT.UNIBO.some_package;
class some_class {
static final int someConstant = 100;
void Some_Method() { /* ... */ }
}
// OK
package it.unibo.somepackage;
class SomeClass {
static final int SOME_CONSTANT = 100;
void someMethod() { /* ... */ }
}
// MIGLIORABILE
class SomeClass {
void someMethod() { /* ... */ }
private static final int SOME_CONSTANT1 = 100;
public static final int SOME_CONSTANT2 = 100;
private int someField;
}
// OK
class SomeClass {
// Campi statici
public static final int SOME_CONSTANT1 = 100;
private static final int SOME_CONSTANT2 = 100;
// Campi d'istanza
private int someField;
// Metodi d'istanza
void someMethod() { /* ... */ }
}
Point3D
/**
* Point3D is an example showcasing some OO functionality
* in Java, with a focus on formatting.
* All properties have default (package) access.
*/
public class Point3D {
/*
* A Point3D object is made of three doubles,
* and has some manipulation functions.
*/
// A constant ZERO point
static final Point3D ZERO = new Point3D(0, 0, 0);
double x; // x coordinate
double y; // y coordinate
double z; // z coordinate
/* A standard constructor initializing fields */
Point3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/* A simple function that
* extracts useful info from a point
*/
double getSquareModule() {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/* The following three are called selector methods */
double getX() {
return this.x;
}
double getY() {
return this.y;
}
double getZ() {
return this.z;
}
/* An example of a method changing the object state */
void translate(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
}
/* We also provide an example static method */
static Point3D max(Point3D[] ps) {
Point3D max = Point3D.ZERO; // Inizializing max
for (Point3D elem : ps) { // Iterating over all input points
if (elem.getSquareModule() > max.getSquareModule()) {
max = elem; // Update max if needed..
}
}
return max; // Return max
}
}
void
e accetta un valore che modifica una proprietà dell’oggettoLamp
, getIntensity
e isSwitchedOn
sono getter, setIntensity
è un setterXYZ
di tipo T
T getXYZ(){...}
boolean getXYZ(){...}
o anche boolean isXYZ() {...}
void setXYZ() {...}
public class Lamp {
...
// Setter per proprieta' Intensity di tipo double
public void setIntensity(double value) {
...
}
// Getter per proprieta' Intensity di tipo double
public double getIntensity() {
...
}
// Getter per proprieta' SwitchedOn di tipo boolean
public boolean isSwitchedOn() {
...
}
}
public class LampUtilities {
/* Costruisco una fila di lampadine */
public static Lamp[] buildLampRow(final int size) {
final Lamp[] array = new Lamp[size];
for (int index = 0; index < size; index++) {
array[index] = new Lamp();
}
return array;
}
/* Accendo/spengo una fila di lampadine */
public static void switchAll(final boolean switchedOn, final Lamp[] array) {
for (Lamp l : array) {
if (switchedOn) {
l.switchOn();
} else {
l.switchOff();
}
}
}
}
Checkstyle si occupa di trovare errori di stile:
checkstyle.xml
(un esempio)<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="charset" value="UTF-8"/>
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf"/>
</module>
<module name="LineLength">
<property name="max" value="85"/>
</module>
<!-- ... -->
</module>
checkstyle
per analizzare codice rispetto allo stile configuratocheckstyle
, da dichiarare in build.gradle.kts
:plugins {
java
checkstyle
}
repositories {
mavenCentral()
}
<root>
└── config
└── checkstyle
└── checkstyle.xml
└── suppressions.xml
check
(che include checkstyleMain
e checkstyleTest
)build/reports/checkstyle
$ ./gradlew check
> Task :checkstyleMain
[ant:checkstyle] [ERROR] /<PATH-TO-PROJ>/src/main/java/.../File.java:4:
Line is longer than 85 characters (found 102). [LineLength]
> Task :checkstyleMain FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':checkstyleMain'.
> A failure occurred while executing org.gradle.api.plugins.quality.internal.CheckstyleAction
> Checkstyle rule violations were found. See the report at: file:///<PATH-TO-PROJ>/build/reports/checkstyle/main.html
Checkstyle files with violations: 1
Checkstyle violations by severity: [error:1]