Writing better code

Post on 24-Jan-2017

114 views 3 download

Transcript of Writing better code

The exact year that Da Vinci completed the Mona Lisa is unknown, but most agree that he started it in 1503/1504 and worked on it for several years.

He spent years perfecting this painting. Began it in Italy and finished it while living in France.

X-rays and other tests with lasers and infrared light have revealed three earlier versions beneath the surface.

Write

Re-factor

Re-Write

WRITING BETTER CODEby JW

"Make it correct, make it clear, make it concise, make it fast. In that order."

Wes Dyer

But Clear and Concise depends on the complexity…

Complexity

- Can be removed Vs cannot be removed - Can be reduced Vs cannot be reduced - Inherent vs Induced

- Domain Vs Design

How do we hide complexity?

Comments

- Explains What - Can become obsolete - Can confuse - Hides bad code

COUPLING

Coupling (dependency)

is defined as the degree of interdependence between two or more modules.

- build modules that are independent

Reduce OOP Inheritance and favour OOP Composition

COHESION

Cohesionis defined as the degree to which all elements of a module work together as a functional unit.

- favour high cohesion and loose coupling

WHERE TO START…long method…

Why is long method bad

- Hard to read - Hard to understand - Hard to remember

- Hard to test - Hard to debug - Hard to code reuse

- Violates SRP - Cannot be optimised - lacks Cohesion - Many reasons to change

- Hides business rules - Obsolete comment

Lets see an example…

Separation of Data

new QuoteViewExcelModel().countryFeeDetails(getCountryFeeDetails(quoteForm,quoteFeeOutputMap)).countryFeesTotal(getCountryFeesTotal(quoteForm,quoteFeeOutputMap)).x713FeeDetails(get713FeeDetails(quoteForm,quoteFeeOutputMap)).x713FeesTotal(get713FeesTotal(quoteForm,quoteFeeOutputMap)).translationFeeDetails(getTranslationFeeDetails(quoteForm,quoteFeeOutputMap)).translationFeesTotal(getTranslationFeesTotal(quoteForm,quoteFeeOutputMap)).terms(getCompanyName(request));

- Separation of data from view https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

- Builder pattern when many arguments https://en.wikipedia.org/wiki/Builder_pattern

- facilitate declarative style of programming

Declarative style of programming

- Code should read like a story - Less like a puzzle - Should convey the intent - Can be functional - Comment on Why and not on What

Declarative style of programming

Workbook quoteExcelWorkbook = Workbook.Create(workbook).createSheet("SheetName").begin("Quote generation")

.begin("Quote header").cell("A1").rowHeight(Workbook.ROW_HEIGHT_20).val(getCompanyName(request))

.range("A1:F1").merge()

.rBorder(Workbook.BORDER_TOP_BOTTOM, IndexedColors.ROYAL_BLUE, CellStyle.BORDER_THICK)

.cell("A2")

.rowHeight(Workbook.ROW_HEIGHT_20)

.val(getQuoteTitle(quoteForm))

.range("A2:F2").merge()

.cell("A3")

.val(getPatentDetails(quoteForm))

.range("A3:F3").merge()

.rBorder(Workbook.BORDER_TOP_BOTTOM, IndexedColors.ROYAL_BLUE, CellStyle.BORDER_THICK)

.end("Quote header")

Future you should thank you

Focus on

- Maintenance- Understandability

- Embrace declarative style- Keep it short and simple- Single responsibility