Taking care of
clean code
Clean Code training.
Developer’s effort
2%
13% Understanding Code
Modifying Existing
85% Code
Writing New Code
2
Why Clean Code matters
1 READ FAST
2 CHEAP CHANGE
3 LESS EXPENSIVE
4 LESS COMPLEXITY
5 LESS BUGS
6 PROFESSIONAL
3
Keeping the code quality
1 TEAM COMMITMENT
2 REGULAR ACTIVITY
3 STANDARDS
4 PRACTICES
5 TOOLS
4
Code convention
Clean Code training.
Naming variables
Clean Code training.
Naming variables
USE AV O I D
• Follow code convention • Magic numbers
• Use comprehensive names • Hungarian notation
• 1 and 0 harcoded • Abbreviations
• Java convention: • x, y, z or any one-letter apart i/j
i and j allowed for loops
•
• file2, sum1
• ALL_CAPS – constant
• ClassFromCapital
• Unclear “flag”, “result”, “status”
• methodOrVariable
7
Naming classes
Clean Code training.
Naming classes
USE AV O I D
• Noun • God class/ Magic Hammer
• Use comprehensive names • Data storage
• Abbreviations
• Unclear “Instance”, “Entity”
• Numbers like “Test2”
• “MyClass”
9
Naming
functions
Clean Code training.
Naming functions
USE AV O I D
• Verb • Several actions in one method
• Use long names • Unclear “check…”, “verify”, “try…”
• void get(), String print() etc.
• Lot of parameters
11
Comments
Clean Code training.
Comments
USE AV O I D
• Legal (license) • /* */
• “Javadoc” for libraries • // TODO
• Intention for solution made • Obvious statement
• // for,if,try,while close
• Commented code
• “All methods should have comment”
13
Excess code
Clean Code training.
Excess code
USE AV O I D
• Direct return • [Link]()
• Ternary • Comparing Boolean with boolean
• Variable just for return
15
Exceptions
Clean Code training.
Exceptions
USE AV O I D
• Use them • Empty catch
• Throwing default exceptions like “Exception”
17
Tests
Clean Code training.
Tests
USE AV O I D
• Must have assert • Test data in test name
• One test – one assert • loops/conditional statements/lambdas
• Before/After • Strict tests order
• Comprehensive exception name • Tests dependency
19
Design principles
Clean Code training.
Design principles
USE AV O I D
• KISS • Blind copy-pasting
• DRY • Complexity
• Write in main, then split into classes
• “I’ll refactor it later”
21
Tools
Clean Code training.
Tools
USE
• Code Convention
• Code Review
• Static Code Analyzers
23
Persona
Clean Code training.
Persona
USE
• Be Engineer not Coder
• Read professional books/blogs
• Talk with mature developers
• Be Honest
• Be Disciplined
25
Check list
& summary
Clean Code training.
Summary
• Engineer reads more code than writes
• Naming is extremely important for variables, classes, functions
• Use comments only were it’s necessary
• Avoid complexity in code and tests
• KISS and DRY
• Throw custom Exceptions
• Tools like static code analyzers and code coverage trackers can help may help
• Be Engineer not Coder
• Read books
27
Extras
Version Control with Git. DevTestOps training.
Extras
EXTRAS READ MORE
• SOLID • Code Complete by Steve McConnell
• Law of Demeter • Clean Code Robert Martin
• Multi-threading
• Refactoring
• IoC
• Design Patterns
• Anti-patterns
29