Clean Code

Antoine Vernois

Software Anarchist

twitter: @avernois

Ludovic Pradel

Continuous Learner

twitter: @ludopradel

Why code in an agile conf?

Why clean code?

What is a clean code ?

Clean code is not about beautifullness,
it's about goodness.
Rebecca Wirfs-Brock

measuring cleanness?

What can we do?

make it readable

unreadable


public List<int[]> getFlg() {
  List<int[]> list1 = new ArrayList<int[]>();
  for (int[] x : theList ) {
    if (x[0] == 4)
      list1.add(x);
  }
	
  return list1;
}
					

much better!


public List<Cell> getFlaggedCells() {
  List<Cell> flaggedCells = new ArrayList<Cell>();
  for (Cell cell : gameBoard ) {
    if (cell.isFlagged())
      flaggedCells.add(cell);
  }
		
  return flaggedCells;
}	
					

unreadable


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);
}
				

extract code

much better!


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;
}


Comments

false good idea

comments in real life


/*
* 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 failure
Uncle Bob
Don't comment bad code. Rewrite it.
Brian W. Kernighan, P.J. Plaugher


Comments : the exception

explain the Why!




Comments

Tests tells me What

Code tells me How

Comment, if needed, tells me why

Tests

They give you confidence to change the code.

A test should tell you a story

one test, one assert

tests document your code

Tests are first class citizens!

by the way

Test everything!

coupling

this is bad


a.getB().getC().doThings(); // this is bad
				
instead

a.doThings();
					

Demeter's Law


public class Client {
  private Service service;

  public Client() {
    this.service = new ServiceExample();
  }

  public String greet() {
    return "Hello " + service.getName();
  }
}    
                

bad

Dependency Injection


public class Client {
  private Service service;

  public Client(Service service) {
    this.service = service;
  }

  public String greet() {
    return "Hello " + service.getName();
  }
}    
                

better

Dependency injection frameworks

they can be dangerous

Spring, I'm looking at you!

coupling with external libraries

or technical stuff

like frameworks

don't.

really. Please.

don't let them go inside your business

use abstractions

or facades

try wishfull design

and you can do that for you whole application!

maybe, you should :)

some smells

  • Singleton
  • Tight coupling
  • Untestable
  • Premature Optimisation
  • Indescriptive naming
  • Duplication

lots of other good stuff

  • SOLID principles are good for you
  • never ever return null
  • do not check exceptions
  • do not always think inheritance, think composition
  • do not think if/switch, think polymorphism
  • avoid singletons
  • ...

a clean code

  • 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

Read list


Thanks!

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/

Credits

Plastics Injection Factory by Renee Prisble
Billard 03 by Ghislain Mary
A sonar image by Tuukritööde OÜ (Muinsuskaitseamet) via Wikimedia Commons
LED by Nao. Fujita
Chained by Jamie Bradway
Chains by John Kenedy
Chains by Rubèn Rico
Wistful Wishes by Julie Jablonsky
143 by Tobias Myrstrand Leander
Refinery by Glenn Euloth
Sweets by Lyrical Lemongrass
Gotta clean myself by Lucia Sanchez
the big net by popofatticus
José Vasconcelos Library by Rossa Menkman
One by Lazy Librarian