Beautiful Code
“Programs must be written for people to read, and only incidentally for machines to execute.”
Aleksandar Radulovic / a13x@spotify.com / @a13xnet
Who am I?
Aleksandar Radulovic
Backend Team Lead @ Spotify
Twitter: @a13xnet
Email: a13x@spotify.com
Why talk about this?
We read code all the time
Code written, then rewritten again (and again)
Back to basics!
There are no examples in this presentation!
What is ugly code?
Code you wrote x months ago
Difficult to read and understand
Code you're afraid to modify
So, what is beautiful code?
Beauty is in the eye of the beholder.
Code you are proud of!
Easy to read and understand
Next maintainer will thank you
Your future self will thank you
How?
Many techniques and tools
Discipline
Practice
Read bunch of books
Read bunch of code!
Write bunch of code!
Simple stuff
Code style
Good and consistent naming
Small (clean) methods
Command Query Separation
Simple stuff
Sometimes a function is better than a class
Language Idioms
Avoid complexity you don't need!
Intermediate
Modular design
Refactoring
SOLID principles in OOP
Modular design
Locality of change
Loosely coupled
Cohesive
SMALL!
Refactoring
Code improvement technique
Reduce complexity
Improve readability
Refactoring Mantra
Rule of Three
Hard
Test Driven Design
Working on Legacy Code
Why is TDD hard?
Different approach
Discipline and practice
Why is TDD great?
Promotes modular design
Maximizes throughput, minimizes defects
Reduces feedback loop
Minimizes duplication
Maximizes clarity
Why is TDD great?
Well designed systems
Isolated code
Confidence
Live documentation!
NO debugging!
TDD in a slide
Write a failing test
Enough code to pass the test
Refactor
Repeat
NEVER refactor when tests are broken!
Elements of a test
Arrange
Act
Assert
How (and when) to use TDD
Just do it®
New features
Don't think too far ahead
Ping-Pong Pair Programming
When not to do TDD?
No client
Experimentation
Shallow code
Spikes
Final words
Think of code as poems
Share knowledge
Share code
The
beginning
Journey starts here
Books and materials