SlideShare a Scribd company logo
1 of 64
Download to read offline
Clean Code
⇧⌘K
Edwin Kwok
Monday, 11 March, 13
Provided to you by
Open source development kit for
mobile, web & IoT apps
Skygear.io
Skygear.io
Clean Code
Title: Clean Code: A
Handbook of Agile
Software
Craftsmanship
Author: Robert C.
Martin (Uncle Bob)
Monday, 11 March, 13
Skygear.io
Why Clean Code?
Monday, 11 March, 13
Skygear.io
Why Clean Code?
0
10
20
30
40
50
60
70
80
90
100
0 500 1000 2000 2500 3000 3500 4000
Productivity Ratio vs Time
Monday, 11 March, 13
Skygear.io
Smart vs Professional
Smart
Great Coding Skill
Write advanced code
String r; // lowercase url
Monday, 11 March, 13
Skygear.io
Smart vs Professional
Smart
Great Coding Skill
Write advanced code
String r; // lowercase url
Professional
Readable code
Maintainable code
String lowercaseUrl;
Monday, 11 March, 13
Skygear.io
Name
Choose your names thoughtfully
If a name requires a comment, the name
does not reveal its intent
Monday, 11 March, 13
Skygear.io
Name
Choose your names thoughtfully
If a name requires a comment, the name
does not reveal its intent
int d;
What does it mean? Days? Diameter?
Monday, 11 March, 13
Skygear.io
Name
Choose your names thoughtfully
If a name requires a comment, the name
does not reveal its intent
int d;
What does it mean? Days? Diameter?
int d; //elapsed time in days
Is this any better?
Monday, 11 March, 13
Skygear.io
Name
Choose your names thoughtfully
If a name requires a comment, the name
does not reveal its intent
int d;
What does it mean? Days? Diameter?
int elapsedTimeInDays;
What about this?
int d; //elapsed time in days
Is this any better?
Monday, 11 March, 13
Skygear.io
Name
Choose part of speech well
method / function => verb
class / object => noun
def authentication
# ...
end
def authenticate
# ...
end
Monday, 11 March, 13
Skygear.io
Name
use Pronounceable Names
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private final String pszqint = “102”;
}
class Customer {
private Date generationTimestamp;
private Date modificationTimestamp;
private final String recordId = “102”;
}
Monday, 11 March, 13
Skygear.io
Names...
Avoid encodings
public class Part {
private String mName;
void setName(String name) {
mName = name;
}
}
public class Part {
private String name;
void setName(String name) {
this.name = name;
}
}
Hungarian notation
bBusy: boolean
chInitial: char
cApples: count of items
fpPrice: floating-point
dbPi: double (Systems)
pFoo: pointer
Monday, 11 March, 13
Skygear.io
Discussion:
Android vs IOS
ListView listView = new ListView(context);
listView.setAdapter(anAdapter);
UITableView tableView =
[[UITableView alloc]
initWithFrame:aFrame
style:UITableViewStylePlain];
tableView.datasource = aDataSource;
Android:
IOS:
Which one is better?
Monday, 11 March, 13
Skygear.io
Adapter
The adapter pattern is adapting between
classes and objects, like a bridge between
two objects.
e.g.
SimpleCursorAdapter
ArrayAdapter
Monday, 11 March, 13
Skygear.io
Adapter
Title: Design Patterns: Elements of
Reusable Object-Oriented Software
Author:
John Vlissides
Richard Helm
Ralph Johnson
Erich Gamma
Monday, 11 March, 13
Skygear.io
Use Case (Philips)
UITabBarController
Adapter
UITableViewController
Monday, 11 March, 13
Skygear.io
Use design pattern as name
Use Solution Domain Names, e.g.
AccountVisitor
JobQueue
LabelObserver
SimpleCursorAdapter
Monday, 11 March, 13
Skygear.io
Singleton
https://www.youtube.com/watch?v=-FRm3VPhseI
Monday, 11 March, 13
Skygear.io
Singleton
https://www.youtube.com/watch?v=-FRm3VPhseI
v.s.
Global Object
Monday, 11 March, 13
Skygear.io
Deceptive API
testCharge() {
CreditCard cc;
cc = new CreditCard(“1234567890121234”);
cc.charge(100);
}
java.lang.NullPointerException
at talk.CreditCard.charge(CreditCard.java:49)
Monday, 11 March, 13
Skygear.io
Deceptive API
testCharge() {
CreditCardProcessor.init(...);
CreditCard cc;
cc = new CreditCard(“1234567890121234”);
cc.charge(100);
}
java.lang.NullPointerException
at talk.CreditCardProcessor.init
(CreditCardProcessor.java:146)
Monday, 11 March, 13
Skygear.io
Deceptive API
testCharge() {
OffineQueue.start();
CreditCardProcessor.init(...);
CreditCard cc;
cc = new CreditCard(“1234567890121234”);
cc.charge(100);
}
java.lang.NullPointerException
at talk.OfflineQueue.start (OfflineQueue.java:16)
Monday, 11 March, 13
Skygear.io
Deceptive API
testCharge() {
Database.connect(...);
OffineQueue.start();
CreditCardProcessor.init(...);
CreditCard cc;
cc = new CreditCard(“1234567890121234”);
cc.charge(100);
}
CreditCard API lies
It pretends to not need the CreditCardProcessor
The API doesn’t tell the exact order of the
initialization
Monday, 11 March, 13
Skygear.io
Deceptive API
testCharge() {
database = new Database(...);
queue = new OfflineQueue(database);
creditCardProcessor = new CreditCardProcessor(queue);
CreditCard cc;
cc = new CreditCard(“1234567890121234”,
creditCardProcessor);
cc.charge(100);
}
Dependency injection enforces the order of
initialization at compile time.
Monday, 11 March, 13
Skygear.io
Discussion:
Object passing in Android?
How to pass objects to another Activity?
Monday, 11 March, 13
Skygear.io
Comment
Monday, 11 March, 13
Skygear.io
Comment
//* no Comments *//
Monday, 11 March, 13
Skygear.io
Comment
Comment doesn’t make your code becomes
good code
//* no Comments *//
Monday, 11 March, 13
Skygear.io
Good Comments
Informative Comments
// format matched kk:mm:ss EEE, MMM dd, yyyy
Pattern timePattern =
Pattern.compile(
“d*:d*:d* w*, w* d*, d*”);
Todo Comments
/*
TODO: All calls to getPage should actually come
here, and be relative to the current page, not the
parent page. It was a gross error to have the whole
wiki know that references were relative to the
parent instead of the page.
*/
Pubic API documentation
Monday, 11 March, 13
Skygear.io
Bad Comments
Redundant Comments
/**
* The processor delay for this component.
*/
protected int backgroundProcessorDelay = -1;
/**
* The container event listeners for this Container.
*/
protected ArrayList listeners = new ArrayList();
Monday, 11 March, 13
Skygear.io
Bad Comments
Attribution Comments
/* Added by Gary */
Big Banner Comments
// **********************
// * Instance Variables *
// *********************
private int myVariable;
// ***********************
// * Default Constructor *
// ***********************
public MyClass() {}
Monday, 11 March, 13
Skygear.io
Mumbling
/* For bug FS-13005, we had to add this. The bug
was that the Now Playing screen was somehow being
launched, in that viewDidAppear was being called, but the view
was not being shown on the screen. The Now Playing screen then
when on to do all it's stuff and the user was left looking at an
incomplete Mode screen. So the "fix" is to kill off any residual Now
Playing screen that is under the Mode tab whenever we start a
new connection to a radio. */
What is
FS-13005?
Sorry! I have
no idea what you are talking about.
Monday, 11 March, 13
Skygear.io
Horizontal Alignment
@interface Tape : NSObject {
! NSString *_brushName;
! NSString *_headImageNamed;
! NSString *_bodyImageNamed;
! NSString *_tailImageNamed;
! CGFloat _opacity;
! BOOL! _includeShadow;
! NSString *_text;
! NSString *_fontName;
! CGFloat! _fontSize;
! NSString *_colorHex;
! NSDictionary *_all;
}
@interface Tape : NSObject {
! NSString *_brushName;
! NSString *_headImageNamed;
! NSString *_bodyImageNamed;
! NSString *_tailImageNamed;
! CGFloat _opacity;
! BOOL! _includeShadow;
! NSString *_text;
! NSString *_fontName;
! CGFloat! _fontSize;
! NSString *_colorHex;
! NSDictionary *_all;
}
Monday, 11 March, 13
Skygear.io
Function
should be Small
does one thing
the ideal number of arguments for a
function is .... 0
the less arguments, the better
try not to more than 3 arguments
Monday, 11 March, 13
Skygear.io
Function
No Side effects
// do something or answer something, but not both
public boolean set(String attribute, String value);
if (attributeExists(“username”)) {
setAttribute(“username”, “Ben”);
}
Monday, 11 March, 13
Skygear.io
Class
Avoid God Class
In OO, God Class is a class that does lots
of things
example: UITableViewController
Monday, 11 March, 13
Skygear.io
Data Structure and Object
public class Square {
public Point topLeft;
public double side;
}
public class Geometry {
public double calculateArea(Object shape)
throws noSuchShapeException {
if (shape instanceof Square) {
Square square = (Square)shape;
return square.side * square.side;
} else if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle)shape;
return rectangle.height * rectangle.width;
}
throw new NoSuchShapeException();
}
}
public class Rectangle {
public Point topLeft;
public double height;
public double width;
}
Monday, 11 March, 13
Skygear.io
Data Structure and Object
public class Square implements Shape {
public Point topLeft;
public double side;
public double area() {
return side*side;
}
}
public class Rectangle implements Shape{
public Point topLeft;
public double height;
public double width;
public double area() {
return height * width;
}
}
Monday, 11 March, 13
Skygear.io
Data Structure and Object
Procedural Code (code using data structure)
Pros: easy to add new functions without
changing existing data structure
Cons: hard to add new data structure all
the functions must change
OO code
Pros: easy to add new classes without
changing existing function
Cons: hard to a new function as all classes
must change
Monday, 11 March, 13
Skygear.io
Data Structure and Object
Procedural Code (code using data structure)
Pros: easy to add new functions without
changing existing data structure
Cons: hard to add new data structure all
the functions must change
OO code
Pros: easy to add new classes without
changing existing function
Cons: hard to a new function as all classes
must change
Avoid Hybrids
Monday, 11 March, 13
Skygear.io
How About
ActiveRecord?
Monday, 11 March, 13
Error Handling
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”);
return E_ERROR;
}
Monday, 11 March, 13
Skygear.io
Error Handling
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());
}
easier to find the normal path
avoid nested conditions
Monday, 11 March, 13
Skygear.io
Error Handling
Don’t Return Null
List<Employee> employees = getEmployees();
if (employees != null) {
for (Employee e : employees) {
totalPay += e.getPay();
}
}
Monday, 11 March, 13
Skygear.io
Error Handling
Don’t Return Null
List<Employee> employees = getEmployees();
for (Employee e : employees) {
totalPay += e.getPay();
}
public List<Employee> getEmployees() {
if (/* there are no employees */) {
return Collections.emptyList();
}
}
Monday, 11 March, 13
Skygear.io
SOLID
Single Responsibility Principle
Open Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency inversion Principle
Monday, 11 March, 13
Skygear.io
Single Responsibility Principle
LabelBox Cloud Labels
Monday, 11 March, 13
Skygear.io
Architecture
Monday, 11 March, 13
Skygear.io
Monday, 11 March, 13
Skygear.io
Architecture
The Lost Years
http://www.youtube.com/watch?
v=WpkDN78P884
The Web (Rails) is a Delivery Mechanism!
Monday, 11 March, 13
Skygear.io
Test - a real scenario
...
client server
workerss3
1. upload a video
2. store in s3
3. start worker and
send request to worker
4. dedicated worker pulls the file
from s3 and do transcoding
Monday, 11 March, 13
Skygear.io
Test
Remove FEAR during Development
You know you break something during
testing.
For example:
Rugby - ~178 tests
Refer to Ben’s presentation
https://speakerdeck.com/oursky/testing
Monday, 11 March, 13
Skygear.io
Test
FIRST
Fast
Independent
Repeatable
Self-validating
Timely
Monday, 11 March, 13
Skygear.io
Refactor
Title:Refactoring: Improving the
Design of Existing Code
Author:
Martin Fowler
Kent Beck
John Brant
William Opdyke
Don Roberts
Monday, 11 March, 13
Skygear.io
Familiar With your Tools
try to familiar with your tools before coding,
don’t create messy stuffs by your first
impression
do testing before adopting to the real code
read the API, doc and Google
ask others ...
Monday, 11 March, 13
Skygear.io
Quiz (javascript)
The expected output of the following
javascript is alert count down from 5 to 0.
Explain why it doesn’t work and fix the bug.
function count (num) {
for (var i = 0; i <= num; i += 1) {
setTimeout(function () {
alert(num - i);
}, i * 1000);
}
}
count(5);
Monday, 11 March, 13
Skygear.io
Quiz (javascript)
function count (num) {
for (var i = 0; i <= num; i += 1) {
(function (time) {
setTimeout(function () {
alert(num - time);
}, time * 1000);
}(i));
}
}
count(5);
Monday, 11 March, 13
Skygear.ioSkygear.io
Quiz (javascript) cont.
function changeAnchorsToLightBox(anchors) {
var length = anchors.length;
for (var i = 0; i < length; i++) {
anchors[i].onclick = function () {
lightBox.open(anchors[i]);
return false;
};
}
}
Monday, 11 March, 13
Skygear.io
Quiz (javascript) cont.
function changeAnchorsToLightBox(anchors) {
var length = anchors.length;
for (var i = 0; i < length; i++) {
(function (anchor) {
anchor.onclick = function () {
lightBox.open(anchor);
return false;
};
}(anchors[i]));
}
}
Monday, 11 March, 13
Skygear.io
Quiz (javascript) cont.
function changeAnchorsToLightBox(anchors) {
var length = anchors.length;
for (var i = 0; i < length; i++) {
(function (anchor) {
anchor.onclick = function () {
lightBox.open(anchor);
return false;
};
}(anchors[i]));
}
}
Monday, 11 March, 13
Skygear.io
Reference
Clean Code: A Handbook of Agile Software
Craftsmanship
http://www.amazon.com/Clean-Code-Handbook-
Software-Craftsmanship/dp/0132350882
The Clean Code Talks - "Global State and
Singletons"
https://www.youtube.com/watch?v=-FRm3VPhseI
Monday, 11 March, 13
Skygear.io
Q & A and ...
“Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.”
Martin Fowler:
Monday, 11 March, 13
Skygear.io
Brought to you by Oursky
Build your mobile app fast
skygear.io (open source)

