29
Clean code Better coding practices

Clean code, Better coding practices

Embed Size (px)

DESCRIPTION

Why is there so much emphasis on reading the code rather than writing? Because: Ratio of time spent reading vs. writing code is well over 10:1 So if we are spending 1 hour writing code, on an average we have to spend 10 hours reading some code. Each time we change in logic of some code, add or remove some feature, resolve some bug, understand the context of what is going on, or just use a third party API, what we do? We READ code. At HomeShop18, we strive for clean code.

Citation preview

Page 1: Clean code, Better coding practices

Clean code

Better coding practices

Page 2: Clean code, Better coding practices

What is clean code?

Page 3: Clean code, Better coding practices

Bjarne Stroustrup, inventor of C++Elegant and Efficient

Page 4: Clean code, Better coding practices

"Reading it should make you smile the way a well-crafted music or well-designed car would."

Page 5: Clean code, Better coding practices

Ratio of time spent reading vs. writing code is well over 10:1

Page 6: Clean code, Better coding practices

Dave Thomas- Easy to enhance- Has unit and acceptance tests- Has one way of doing one thing

Page 7: Clean code, Better coding practices

Michael Feathers“Clean code looks like it was written by someone

who cares.”

Page 8: Clean code, Better coding practices

Clean code

(bad code is just like this slide, shows carelessness)

Wyh U should, right clean Code.?

Page 9: Clean code, Better coding practices

80% or more of what we do is called Maintenance.

Page 10: Clean code, Better coding practices

Broken windows.

Page 11: Clean code, Better coding practices

Where to start?NamingMethodsClassesCommentsError handling

Page 12: Clean code, Better coding practices

NamingIt should tell you why it exists, what it does, and

how it is used.

No harm in re-naming if you find better names.

Page 13: Clean code, Better coding practices

Namingpublic List<int[]> getProductList() {

List<int[]> list1 = new ArrayList<int[]>();for (int[] x : theList)

if (x[0] == 4)list1.add(x);

return list1;}

public List<int[]> getFlaggedProducts() {

List<int[]> flaggedProducts = new ArrayList<int[]>();

for (int[] product: inventory)

if (product[STATUS_VALUE] == FLAGGED)

flaggedProducts .add(product);

return flaggedProducts ;

}

Page 14: Clean code, Better coding practices

NamingUse Pronounceable Names

genTimstmp generationTimestamprcrdId recordIdtxtViewRltion textToViewRelation

Page 15: Clean code, Better coding practices

NamingUse searchable names

int realDaysPerIdealWeek = 4;const int WORK_DAYS_PER_WEEK = 5;int sum = 0;for (int j=0; j < NUMBER_OF_TASKS; j++) {int realTaskDays = taskEstimate[j] * realDaysPerIdealWeek;int realTaskWeeks = (realdays / WORK_DAYS_PER_WEEK);sum += realTaskWeeks;}

for (int j=0; j<34; j++) {

s += (t[j]*4)/5;

}

Page 16: Clean code, Better coding practices

Naming

You will probably end up surprising someone

when you rename, just like you might with any

other code improvement. Don’t let it stop you

in your tracks.

-Robert C. Martin

Page 17: Clean code, Better coding practices

MethodsFirst rule: they should be small!

Second rule: they should be smaller than that!!

Page 18: Clean code, Better coding practices

MethodsDo one thing

Page 19: Clean code, Better coding practices

Methods Use descriptive names

Page 20: Clean code, Better coding practices

MethodsUse less number of arguments

Page 21: Clean code, Better coding practices

MethodsHave no side-effects

Page 22: Clean code, Better coding practices

ClassesOrganization

Page 23: Clean code, Better coding practices

Classes Size

Page 24: Clean code, Better coding practices

ClassesSingle Responsibility Principle

Page 25: Clean code, Better coding practices

CommentsExplain yourself in code

Page 26: Clean code, Better coding practices

Error handlingUse exceptions than return codes

Page 27: Clean code, Better coding practices

Error handlingDo not return null

Do not pass null

Page 28: Clean code, Better coding practices

Conclusion“Always leave the ground cleaner than you

found it.”

Page 29: Clean code, Better coding practices

Paramvir Singh, Senior Android Developer,HomeShop18