Clean Code

Michele Flores e Ricardo Machado

Eduardo Pretz

Engenharia de Software I

Discipline:

The Software engineering is an area of computing focused in specification, development and maintenance of software systems, with application of technologies and project management practices and other disciplines, aimed at organization, productivity and quality.

More

work?

Waste of time?

Clean Code?

It serves no purpose!

My code is perfect!

This doesn't speed up the process!

Software engineering?

Clean Code

  • Emergence
  • Meaningful Names
  • Functions
  • Classes
  • Comments
  • Formatting
  • Error Handling
  • Refactorin

Emergence

  • Runs all the tests
  • Contains no duplication
  • Expresses the intent of the programmer
  • Minimizes the number of classes and methods

Meaningful Names

  • Intention-Revealing Names
  • Avoid Disinformation
  • Pronunciable Names
  • Searchable Names
  • Avoid Mental Mapping
  • Don't be Cute

Bad!

Good!

Intention-Revealing Names

public List<int[]> getThem() {

 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;

 }

Bad!

Good!

Avoid Disinformation

int a = l;

if ( O == l )

 a = 01;

else

 l = 01;
int number = value;

if ( ORIGINAL_VALUE == value )

 number = 01;

else

 value = 01;

Bad!

Good!

Pronunciable Names

class DtaRcrd102 {

private Date genymdhms; 

private Date modymdhms;

private final String pszqint = "102";

/* ... */

};
class Customer {

private Date generationTimestamp; 

private Date modificationTimestamp;;

private final String recordId = "102";

/* ... */

};

Bad!

Good!

Searchable Names






for (int j=0; j<34; j++) {
s += (t[j]*4)/5;
}


int realDaysPerIdealDay = 4;
const int WORK_DAYS_PER_WEEK = 5;
int sum = 0;

for (int j=0; j < NUMBER_OF_TASKS; j++) {
int realTaskDays = taskEstimate[j] * realDaysPerIdealDay;
int realTaskWeeks = (realdays / WORK_DAYS_PER_WEEK);
sum += realTaskWeeks;
}

Bad!

Good!

Avoid Mental Mapping

List<Vehicle> v = vehicleService.list();
m.addAttribute("vehicles", v);

if(me == null){
	me = "";
}
		
if(r == null){
	r = RequestStatus.SUCCESS;
}
		
m.addAttribute("r", r);
m.addAttribute("me", me);
return "vehicle/list";
List<Vehicle> vehicles = vehicleService.list();
m.addAttribute("vehicles", vehicles);

if(message == null){
	message = "";
}
		
if(requestStatus == null){
	requestStatus = RequestStatus.SUCCESS;
}
		
m.addAttribute("requestStatus", requestStatus);
m.addAttribute("message", message);
return "vehicle/list";

Bad!

Good!

Don't be Cute





   HolyHandGrenade
 
   whack()

   eatMyShorts()




   DeleteItems

   kill()

   abort()

Functions

  • Do One Thing
  • Small
  • Flag Arguments
  • Function Arguments
  • Command Query Separation

Classes

  • Organization
    • Encapsulation
  • Should Be Small
    • Single Responsability
    • Cohesion
  • Organizing for Change
    • Isolating for Change

Comments

  • Good Comments
    • ​​Legal Comments
    • Informative
    • Clarification
    • Warning of Consequences
  • Bad Comments
    • Redundant Comments
    • Journal Comments
    • Noise Comments
    • Scary Noise

Formatting

  • Communication
  • Clean

Bad!

public class Assert {
static public void assertTrue(String message, boolean condition)
{
if (!condition)
fail(message);
}
static public void assertTrue(boolean condition) {assertTrue(null, condition);}

static public void assertFalse
(
String message,
boolean condition
) {assertTrue(message, !condition);
}

static public void assertFalse(boolean
condition) 

{

assertFalse(null, condition);

}
public class Assert {
static public void assertTrue(String message, boolean condition) {
if (!condition)
fail(message);
}

static public void assertTrue(boolean condition) {
assertTrue(null, condition);
}

static public void assertFalse(String message, boolean condition) {
assertTrue(message, !condition);
}

static public void assertFalse(boolean condition) {
assertFalse(null, condition);
}

Good!

Error Handling

  • Use Exceptions Rather Than Return Codes
  • Write Your Try-Catch-FinallyStatement First
  • Use Unchecked Exceptions
  • Provide Context with Exceptions
  • Don't Return Null
  • Don't Pass null

Error Handling

Error Handling

Error Handling

Refactorin

Make it Work, then make it right

Thanks!

MARTIN, Robert C. Clean Code, a Handbook of Agile Software Craftsmanship. Pearson Education, 2009.


REFACTORING. Available in <https://sourcemaking.com/refactoring>. Access November 18th, 2015.

Bibliography

Clean Code

By Michele Correia Flores

Clean Code

This work is about Clean Code. What is and how we can practice this. Bibliography: MARTIN, Robert C. Clean Code, a Handbook of Agile Software Craftsmanship. Pearson Education, 2009. | REFACTORING. Available in . Access November 18th, 2015.

  • 505