More Related Content

Viewers also liked

PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
How to Teach Yourself to Code
How to Teach Yourself to CodeHow to Teach Yourself to Code
How to Teach Yourself to CodeMattan Griffel
 
K to 12 General Presentation
K to 12 General PresentationK to 12 General Presentation
K to 12 General PresentationDepEdPhilippines
 
Learn HTML & CSS From Scratch in 30 Days
Learn HTML & CSS From Scratch in 30 DaysLearn HTML & CSS From Scratch in 30 Days
Learn HTML & CSS From Scratch in 30 DaysJonathan Grover
 
How Not To Crumble Under Pressure
How Not To Crumble Under PressureHow Not To Crumble Under Pressure
How Not To Crumble Under PressureFaisal Hoque
 
K to 12 Mathematics Curriculum Guide for Grades 1 to 10
K to 12 Mathematics Curriculum Guide for Grades 1 to 10K to 12 Mathematics Curriculum Guide for Grades 1 to 10
K to 12 Mathematics Curriculum Guide for Grades 1 to 10Dr. Joy Kenneth Sala Biasong
 
Must Haves For Small Business Growth
Must Haves For Small Business GrowthMust Haves For Small Business Growth
Must Haves For Small Business GrowthFaisal Hoque
 
大型 Web Application 轉移到 微服務的經驗分享
大型 Web Application 轉移到微服務的經驗分享大型 Web Application 轉移到微服務的經驗分享
大型 Web Application 轉移到 微服務的經驗分享Andrew Wu
 
