clean code for high quality software

Embed Size (px)

Citation preview

{Clean Code}
The fundamental of high quality software
presented by Arif Akbarul Huda (twitter @omayib)

Interesting Quotes

Writing code is more like write a book. An other pepople will read and use it. Keep it human readible instead of mechine readible. @omayib (6/2/2017)

Buat aplikasi itu juga harus mikir gimana caranya bisa didebug dengan gampang. Ndak kayak gini juga. Edan @linklunx (25/1/2017)

Software quality & Productivity are realted. Better quality leads to enhanced productivity @_ericelliot (4/12/2016)

Problem state

Are you worried after publish your application to the store?

did you found any side effect after do refactoring?

Oh no! We have a spaghety code

Lets make our code cleaner than before.

1. Meaningful names

Use descriptive name

This is bad:

protected $d; // elapsed time in days

This is good:

protected $elapsedTimeInDays;protected $daysSinceCreation;protected $daysSinceModification;protected $fileAgeInDays;

Use pronounceable name

This is bad:

public $genymdhms;public $modymdhms;

This is good:

public $generationTimestamp;public $modificationTimestamp;

Use namespaces instead of prefixing names

This is bad:

class Part {

private $m_dsc;

}

This is good:

class Part {

private $description;

}

Don't be cute

This is bad:

$program->whack();

This is good:

$program->kill();

Use one word per one concept.

This is Bad :

void LoadSingleData()void FetchDataFiltered()Void GetAllData()

This is Better :

void SetDataToView();void SetObjectValue(int value)

Use meaningful names in their self context

This is bad :

string addressCity;string addressHomeNumber;string addressPostCode;

This is better :

class Address{string city;string homeNumber;string postCode;}

https://blog.goyello.com/2013/05/17/express-names-in-code-bad-vs-clean/

2. Better Function

The smaller the better
A function should only do one thing

public void purchase(List items){
if(user.getAge>17 && database != null ){
database.connect();
boolean status = database.save(items);
if(status==true){
textLabel.setText("purchase succeed");
}
}
}

public void purchase(List items){
if(user.isAllowed()){
try {
user.purcashe(items);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Less arguments are better

More than three arguments are evil. For example:

Circle makeCircle(Point center, double radius);

Is better than

Circle makeCircle(double x, double y, double radius);

3. Comment

Dont comment bad code, rewrite it

If code is readable you dont need comments

This is bad:

// Check to see if the employee is eligible for full benefits

if ($employee->flags && self::HOURLY_FLAG && $employee->age > 65)

This is good:

if ($employee->isEligibleForFullBenefits())

Explain your intention in comments :

// if we sort the array here the logic
// becomes simpler in calculatePayment()
// method

Warn of consequences in comments :

// this script will take a very long time
// to run

Emphasis important points in comments :

// the trim function is very important, in
// most cases the username has a trailing
// space

Noise comments are bad :

/** The day of the month. */private $dayOfMonth;

4. S.O.L.I.D. Priciple

S Single-responsiblity principle O Open-closed principle L Liskov substitution principle I Interface segregation principle D Dependency Inversion Principle

TOBE CONTINUE >>

Recommended Books