clean code is the fundamental to build a high quality software. a sharing session in "startup-talk" , 4th march 2017 at Incubator Universitas Amikom Yogyakarta.
2. 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)
9. Use descriptive name
This is bad:
protected $d; // elapsed time in days
This is good:
protected $elapsedTimeInDays;
protected $daysSinceCreation;
protected $daysSinceModification;
protected $fileAgeInDays;
10. Use pronounceable name
This is bad:
public $genymdhms;
public $modymdhms;
This is good:
public $generationTimestamp;
public $modificationTimestamp;
11. Use namespaces instead of prefixing names
This is bad:
class Part {
private $m_dsc;
}
This is good:
class Part {
private $description;
}
12. Don't be cute
This is bad:
$program>whack();
This is good:
$program>kill();
13. Use one word per one concept.
This is Bad :
void LoadSingleData()
void FetchDataFiltered()
Void GetAllData()
This is Better :
void SetDataToView();
void SetObjectValue(int value)
14. 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/
16. 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");
}
}
}
18. 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);
21. 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())
22. 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
23. 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;