The Universe: A Module in Science and Technology for Grade 5 Pupils
The Universe: A Module in Science and Technology for Grade 5 PupilsThe Universe: A Module in Science and Technology for Grade 5 Pupils
The Universe: A Module in Science and Technology for Grade 5 Pupilscryster
 
How to write a good business letter
How to write a good business letter   How to write a good business letter
How to write a good business letter Sukh Sandhu
 
50 Ways to Become More Professionally Excellent
50 Ways to Become More Professionally Excellent50 Ways to Become More Professionally Excellent
50 Ways to Become More Professionally ExcellentLeslie Bradshaw
 
Personal SWOT for Teachers
Personal SWOT for TeachersPersonal SWOT for Teachers
Personal SWOT for Teachersm nagaRAJU
 

Viewers also liked (14)

PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
How to Teach Yourself to Code
How to Teach Yourself to CodeHow to Teach Yourself to Code
How to Teach Yourself to Code
 
K to 12 General Presentation
K to 12 General PresentationK to 12 General Presentation
K to 12 General Presentation
 
Learn HTML & CSS From Scratch in 30 Days
Learn HTML & CSS From Scratch in 30 DaysLearn HTML & CSS From Scratch in 30 Days
Learn HTML & CSS From Scratch in 30 Days
 
How Not To Crumble Under Pressure
How Not To Crumble Under PressureHow Not To Crumble Under Pressure
How Not To Crumble Under Pressure
 
