Software Anarchist
twitter: @avernois
Continuous Learner
twitter: @ludopradel
Clean code is not about beautifullness,
it's about goodness.Rebecca Wirfs-Brock
public List<int[]> getFlg() {
List<int[]> list1 = new ArrayList<int[]>();
for (int[] x : theList ) {
if (x[0] == 4)
list1.add(x);
}
return list1;
}
public List<Cell> getFlaggedCells() {
List<Cell> flaggedCells = new ArrayList<Cell>();
for (Cell cell : gameBoard ) {
if (cell.isFlagged())
flaggedCells.add(cell);
}
return flaggedCells;
}
public RGBColor readCurrentColor(BlinkLed led) {
device.sendCommand(new ReadColorRequest(led));
byte[] response = device.readResponse();
int red = response[2] >= 0 ? response[2] : response[2] + 256;
int green = response[3] >= 0 ? response[3] : response[3] + 256;
int blue = response[4] >= 0 ? response[4] : response[4] + 256;
return new RGBColor(red, green, blue);
}
public RGBColor readCurrentColor(BlinkLed led) {
device.sendCommand(new ReadColorRequest(led));
byte[] response = device.readResponse();
return extractColor(response);
}
private RGBColor extractColor(byte[] response) {
int red = convertToPositiveInt(response[2]);
int green = convertToPositiveInt(response[3]);
int blue = convertToPositiveInt(response[4]);
return new RGBColor(red, green, blue);
}
private int convertToPositiveInt(byte byt) {
return byt >= 0 ? byt : byt + 256;
}
/*
* A comment to please checkstyle
*/
/*
* Set the port
*
* @params port
*/
public void setPort(Port port) {this.port=port}
...
} /* end for */
dao.flush();
default :
break;
} /* end switch */
} /* end if */
} /* end if */
} catch ...
Comments are always failureUncle Bob
Don't comment bad code. Rewrite it.Brian W. Kernighan, P.J. Plaugher
a.getB().getC().doThings(); // this is bad
a.doThings();
public class Client {
private Service service;
public Client() {
this.service = new ServiceExample();
}
public String greet() {
return "Hello " + service.getName();
}
}
public class Client {
private Service service;
public Client(Service service) {
this.service = service;
}
public String greet() {
return "Hello " + service.getName();
}
}
- is tested !
- has no duplication
- reveals its intention
- easy to read
B. Stroustrup, R. Jeffries, K. Beck, M. Feathers, W. Cunningham, ...
Continuous attention to technical excellence and good design enhances agility.Agile Manifesto
For questions, we'll be there the whole day,
just come and talk to us.
Slides will be available here very soon:
http://avernois.github.io/prez-clean_code/