Clean Code
What is code?
Specifying requirements in such detail that a
machine can execute them is programming.
Such specification is code.
What is clean code?
❏ Elegant
❏ Efficient
❏ Focused
❏ Readable
❏ It should do only one thing and do it well.
What is bad code?
❏ Code which doesn't have above mentioned traits is bad code.
- Boy scout rule
Heuristics and Smell
❏ Naming
❏ Functions
❏ Comments
❏ Formatting
❏ Object and Data Structure
❏ Error Handling
Name should be
❏ Intention Revealing
❏ Avoids Disinformation
❏ Pronounceable
❏ Class names should be noun or noun phrase
❏ Method name should be verb or verb phrase
❏ Uniformity should be maintained while assigning a name to specific
concept.
Methods
❏ Methods should be small. If there are two many process , it should be
divided in more functions.
❏ Do one thing only and do it well
❏ Use descriptive names
❏ Should have less number of arguments.(i.e ideally no argument
should be passed)
❏ Method should not be repeated (Ruby follows DRY)
Comments
❏ Comments are inability to express through code.
❏ Comments Do Not Make Up for Bad Code
❏ Comments should be
➢ Avoided
➢ updated regularly
➢ Should not be misleading
Formatting
The Purpose of Formatting
❏ It provides systematic and rhythmic approach to the code.
❏ It increases readability and hence is helpful in maintaining code.
Types
1. Vertical Formatting(line of codes in a file)
2. Horizontal Formatting(length of the line)
Objects and Data Structures
The Law of Demeter state
❏ Method can call other methods in its class directly
❏ Method can call methods on its own fields directly (but not on the fields'
fields)
❏ When method takes parameters, they can call methods on those
parameters directly.
❏ When method creates local objects, they can call methods on the local
objects.
Objects and Data Structures...
❏ One should not call methods on a global object (but it can be passed
as a parameter)
❏ One should not have a chain of messages a.getB().getC().doSomething() in
some class other than a's class.
Error Handling
❏ Use Exceptions
❏ Provide Context with Exceptions
❏ Don’t Pass Null
References
❏ https://www.ufm.edu/images/0/04/Clean_Code.pdf
❏ http://blog.bbv.ch/wp-content/uploads/2013/06/Clean-Code-V2.1.pdf
Thank You

Clean code

  • 1.
  • 2.
    What is code? Specifyingrequirements in such detail that a machine can execute them is programming. Such specification is code.
  • 3.
    What is cleancode? ❏ Elegant ❏ Efficient ❏ Focused ❏ Readable ❏ It should do only one thing and do it well. What is bad code? ❏ Code which doesn't have above mentioned traits is bad code. - Boy scout rule
  • 4.
    Heuristics and Smell ❏Naming ❏ Functions ❏ Comments ❏ Formatting ❏ Object and Data Structure ❏ Error Handling
  • 5.
    Name should be ❏Intention Revealing ❏ Avoids Disinformation ❏ Pronounceable ❏ Class names should be noun or noun phrase ❏ Method name should be verb or verb phrase ❏ Uniformity should be maintained while assigning a name to specific concept.
  • 6.
    Methods ❏ Methods shouldbe small. If there are two many process , it should be divided in more functions. ❏ Do one thing only and do it well ❏ Use descriptive names ❏ Should have less number of arguments.(i.e ideally no argument should be passed) ❏ Method should not be repeated (Ruby follows DRY)
  • 7.
    Comments ❏ Comments areinability to express through code. ❏ Comments Do Not Make Up for Bad Code ❏ Comments should be ➢ Avoided ➢ updated regularly ➢ Should not be misleading
  • 8.
    Formatting The Purpose ofFormatting ❏ It provides systematic and rhythmic approach to the code. ❏ It increases readability and hence is helpful in maintaining code. Types 1. Vertical Formatting(line of codes in a file) 2. Horizontal Formatting(length of the line)
  • 9.
    Objects and DataStructures The Law of Demeter state ❏ Method can call other methods in its class directly ❏ Method can call methods on its own fields directly (but not on the fields' fields) ❏ When method takes parameters, they can call methods on those parameters directly. ❏ When method creates local objects, they can call methods on the local objects.
  • 10.
    Objects and DataStructures... ❏ One should not call methods on a global object (but it can be passed as a parameter) ❏ One should not have a chain of messages a.getB().getC().doSomething() in some class other than a's class.
  • 11.
    Error Handling ❏ UseExceptions ❏ Provide Context with Exceptions ❏ Don’t Pass Null
  • 12.
  • 13.