K to 12 Mathematics Curriculum Guide for Grades 1 to 10
K to 12 Mathematics Curriculum Guide for Grades 1 to 10K to 12 Mathematics Curriculum Guide for Grades 1 to 10
K to 12 Mathematics Curriculum Guide for Grades 1 to 10
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Must Haves For Small Business Growth
Must Haves For Small Business GrowthMust Haves For Small Business Growth
Must Haves For Small Business Growth
 
K to 12 Science Curriculum Guide
K to 12  Science Curriculum GuideK to 12  Science Curriculum Guide
K to 12 Science Curriculum Guide
 
大型 Web Application 轉移到 微服務的經驗分享
大型 Web Application 轉移到微服務的經驗分享大型 Web Application 轉移到微服務的經驗分享
大型 Web Application 轉移到 微服務的經驗分享
 
The Universe: A Module in Science and Technology for Grade 5 Pupils
The Universe: A Module in Science and Technology for Grade 5 PupilsThe Universe: A Module in Science and Technology for Grade 5 Pupils
The Universe: A Module in Science and Technology for Grade 5 Pupils
 
How to write a good business letter
How to write a good business letter   How to write a good business letter
How to write a good business letter
 
50 Ways to Become More Professionally Excellent
50 Ways to Become More Professionally Excellent50 Ways to Become More Professionally Excellent
50 Ways to Become More Professionally Excellent
 
Personal SWOT for Teachers
Personal SWOT for TeachersPersonal SWOT for Teachers
Personal SWOT for Teachers
 

Similar to How to write better code: in-depth best practices for writing readable, simple, extendable and efficient code (Part I)

Drupal 8 configuration system for coders and site builders - Drupalaton 2013
Drupal 8 configuration system for coders and site builders - Drupalaton 2013Drupal 8 configuration system for coders and site builders - Drupalaton 2013
Drupal 8 configuration system for coders and site builders - Drupalaton 2013swentel
 
What is this DI and AOP stuff anyway...
What is this DI and AOP stuff anyway...What is this DI and AOP stuff anyway...
What is this DI and AOP stuff anyway...Richard McIntyre
 
