danilo.pianini@unibo.it
gianluca.aguzzi@unibo.it
angelo.filaseta@unibo.it
Compiled on: 2025-10-15 — versione stampabile
// 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, quindi la spezziamo");
}
// 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"
+ " quindi la spezziamo"
);
}
// ...
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 + ", c = " + c); } else { System.out.println("b = " + b); }
// OK
int a = 0;
int b = 5;
if(a > b) {
int c = 5;
System.out.println("a = " + a + ", c = " + c);
} else {
System.out.println("b = " + b);
}
// MIGLIORABILE
void m() // Allman style, da evitare in Java (si usa invece in C#)
{
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() {
// ...
}
PascalCase
camelCase
_
SCREAMING_SNAKE_CASE
// 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() {
...
}
}
danilo.pianini@unibo.it
gianluca.aguzzi@unibo.it
angelo.filaseta@unibo.it
Compiled on: 2025-10-15 — versione stampabile