{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
Let’s 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<Item> 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<Item> 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
Don’t comment bad code, rewrite it
If code is readable you don’t 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

clean code for high quality software

  • 1.
    {Clean Code} The fundamental ofhigh quality software presented by Arif Akbarul Huda (twitter @omayib)
  • 2.
    Interesting Quotes “Writing codeis 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)
  • 3.
  • 4.
    Are you worriedafter publish your application to the store?
  • 5.
    did you foundany side effect after do refactoring?
  • 6.
    Oh no! Wehave a spaghety code
  • 7.
    Let’s make ourcode cleaner than before.
  • 8.
  • 9.
    Use descriptive name Thisis bad: protected $d; // elapsed time in days This is good: protected $elapsedTimeInDays; protected $daysSinceCreation; protected $daysSinceModification; protected $fileAgeInDays;
  • 10.
    Use pronounceable name Thisis bad: public $genymdhms; public $modymdhms; This is good: public $generationTimestamp; public $modificationTimestamp;
  • 11.
    Use namespaces insteadof prefixing names This is bad: class Part { private $m_dsc; }  This is good: class Part { private $description; } 
  • 12.
    Don't be cute Thisis bad: $program­>whack(); This is good: $program­>kill();
  • 13.
    Use one wordper one concept. This is Bad : void LoadSingleData() void FetchDataFiltered() Void GetAllData() This is Better : void SetDataToView(); void SetObjectValue(int value)
  • 14.
    Use meaningful namesin 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/
  • 15.
  • 16.
    The smaller thebetter A function should only do one thing public void purchase(List<Item> items){     if(user.getAge>17 && database != null ){         database.connect();         boolean status = database.save(items);         if(status==true){             textLabel.setText("purchase succeed");         }     } }
  • 17.
  • 18.
    Less arguments arebetter 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);
  • 19.
  • 20.
    Don’t comment badcode, rewrite it
  • 21.
    If code isreadable you don’t 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())
  • 22.
    Explain your intentionin 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 
  • 23.
    Emphasis important pointsin 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;
  • 24.
  • 25.
    S – Single-responsiblityprinciple O – Open-closed principle L – Liskov substitution principle I – Interface segregation principle D – Dependency Inversion Principle TOBE CONTINUE >>
  • 26.