Drupal 8 configuration system for coders and site builders - DrupalCamp Balti...
Drupal 8 configuration system for coders and site builders - DrupalCamp Balti...Drupal 8 configuration system for coders and site builders - DrupalCamp Balti...
Drupal 8 configuration system for coders and site builders - DrupalCamp Balti...swentel
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaDILo Surabaya
 
Functional Reactive Programming on Android
Functional Reactive Programming on AndroidFunctional Reactive Programming on Android
Functional Reactive Programming on AndroidSam Lee
 
The Best Way to Become an Android Developer Expert with Android Jetpack
The Best Way to Become an Android Developer Expert  with Android JetpackThe Best Way to Become an Android Developer Expert  with Android Jetpack
The Best Way to Become an Android Developer Expert with Android JetpackAhmad Arif Faizin
 
Purely Functional I/O
Purely Functional I/OPurely Functional I/O
Purely Functional I/OC4Media
 
Android Security & Penetration Testing
Android Security & Penetration TestingAndroid Security & Penetration Testing
Android Security & Penetration TestingSubho Halder
 
XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>Arun Gupta
 
Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.Eugene Lazutkin
 
Workers of the web - BrazilJS 2013
Workers of the web - BrazilJS 2013Workers of the web - BrazilJS 2013
Workers of the web - BrazilJS 2013Thibault Imbert
 
Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Murat Yener
 
Engineering culture
Engineering cultureEngineering culture
Engineering culturePamela Fox
 
The Horoscope of OSGi: Meet Eclipse Libra, Virgo and Gemini (JavaOne 2013)
The Horoscope of OSGi: Meet Eclipse Libra, Virgo and Gemini (JavaOne 2013)The Horoscope of OSGi: Meet Eclipse Libra, Virgo and Gemini (JavaOne 2013)
The Horoscope of OSGi: Meet Eclipse Libra, Virgo and Gemini (JavaOne 2013)Murat Yener
 
Symfony2 and MongoDB - MidwestPHP 2013
Symfony2 and MongoDB - MidwestPHP 2013   Symfony2 and MongoDB - MidwestPHP 2013
Symfony2 and MongoDB - MidwestPHP 2013 Pablo Godel
 
Dinosaurs and Androids: The Listview Evolution
Dinosaurs and Androids: The Listview EvolutionDinosaurs and Androids: The Listview Evolution
Dinosaurs and Androids: The Listview EvolutionFernando Cejas
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahNick Plante
 
Towards a software ecosystem for java prolog interoperabilty
Towards a software ecosystem for java prolog interoperabiltyTowards a software ecosystem for java prolog interoperabilty
Towards a software ecosystem for java prolog interoperabiltykim.mens
 

Similar to How to write better code: in-depth best practices for writing readable, simple, extendable and efficient code (Part I) (20)

Drupal 8 configuration system for coders and site builders - Drupalaton 2013
Drupal 8 configuration system for coders and site builders - Drupalaton 2013Drupal 8 configuration system for coders and site builders - Drupalaton 2013
Drupal 8 configuration system for coders and site builders - Drupalaton 2013
 
What is this DI and AOP stuff anyway...
What is this DI and AOP stuff anyway...What is this DI and AOP stuff anyway...
What is this DI and AOP stuff anyway...
 
Demystifying Maven
Demystifying MavenDemystifying Maven
Demystifying Maven
 
Drupal 8 configuration system for coders and site builders - DrupalCamp Balti...
Drupal 8 configuration system for coders and site builders - DrupalCamp Balti...Drupal 8 configuration system for coders and site builders - DrupalCamp Balti...
Drupal 8 configuration system for coders and site builders - DrupalCamp Balti...
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo Surabaya
 
Functional Reactive Programming on Android
Functional Reactive Programming on AndroidFunctional Reactive Programming on Android
Functional Reactive Programming on Android
 
The Best Way to Become an Android Developer Expert with Android Jetpack
The Best Way to Become an Android Developer Expert  with Android JetpackThe Best Way to Become an Android Developer Expert  with Android Jetpack
The Best Way to Become an Android Developer Expert with Android Jetpack
 
Purely Functional I/O
Purely Functional I/OPurely Functional I/O
Purely Functional I/O
 
Android Security & Penetration Testing
Android Security & Penetration TestingAndroid Security & Penetration Testing
Android Security & Penetration Testing
 
XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>
 
Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.
 
Workers of the web - BrazilJS 2013
Workers of the web - BrazilJS 2013Workers of the web - BrazilJS 2013
Workers of the web - BrazilJS 2013
 
Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15
 
Engineering culture
Engineering cultureEngineering culture
Engineering culture
 
The Horoscope of OSGi: Meet Eclipse Libra, Virgo and Gemini (JavaOne 2013)
The Horoscope of OSGi: Meet Eclipse Libra, Virgo and Gemini (JavaOne 2013)The Horoscope of OSGi: Meet Eclipse Libra, Virgo and Gemini (JavaOne 2013)
The Horoscope of OSGi: Meet Eclipse Libra, Virgo and Gemini (JavaOne 2013)
 
