2. What‘s the content?
// try to find a simple definition
What is Clean Code?
// try to convince them, so you don‘t have to force them
Why do I need Clean Code?
// show some simple rules
How to produce Clean Code?
8. What is Clean Code?
Ward Cunningham
“You know you are working
on clean code, when each routine
you read turns out to be
the pretty much what you
expected“
17. Robert C. Martin
(aka Uncle Bob)
“The Boy Scout Rule“
How to produce Clean Code?
// General
18. How to produce Clean Code?
// Naming
“Names are sound and smoke”
Johannes Wolfgang von Goethe – Faust I
19. How to produce Clean Code?
Take care about names!
// we name everything:
// variables, functions, arguments, classes, packages
// Naming
20. How to produce Clean Code?
public class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private static final String pszqint = “102“;
}
// Naming
Use Pronounceable Names
21. How to produce Clean Code?
public class Customer {
private static final String RECORD_ID = “102“;
private Date generationTimestamp;
private Date modificationTimestamp;
}
// Naming
Use Pronounceable Names
22. How to produce Clean Code?
private String m_dsc;
// Naming
Avoid Encodings (member prefixes)
private PhoneNumber phoneString;
Avoid Encodings (hungarian notation)
23. How to produce Clean Code?
private String description;
// Naming
Avoid Encodings (member prefixes)
private PhoneNumber phone;
Avoid Encodings (hungarian notation)
24. How to produce Clean Code?
Add Meaningful Context
fistName, lastName, street, city, state, zipcode
// a better solution
addressFirstName, addressLastName, addressState
// a better solution
Adress myAddress = new Address();
myAddress.getFirstName();
// Naming
25. How to produce Clean Code?
Small!
Rules of Functions:
1. should be small
2. should be smaller than that
// < 150 characters per line
// < 20 lines
// Functions
26. How to produce Clean Code?
Do One Thing
Functions should do ONE thing.
They should do it WELL.
They should do it ONLY.
// Functions
27. How to produce Clean Code?
One Level of Abstraction
// high level of abstraction
getHtml();
// intermediate level of abstraction
String pagePathName = PathParser.getName( pagePath );
// remarkable low level
htmlBuilder.append(“n”);
// Functions
28. How to produce Clean Code?
Switch Statements
class Employee {
int getSalary() {
switch( getType() ) {
case EmployeeType.ENGINEER:
return _monthlySalary;
case EmployeeType.SALESMAN:
return _monthlySalary + _commission;
case EmployeeType.MANAGER:
return _monthlySalary + _bonus;
default:
throw new InvalidEmployeeException();
}
}
}
// Functions
29. How to produce Clean Code?
Switch Statements
interface Employee {
int getSalary();
}
class Salesman implements Employee {
int getSalary() {
return getMonthlySalary() + getCommision();
}
}
class Manager implements Employee {
…
}
// Functions
30. How to produce Clean Code?
// niladic
getHtml();
// monadic
execute( boolean executionType );
// dyadic
assertEquals(String expected, String actual);
// triadic
drawCircle(double x, double y, double radius);
// Function Arguments
31. How to produce Clean Code?
Improve Monadic Functions with Flag Argument
// ???
execute( boolean executionType );
// !!!
executeInSuite();
executeStandAlone();
// Function Arguments
32. How to produce Clean Code?
Improve Dyadic Functions with two similar Argument Types
// ???
assertEquals(String expected, String actual);
assertEquals(String actual, String expected);
// !!!
assertExpectedEqualsActual(expected, actual);
// Function Arguments
33. How to produce Clean Code?
Improve Triadic Functions with three similar Argument Types
// ???
drawCircle(double x, double y, double radius);
drawCircle(double radius, double x, double y);
// …
// !!!
drawCircle(Point center, double radius);
// Function Arguments
34. How to produce Clean Code?
Don‘t comment bad code, rewrite it!
// Comments
35. How to produce Clean Code?
Noise
/** Default Consturctor */
public AnnaulDateRule() { … }
/** The day of the Month. */
private int dayOfMonth();
/** Returns the day of the Month.
@return the day of the Month. */
public int getDayOfMonth() {
return dayOfMonth;
}
// Comments
36. How to produce Clean Code?
Scary Noise
/** The name. */
private String name;
/** The version. */
private String version;
/** The licenseName. */
private String licenseName;
/** The version. */
private String info;
// Comments
37. How to produce Clean Code?
Don‘t use comments if you can use a Function/Variable
// does the moduel from the global list <mod> depend on the
// subsystem we are part of?
if(smodule.getDependSubsystems()
.contains(subSysMod.getSubSystem())) { … }
// improved
ArrayList moduleDependencies = module.getDependentSubsystems();
String ourSubsystem = subSystemModule.getSubSystem();
if( moduleDependencies.contains( ourSubsystem ) ) { … }
// Comments
38. How to produce Clean Code?
The Law of Demeter
// Bad
String outputDir = ctxt.getOptions()
.getScratchDir()
.getAbsolutePath();
// Good
String outputDir = ctxt.getScratchDirPath();
// Data Access
39. How to produce Clean Code?
Prefer Exceptions to Returning Error Codes
if( deletePage( page ) == E_OK ) {
if( registry.deleteReference(page.name) == E_OK ) {
if( configKeys.deleteKey( page.name.makeKey() ) == E_OK ) {
logger.log(“page deleted”);
} else {
logger.log(“configKey not deleted”);
}
} else {
logger.log(“deleteReference from registry failed”);
}
} else {
logger.log(“delete failed”);
}
// Error Handling
40. How to produce Clean Code?
Prefer Exceptions to Returning Error Codes
try {
deletePage(page);
registry.deleteReference(page.name);
configKeys.deleteKey(page.name.makeKey());
} catch( Exception e ) {
logger.log(e.getMessage());
}
// Error Handling
41. How to produce Clean Code?
Extract Try/Catch Blocks
public void delete(Page page) {
try {
deletePageAndAllReferences( page );
} catch ( Exception e ) {
logError( e );
}
}
private void deletePageAndAllReferences(Page page) throws Exception {
deletePage( page );
registry.deleteReference(page.name);
configKeys.deleteKey(page.name.makeKey());
}
// Error Handling
43. How to produce Clean Code?
Don‘t return Null
List<Employee> employees = getEmployees();
for( Employee employee : employees ) {
totalSalary += employee.getSalary();
}
// Error Handling
44. How to produce Clean Code?
Small!
Rules of Classes:
1. should be small
2. should be smaller than that
// Single Responsibility Principle (SRP)
// a class should have one, and only one, readon to change
// Classes
45. How to produce Clean Code?
// More Information
Have a look at:
Notes de l'éditeur
Inventor of C++
Author of „Object Oriented Analysis and Design with Applications“
-> fundamentals of UML
Author of „The Programmatic Programmer“ and created the phrases ‚Code Kata‘ and ‚DRY‘
Author of „Working Effectively woth legacy Code“
Inventor of Wiki
coinventor of eXtreme Programming
Motive force behinde Design Patterns
Want to impress your mom? Tell her you’re an author!
An author is someone who practices writing as a profession.
Developers write all day.
It’s easy to forget that each line of code we write is likely to be read 10 or more times by humans during its lifetime. These humans are our fellow co-workers. (fixing bugs and adding features)Great authors are known for writing books that tell a clear, compelling story. They use tools like chapters, headings, and paragraphs to clearly organize their thoughts and painlessly guide their reader.
Developers work in a very similar system, but simply use different jargon of namespaces, classes, and methods.
Everyone knows the previous co-worker whose name became a verb to describe dirty code.
laziness can be a positive attribute in the right context.
As I stumbled through writing code in my early years, I learned the hard way that writing clean code really pays off. Professional developers strive to be the good kind of lazy.
This laziness is based on putting extra care into the code so that it’s not so hard to write up front, and it’s easier to work with later.
Code read to write ratio is 10:1 -> like Database
-> brain sucks as cache