Quick intro to Clean CodingAleksey DrobnychJava Trainer
● What is bad code● What is good code● Code smells● Tests● RefactoringWhat this all about?
I knew one sales guy who predicted that programming languages andprogrammers will disappear some time because of power of next gensmart business wizards.What is programm? This is just set of business receipts written withmaximal level of precision and detalisation. Its impossible to do usinghuman language. Its too flexible. We need to invent specific DSL for thesystem. And OOP is one of possible ways for this purpose.Foreword
Path one● Code that just works (thats easy, man!)● More Code that just works (huh, well survive!)● Mmmooorrreee CCoodeee thaaat j juust wooork s● MMMmmmmmmmmmmm mmmmmmmmm● Great Technical DebtTwo ways of Code
Path two● Code that just works● Tests● Refinement (finally weve 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
● Code needs to be easy forchange foreverAbility to grow
● Big Upfront Design● We have no time for tests● Just do so as you did before● Dont touch it until it worksKnow your daemons
● Any code can be rewritten● Tests are your safety belt● Leave the campground cleanerthan you found itKnow your angels
Transparent: The consequence of change are visible andpredictableReasonable: The cost of adding a new feature isproportional to its valueUsable: if you already wrote the code, you can reuse itExemplary: More code like this would be good for yourappHow to judge the goodnessof code
● Object Oriented Design Patterns● Object Oriented Design Principles● Knowledge of language and Libs● Code Smells● Tests● Code ReviewsClean Code Toolset
● Object Oriented Design PatternsIn software engineering, a design pattern is a general reusable solution toa commonly occurring problem within a given context in software design. Adesign pattern is not a finished design that can be transformed directly intosource or machine code. It is a description or template for how to solve aproblem that can be used in many different situations. Patterns areformalized best practices that the programmer must implement themselvesin the application.Object-oriented design patterns typically showrelationships and interactions between classes or objects, withoutspecifying the final application classes or objects that are involved.Clean Code Toolset
● 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 fiveprinciples" identified by Robert C. Martin in the early 2000s that stands forfive basic principles of object-oriented programming and design. Theprinciples when applied together intend to make it more likely thataprogrammer will create a system that is easy to maintain and extend overtime.The principles of SOLID are guidelines that can be applied whileworking on software to remove code smells by causing the programmer torefactor the softwares source code until it is both legible and extensible. Itis typically used with test-driven development, and is part of an overallstrategy of agile and adaptive programming.Clean Code Toolset
● Knowledge of language and LibsList of Efective Java unitsClean Code Toolset
● Code SmellsIn computer programming, code smell is any symptom in the source codeof a program that possibly indicates a deeper problem. Code smells areusually not bugs—they are not technically incorrect and dont currentlyprevent the program from functioning. Instead, they indicate weaknesses indesign that may be slowing down development or increasing the risk ofbugs or failures in the future.Clean Code Toolset
● TestsTest-driven development (TDD) is a software development process thatrelies on the repetition of a very short development cycle: first thedeveloper writes an (initially failing) automated test case that defines adesired improvement or new function, then produces the minimum amountof code to pass that test, and finally refactors the new code to acceptablestandards. Kent Beck, who is credited with having developed orrediscovered the technique, stated in 2003 that TDD encourages simpledesigns and inspires confidence.Clean Code Toolset
● Code Reviews and refactoringsCode refactoring is a "disciplined technique for restructuring an existingbody of code, altering its internal structure without changing its externalbehavior", undertaken in order to improve some of the nonfunctionalattributes of the software. Advantages include improved code readabilityand reduced complexity to improve the maintainability of the source code,as well as a more expressive internal architecture or object model toimprove extensibility.Clean Code Toolset
Small refactorings way:https://github.com/adrobnych/CleanCalcLive refactoring session
Write tests.If your Classes and Methods are just DSL of thesystem, then tests is your first and mostimportant way to start talk with silent system.Finally both your tests and the system will talkPerfect Language.Keep your tests as a grammar for your DSL.From where I shall start?
Dont write comments.85% of developer job is reading of existing code.Keep your code readable, self-explanatory,intentional. Comments are poor deodorant foryour code. If you will find yourself writingcomment then you cant express your intent incode. This is smell. Refactor.From where I shall start?