Clean Code

RajeshKumar987 20 views 21 slides Nov 16, 2016
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

Clean Code


Slide Content

Clean Code Rajesh Kumar 1

What is Clean Code I like my code to be elegant and efficient , The logic should be straightforward to make it hard for bugs to hide, T he dependencies minimal to ease maintenance, E rror handling is complete, Clean code does one thing well . Bjarne Stroustrup 2

Meaningful Names int i ; What is i ? i nt elapsedTime ; Now we know it is an integer indicating elapsed time. But what time? Seconds, years what? int elapsedTimeInDays Better? int elapsedTimeInDaysSinceRestart 3

Meaningful Names controllerForEfficientHandlingOfStrings controllerForEfficientStorageOfStrings Frightfully similar int a = l;    if ( O == 0 ){      a = O ; } Not very clear? 4

Meaningful Names Names with similar meaning accountData vs account customerInfo vs customer Variable names should be pronounceable dtValFmtddmmyy dateValueFormattedAsddmmyy Use searchable names If you have to search a variable named a  5

No magic numbers public List< int []> getList (List< int []> inputList ) { List< int []> tempList = new ArrayList <>(); for ( int [] x : inputList ) if (x[0] == 4) tempList.add (x); return tempList ; } 6

No magic numbers contd public List< int []> getFlaggedCells (List< int []> gameBoard ) { List< int []> flaggedCells = new ArrayList <>(); for ( int [] cell : gameBoard ) if (cell[ STATUS_VALUE ] == FLAGGED ) flaggedCells.add (cell); return flaggedCells ; } 7

Methods Method name must be verb One name per concept Fetch, retrieve, get Do not add too much context Prefixing everything with application name Must always return value Should not have side effects 8

METHODS Contd Keep methods small Make sure it does one thing Niladic, monadic and dyadic Triadic is so evil, try enclosing in a class if really required So are flag arguments, It means method does more than one thing 9

Functions contd Command query separation Method should do something or answer something but not both Prefer exceptions to returning error codes Violate the command query separation The error codes class is dependency magnet 10

WordS of Wisdom Every function, and every block within a function, should have one entry and one exit. Means that there should only be one  return statement in a function, no  break or continue statements in a loop, and never,  ever,  any goto statements . Dijkstra 11

COMMENTS What would you prefer? // Check if employee is eligible for full benefits if ( employee.flag && employee.age > 65) Or if ( employee.isEligibleForFullBenefits ()) 12

COMMENTS Do not comment bad code, rewrite it, refactor it Kernighan If your code can not reveal its intention then the comments shall neither. The comments are seldom maintained Comments clutter the code TODO comments 13

FORMATTING Is as important as the code itself Should be standardized for a project The old Hollerith limit of 80 is a bit  old 14

Abstraction Do not blindly create getters and setters Understand when to use Procedural Object oriented DTOs do nothing more than to duplicate code 15

ERROR HANDLING Use informative error messages Do not catch and re-throw Use exceptions rather than error codes Write try-catch-finally in separate function? Checked vs Unchecked exceptions? Violates open closed principle 16

Tests F.I.R.S.T. principle Tests must be uniquely named Given-When-Then One assert per test Okay to make a function protected to test Generally not required if the functions are small 17

CLASSES Single R esponsibility Principle Too many classes Look for cohesion Dependency Inversion Principle Enums vs constants Do not inherit constants 18

SYSTEM Object creation should be separated from business logic Dependency Injection, Factories Cross cutting concerns Business logic should be separate from framework related code 19

SYSTEM Always delete commented code And dead code Building and deploying a system should be one step Should be able to run all tests on local Do not override safeties Maintain separation between layers 20

Questions? 21