Symfony2 and MongoDB - MidwestPHP 2013
Symfony2 and MongoDB - MidwestPHP 2013   Symfony2 and MongoDB - MidwestPHP 2013
Symfony2 and MongoDB - MidwestPHP 2013
 
Dinosaurs and Androids: The Listview Evolution
Dinosaurs and Androids: The Listview EvolutionDinosaurs and Androids: The Listview Evolution
Dinosaurs and Androids: The Listview Evolution
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 
Serialization
SerializationSerialization
Serialization
 
Towards a software ecosystem for java prolog interoperabilty
Towards a software ecosystem for java prolog interoperabiltyTowards a software ecosystem for java prolog interoperabilty
Towards a software ecosystem for java prolog interoperabilty
 

Recently uploaded

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 

Recently uploaded (20)

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 

How to write better code: in-depth best practices for writing readable, simple, extendable and efficient code (Part I)

  • 1. Clean Code ⇧⌘K Edwin Kwok Monday, 11 March, 13 Provided to you by Open source development kit for mobile, web & IoT apps Skygear.io Skygear.io
  • 2. Clean Code Title: Clean Code: A Handbook of Agile Software Craftsmanship Author: Robert C. Martin (Uncle Bob) Monday, 11 March, 13 Skygear.io
  • 3. Why Clean Code? Monday, 11 March, 13 Skygear.io
  • 4. Why Clean Code? 0 10 20 30 40 50 60 70 80 90 100 0 500 1000 2000 2500 3000 3500 4000 Productivity Ratio vs Time Monday, 11 March, 13 Skygear.io
  • 5. Smart vs Professional Smart Great Coding Skill Write advanced code String r; // lowercase url Monday, 11 March, 13 Skygear.io
  • 6. Smart vs Professional Smart Great Coding Skill Write advanced code String r; // lowercase url Professional Readable code Maintainable code String lowercaseUrl; Monday, 11 March, 13 Skygear.io
  • 7. Name Choose your names thoughtfully If a name requires a comment, the name does not reveal its intent Monday, 11 March, 13 Skygear.io
  • 8. Name Choose your names thoughtfully If a name requires a comment, the name does not reveal its intent int d; What does it mean? Days? Diameter? Monday, 11 March, 13 Skygear.io
  • 9. Name Choose your names thoughtfully If a name requires a comment, the name does not reveal its intent int d; What does it mean? Days? Diameter? int d; //elapsed time in days Is this any better? Monday, 11 March, 13 Skygear.io
  • 10. Name Choose your names thoughtfully If a name requires a comment, the name does not reveal its intent int d; What does it mean? Days? Diameter? int elapsedTimeInDays; What about this? int d; //elapsed time in days Is this any better? Monday, 11 March, 13 Skygear.io
  • 11. Name Choose part of speech well method / function => verb class / object => noun def authentication # ... end def authenticate # ... end Monday, 11 March, 13 Skygear.io
  • 12. Name use Pronounceable Names class DtaRcrd102 { private Date genymdhms; private Date modymdhms; private final String pszqint = “102”; } class Customer { private Date generationTimestamp; private Date modificationTimestamp; private final String recordId = “102”; } Monday, 11 March, 13 Skygear.io
  • 13. Names... Avoid encodings public class Part { private String mName; void setName(String name) { mName = name; } } public class Part { private String name; void setName(String name) { this.name = name; } } Hungarian notation bBusy: boolean chInitial: char cApples: count of items fpPrice: floating-point dbPi: double (Systems) pFoo: pointer Monday, 11 March, 13 Skygear.io
  • 14. Discussion: Android vs IOS ListView listView = new ListView(context); listView.setAdapter(anAdapter); UITableView tableView = [[UITableView alloc] initWithFrame:aFrame style:UITableViewStylePlain]; tableView.datasource = aDataSource; Android: IOS: Which one is better? Monday, 11 March, 13 Skygear.io
  • 15. Adapter The adapter pattern is adapting between classes and objects, like a bridge between two objects. e.g. SimpleCursorAdapter ArrayAdapter Monday, 11 March, 13 Skygear.io
  • 16. Adapter Title: Design Patterns: Elements of Reusable Object-Oriented Software Author: John Vlissides Richard Helm Ralph Johnson Erich Gamma Monday, 11 March, 13 Skygear.io
  • 18. Use design pattern as name Use Solution Domain Names, e.g. AccountVisitor JobQueue LabelObserver SimpleCursorAdapter Monday, 11 March, 13 Skygear.io
  • 21. Deceptive API testCharge() { CreditCard cc; cc = new CreditCard(“1234567890121234”); cc.charge(100); } java.lang.NullPointerException at talk.CreditCard.charge(CreditCard.java:49) Monday, 11 March, 13 Skygear.io
  • 22. Deceptive API testCharge() { CreditCardProcessor.init(...); CreditCard cc; cc = new CreditCard(“1234567890121234”); cc.charge(100); } java.lang.NullPointerException at talk.CreditCardProcessor.init (CreditCardProcessor.java:146) Monday, 11 March, 13 Skygear.io
  • 23. Deceptive API testCharge() { OffineQueue.start(); CreditCardProcessor.init(...); CreditCard cc; cc = new CreditCard(“1234567890121234”); cc.charge(100); } java.lang.NullPointerException at talk.OfflineQueue.start (OfflineQueue.java:16) Monday, 11 March, 13 Skygear.io
  • 24. Deceptive API testCharge() { Database.connect(...); OffineQueue.start(); CreditCardProcessor.init(...); CreditCard cc; cc = new CreditCard(“1234567890121234”); cc.charge(100); } CreditCard API lies It pretends to not need the CreditCardProcessor The API doesn’t tell the exact order of the initialization Monday, 11 March, 13 Skygear.io
  • 25. Deceptive API testCharge() { database = new Database(...); queue = new OfflineQueue(database); creditCardProcessor = new CreditCardProcessor(queue); CreditCard cc; cc = new CreditCard(“1234567890121234”, creditCardProcessor); cc.charge(100); } Dependency injection enforces the order of initialization at compile time. Monday, 11 March, 13 Skygear.io
  • 26. Discussion: Object passing in Android? How to pass objects to another Activity? Monday, 11 March, 13 Skygear.io
  • 27. Comment Monday, 11 March, 13 Skygear.io
  • 28. Comment //* no Comments *// Monday, 11 March, 13 Skygear.io
  • 29. Comment Comment doesn’t make your code becomes good code //* no Comments *// Monday, 11 March, 13 Skygear.io
  • 30. Good Comments Informative Comments // format matched kk:mm:ss EEE, MMM dd, yyyy Pattern timePattern = Pattern.compile( “d*:d*:d* w*, w* d*, d*”); Todo Comments /* TODO: All calls to getPage should actually come here, and be relative to the current page, not the parent page. It was a gross error to have the whole wiki know that references were relative to the parent instead of the page. */ Pubic API documentation Monday, 11 March, 13 Skygear.io
  • 31. Bad Comments Redundant Comments /** * The processor delay for this component. */ protected int backgroundProcessorDelay = -1; /** * The container event listeners for this Container. */ protected ArrayList listeners = new ArrayList(); Monday, 11 March, 13 Skygear.io
  • 32. Bad Comments Attribution Comments /* Added by Gary */ Big Banner Comments // ********************** // * Instance Variables * // ********************* private int myVariable; // *********************** // * Default Constructor * // *********************** public MyClass() {} Monday, 11 March, 13 Skygear.io
  • 33. Mumbling /* For bug FS-13005, we had to add this. The bug was that the Now Playing screen was somehow being launched, in that viewDidAppear was being called, but the view was not being shown on the screen. The Now Playing screen then when on to do all it's stuff and the user was left looking at an incomplete Mode screen. So the "fix" is to kill off any residual Now Playing screen that is under the Mode tab whenever we start a new connection to a radio. */ What is FS-13005? Sorry! I have no idea what you are talking about. Monday, 11 March, 13 Skygear.io
  • 34. Horizontal Alignment @interface Tape : NSObject { ! NSString *_brushName; ! NSString *_headImageNamed; ! NSString *_bodyImageNamed; ! NSString *_tailImageNamed; ! CGFloat _opacity; ! BOOL! _includeShadow; ! NSString *_text; ! NSString *_fontName; ! CGFloat! _fontSize; ! NSString *_colorHex; ! NSDictionary *_all; } @interface Tape : NSObject { ! NSString *_brushName; ! NSString *_headImageNamed; ! NSString *_bodyImageNamed; ! NSString *_tailImageNamed; ! CGFloat _opacity; ! BOOL! _includeShadow; ! NSString *_text; ! NSString *_fontName; ! CGFloat! _fontSize; ! NSString *_colorHex; ! NSDictionary *_all; } Monday, 11 March, 13 Skygear.io
  • 35. Function should be Small does one thing the ideal number of arguments for a function is .... 0 the less arguments, the better try not to more than 3 arguments Monday, 11 March, 13 Skygear.io
  • 36. Function No Side effects // do something or answer something, but not both public boolean set(String attribute, String value); if (attributeExists(“username”)) { setAttribute(“username”, “Ben”); } Monday, 11 March, 13 Skygear.io
  • 37. Class Avoid God Class In OO, God Class is a class that does lots of things example: UITableViewController Monday, 11 March, 13 Skygear.io
  • 38. Data Structure and Object public class Square { public Point topLeft; public double side; } public class Geometry { public double calculateArea(Object shape) throws noSuchShapeException { if (shape instanceof Square) { Square square = (Square)shape; return square.side * square.side; } else if (shape instanceof Rectangle) { Rectangle rectangle = (Rectangle)shape; return rectangle.height * rectangle.width; } throw new NoSuchShapeException(); } } public class Rectangle { public Point topLeft; public double height; public double width; } Monday, 11 March, 13 Skygear.io
  • 39. Data Structure and Object public class Square implements Shape { public Point topLeft; public double side; public double area() { return side*side; } } public class Rectangle implements Shape{ public Point topLeft; public double height; public double width; public double area() { return height * width; } } Monday, 11 March, 13 Skygear.io
  • 40. Data Structure and Object Procedural Code (code using data structure) Pros: easy to add new functions without changing existing data structure Cons: hard to add new data structure all the functions must change OO code Pros: easy to add new classes without changing existing function Cons: hard to a new function as all classes must change Monday, 11 March, 13 Skygear.io
  • 41. Data Structure and Object Procedural Code (code using data structure) Pros: easy to add new functions without changing existing data structure Cons: hard to add new data structure all the functions must change OO code Pros: easy to add new classes without changing existing function Cons: hard to a new function as all classes must change Avoid Hybrids Monday, 11 March, 13 Skygear.io
  • 43. Error Handling 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”); return E_ERROR; } Monday, 11 March, 13 Skygear.io
  • 44. Error Handling 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()); } easier to find the normal path avoid nested conditions Monday, 11 March, 13 Skygear.io
  • 45. Error Handling Don’t Return Null List<Employee> employees = getEmployees(); if (employees != null) { for (Employee e : employees) { totalPay += e.getPay(); } } Monday, 11 March, 13 Skygear.io
  • 46. Error Handling Don’t Return Null List<Employee> employees = getEmployees(); for (Employee e : employees) { totalPay += e.getPay(); } public List<Employee> getEmployees() { if (/* there are no employees */) { return Collections.emptyList(); } } Monday, 11 March, 13 Skygear.io
  • 47. SOLID Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency inversion Principle Monday, 11 March, 13 Skygear.io
  • 48. Single Responsibility Principle LabelBox Cloud Labels Monday, 11 March, 13 Skygear.io
  • 50. Monday, 11 March, 13 Skygear.io
  • 51. Architecture The Lost Years http://www.youtube.com/watch? v=WpkDN78P884 The Web (Rails) is a Delivery Mechanism! Monday, 11 March, 13 Skygear.io
  • 52. Test - a real scenario ... client server workerss3 1. upload a video 2. store in s3 3. start worker and send request to worker 4. dedicated worker pulls the file from s3 and do transcoding Monday, 11 March, 13 Skygear.io
  • 53. Test Remove FEAR during Development You know you break something during testing. For example: Rugby - ~178 tests Refer to Ben’s presentation https://speakerdeck.com/oursky/testing Monday, 11 March, 13 Skygear.io
  • 55. Refactor Title:Refactoring: Improving the Design of Existing Code Author: Martin Fowler Kent Beck John Brant William Opdyke Don Roberts Monday, 11 March, 13 Skygear.io
  • 56. Familiar With your Tools try to familiar with your tools before coding, don’t create messy stuffs by your first impression do testing before adopting to the real code read the API, doc and Google ask others ... Monday, 11 March, 13 Skygear.io
  • 57. Quiz (javascript) The expected output of the following javascript is alert count down from 5 to 0. Explain why it doesn’t work and fix the bug. function count (num) { for (var i = 0; i <= num; i += 1) { setTimeout(function () { alert(num - i); }, i * 1000); } } count(5); Monday, 11 March, 13 Skygear.io
  • 58. Quiz (javascript) function count (num) { for (var i = 0; i <= num; i += 1) { (function (time) { setTimeout(function () { alert(num - time); }, time * 1000); }(i)); } } count(5); Monday, 11 March, 13 Skygear.ioSkygear.io
  • 59. Quiz (javascript) cont. function changeAnchorsToLightBox(anchors) { var length = anchors.length; for (var i = 0; i < length; i++) { anchors[i].onclick = function () { lightBox.open(anchors[i]); return false; }; } } Monday, 11 March, 13 Skygear.io
  • 60. Quiz (javascript) cont. function changeAnchorsToLightBox(anchors) { var length = anchors.length; for (var i = 0; i < length; i++) { (function (anchor) { anchor.onclick = function () { lightBox.open(anchor); return false; }; }(anchors[i])); } } Monday, 11 March, 13 Skygear.io
  • 61. Quiz (javascript) cont. function changeAnchorsToLightBox(anchors) { var length = anchors.length; for (var i = 0; i < length; i++) { (function (anchor) { anchor.onclick = function () { lightBox.open(anchor); return false; }; }(anchors[i])); } } Monday, 11 March, 13 Skygear.io
  • 62. Reference Clean Code: A Handbook of Agile Software Craftsmanship http://www.amazon.com/Clean-Code-Handbook- Software-Craftsmanship/dp/0132350882 The Clean Code Talks - "Global State and Singletons" https://www.youtube.com/watch?v=-FRm3VPhseI Monday, 11 March, 13 Skygear.io
  • 63. Q & A and ... “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” Martin Fowler: Monday, 11 March, 13 Skygear.io
  • 64. Brought to you by Oursky Build your mobile app fast skygear.io (open source)