Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Clean code lecture part I


Published on

PT.BUZOO INDONESIA is No1 Japanese offshore development company in Indonesia.
We are professional of web solution and smartphone apps. We can support Japanese, English and Indonesia.
We are hiring now at

Published in: Technology
  • Be the first to comment

Clean code lecture part I

  1. 1. Introduction to Clean Code & Names part I By Niko Adrianus Yuwono
  2. 2. It is not the language that makes programs appear simple. It is the programmer that make the language appear simple
  3. 3.  Dave Thomas says code can be described as a clean code when : 1. The code is simple & direct. 2. Can be read, and enhanced by a developer other thank it’s original author. 3. It has meaningful names. 4. It provides one way rather than many ways for doing one thing. 5. It has minimal dependencies, which are explicitly defined, and provides a clear and minimal API.
  4. 4.  Code is the core of the application. Code represents the details of the requirement. Code is really the language in which we ultimately express the requirements.  If you’ve been in a team project you have probably been significantly slowed down by someone else’s messy code.
  5. 5.  Only knowing how to recognize clean code from dirty code is not enough because it’s doesn’t mean we know how to write clean code.  In this lecture we’ll do some exercises to help us to know how to write clean code and make it a habbit.
  6. 6.  Names are everywhere in software. We name our variables, our functions, our arguments, classes, and packages. We name our source files and the directories that contain them.  Because we do so much of it, we’d better do it well. What follows are some simple rules for creating good names.
  7. 7.  The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent. int e; // total number of employee
  8. 8.  The name e reveals nothing. We should choose a name that specifies what is being measured and the unit of that measurement:  Another example int totalNumberOfEmployee; int daysSinceLastLogin; String employeeEmailAddress
  9. 9.  This is an example of dirty code public ArrayList<int[]> getThem() { ArrayList<int[]> ArrayList1 = new ArrayList<int[]>(); for (int[] x : globalArrayList) if (x[0] == 4) ArrayList1.add(x); return ArrayList1; }
  10. 10.  Why is it a dirty code? Because we cannot comprehend what’s the author want to express in his code. The code is very implicit. We must ask the author some question like : 1. What kinds of things are in globalArrayList? 2. What is the significance of the 0th subscript of an item in globalArrayList? 3. What is the significance of the value 4? 4. How would I use the list being returned?
  11. 11.  We can rewrite the dirty code to this one: public ArrayList<Member> getDietitianMember() { ArrayList<Member> dietitianMembersList = new ArrayList<Member>(); for (Member member : allMemberList) if (member[TYPE] == DIETITIAN) dietitianMembersList.add(member); return dietitianMembersList; }
  12. 12. public ArrayList<Member> getDietitianMember() { ArrayList<Member> dietitianMembersList = new ArrayList<Member>(); for (Member member : allMemberList) if (member.isDietitian()) dietitianMembersList.add(x); return dietitianMembersList; }
  13. 13.  We should avoid words whose entrenched meanings vary from our intended meaning.  For example do not refer to a grouping of accounts as an accountList unless it’s actually a List.  Beware of using names which vary in small ways. How long does it take to spot the subtle difference between a XYZControllerForEfficientHandlingOfStrings in one module and, somewhere a little more distant, XYZControllerForEfficientStorageOfStrings?
  14. 14.  Usually when we can’t use the same name to refer to two different things in the same scope, we might be tempted to change one name in an bad way like a1 and a2.  It is not sufficient to add number series, even though the compiler is satisfied. If names must be different, then they should also mean something different.
  15. 15.  This function is an example of a noninformative function  The use of variable a1 and a2 didn’t give us clue what is the author intention. public static void copyChars(char a1[], char a2[]) { for (int i = 0; i < a1.length; i++) { a2[i] = a1[i]; } }
  16. 16.  Some other example of indistinguishable name : 1. money and moneyAmount 2. customer and customerInfo 3. accountData and account 4. theMessage and message