Docsity

Corso di ingegneria del software - Clean Code, Dispense di Ingegneria del Software

Anteprima parziale del testo

Scarica Corso di ingegneria del software - Clean Code e più Dispense in PDF di Ingegneria del Software solo su Docsity!

Clean code

Good practices for readability and maintainability

Rafael Sousa Herves

Contents 1.Clean code 2.Naming 3.Functions 4.Comments 5.Error handling 6.Classes

What is clean code? Bjarne Stroustrup , inventor of C++ “I like my code to be elegant and efficient.” “The logic should be straightforward to make it hard for bugs to hide.” “Clean code does one thing well.”

What is clean code? Grady Booch , author of Object Oriented Analysis and Design with Applications “Clean code is simple and direct.” “Reads like well-written prose.”

What is clean code? Michael Feathers , author of Working Effectively with Legacy Code “Clean code always looks like it was written by someone who cares.”

What is clean code? Ward Cunningham , inventor of Wiki, inventor of Fit, coinventor of eXtreme Programming. Motive force behind Design Patterns. Smalltalk and OO thought leader. “On clean code, each routine you read turns out to be pretty much what you expected.”

How to measure code quality?

Consequences of bad code

  • (^) The mess grows
  • (^) Productivity lowers
  • (^) Application cannot be maintained → game over

Naming

Naming - Intention revealing names Bad public List< int []> getThem() { List< int []> list1 = new ArrayList< int []>(); for ( int [] x : theList) if (x[ 0 ] == 4 ) list1.add(x); return list1; }

Naming - Pronounceable names class Customer { private Date generationTimestamp ; private Date modificationTimestamp ; private final String recordId = "102" ; }; class DtaRcrd102 { private Date genymdhms ; private Date modymdhms ; private final String pszqint = "102" ; }; Bad Good

Naming - Avoid encodings private String description ; PhoneNumber phone ; private String m_dsc ; PhoneNumber phoneString ; // name not changed when type changed! Bad Good