Quick Intro to Clean Coding

29
Quick intro to Clean Coding Aleksey Drobnych Java Trainer

description

 

Transcript of Quick Intro to Clean Coding

Page 1: Quick Intro to Clean Coding

Quick intro to Clean Coding

Aleksey DrobnychJava Trainer

Page 2: Quick Intro to Clean Coding

● What is bad code● What is good code ● Code smells ● Tests ● Refactoring

What this all about?

Page 3: Quick Intro to Clean Coding

I knew one sales guy who predicted that programming languages and programmers will disappear some time because of power of next gen smart business wizards.

What is programm? This is just set of business receipts written with maximal level of precision and detalisation. It's impossible to do using human language. It's too flexible. We need to invent specific DSL for the system. And OOP is one of possible ways for this purpose.

Foreword

Page 4: Quick Intro to Clean Coding

Bad code

Page 5: Quick Intro to Clean Coding

if (this == null) {

// Logic here }

Page 6: Quick Intro to Clean Coding

switch (number) { case 0: printf("0"); break; case 1: printf("1"); break; case 2: printf("2"); break; case 3: printf("3"); break; case 4: printf("4"); break; case 5: printf("5"); break; case 6: printf("6"); break; case 7: printf("7"); break; case 8: printf("8"); break; case 9: printf("9"); break; }

Page 7: Quick Intro to Clean Coding

public int m_otCalc() {return iThsWkd * iThsRte +

(int) Math.round(0.5 * iThsRte *Math.max(0, iThsWkd - 400)

);}

Page 9: Quick Intro to Clean Coding

Path one● Code that just works (that's easy, man!)● More Code that just works (huh, we'll survive!)● Mmmooorrreee CCoodeee thaaat j juust wooork s● MMMmmmmmmmmmmm mmmmmmmmm● Great Technical Debt

Two ways of Code

Page 10: Quick Intro to Clean Coding

Path two● Code that just works ● Tests● Refinement (finally we've done this spike)● Test ● More Code just enough for Test● Refactoring● Test ● More Code just enough for Test● Refactoring● look mum no hands!

Two ways of Code

Page 11: Quick Intro to Clean Coding

Good Code

Page 12: Quick Intro to Clean Coding

● Code needs to work today just once

Simplisity

Page 13: Quick Intro to Clean Coding

● Code needs to be easy for change forever

Ability to grow

Page 14: Quick Intro to Clean Coding

● Big Upfront Design● We have no time for tests● Just do so as you did before● Don't touch it until it works

Know your daemons

Page 15: Quick Intro to Clean Coding

● Any code can be rewritten● Tests are your safety belt● Leave the campground cleaner

than you found it

Know your angels

Page 16: Quick Intro to Clean Coding

Transparent: The consequence of change are visible and predictableReasonable: The cost of adding a new feature is proportional to its valueUsable: if you already wrote the code, you can reuse itExemplary: More code like this would be good for your app

How to judge the goodness of code

Page 17: Quick Intro to Clean Coding

● Object Oriented Design Patterns● Object Oriented Design Principles● Knowledge of language and Libs● Code Smells ● Tests ● Code Reviews

Clean Code Toolset

Page 18: Quick Intro to Clean Coding

● Object Oriented Design Patterns

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. Patterns are formalized best practices that the programmer must implement themselves in the application.Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.

Clean Code Toolset

Page 19: Quick Intro to Clean Coding

● Object Oriented Design PrinciplesIn computer programming, SOLID (Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion) is a mnemonic acronym introduced by Michael Feathers for the "first five principles" identified by Robert C. Martin in the early 2000s that stands for five basic principles of object-oriented programming and design. The principles when applied together intend to make it more likely that aprogrammer will create a system that is easy to maintain and extend over time.The principles of SOLID are guidelines that can be applied while working on software to remove code smells by causing the programmer to refactor the software's source code until it is both legible and extensible. It is typically used with test-driven development, and is part of an overall strategy of agile and adaptive programming.

Clean Code Toolset

Page 21: Quick Intro to Clean Coding

● Code Smells In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem. Code smells are usually not bugs—they are not technically incorrect and don't currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future.

Clean Code Toolset

Page 22: Quick Intro to Clean Coding

● Tests Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards. Kent Beck, who is credited with having developed or 'rediscovered' the technique, stated in 2003 that TDD encourages simple designs and inspires confidence.

Clean Code Toolset

Page 23: Quick Intro to Clean Coding

● Code Reviews and refactoringsCode refactoring is a "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior", undertaken in order to improve some of the nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility.

Clean Code Toolset

Page 24: Quick Intro to Clean Coding

Small refactorings way:https://github.com/adrobnych/CleanCalc

Live refactoring session

Page 25: Quick Intro to Clean Coding

Quick way: Patterns

Live refactoring session

Page 26: Quick Intro to Clean Coding

Write tests.

If your Classes and Methods are just DSL of the system, then tests is your first and most important way to start talk with silent system. Finally both your tests and the system will talk Perfect Language.

Keep your tests as a grammar for your DSL.

From where I shall start?

Page 27: Quick Intro to Clean Coding

Don't write comments.

85% of developer job is reading of existing code. Keep your code readable, self-explanatory, intentional. Comments are poor deodorant for your code. If you will find yourself writing comment then you can't express your intent in code. This is smell. Refactor.

From where I shall start?