Clean code: SOLID
Maksym Husar
Mobile Team Lead, Indeema Software Inc.
Why do I need it?
The main reason why
Be relaxed. Keep calm.
One more reason why
First steps to Clean Code
DRY - «Don't repeat yourself»
KISS - «Keep it short and simple»
YAGNI - «You aren't gonna need it»
SOLID
SOLID principles
Why do I need SOLID?
It works fine without it...
SOLID as a life saver for the main problems of
a bad architecture:
● Fragility
A change may break unexpected parts — it is very difficult to detect if you
don’t have a good test coverage.
● Immobility
A component is difficult to reuse in another project—or in multiple places
of the same project—because it has too many coupled dependencies.
● Rigidity
A change requires a lot of efforts because it affects several parts of the
project.
Single responsibility principle
There should never be more than one reason for a class to change
Every time you create/change a class, you should ask yourself:
How many responsibilities does this class have?
==>
Single responsibility principle
Single responsibility principle
==>
Command-line script for showing the number of lines in a project:
find . -type f -exec wc -l {} + | sort -n
Basic check on clean code issues
Single responsibility principle
App Delegate is the most popular violator of the SRP principle.
Single responsibility principle
One of the possible ways to solve AppDelegate overloading is to use
Composite Design Pattern*:
* Highly recommend to
read: Refactoring
Massive App Delegate
Open/Closed principle
Entities (classes, modules, functions, etc.) should be open for extension, but
closed for modification.
● Open for extension: You should be able to extend or change the
behaviour of a class without efforts.
● Closed for modification: You must extend a class without changing the
implementation.
Open/Closed principle
Open/Closed principle
==>
*Decorator Design pattern is mainly focused on Open/Closed Principle.
Liskov substitution principle
Functions that use pointers on base classes must be able to use objects of
derived classes without knowing it.
To avoid violating this principle, the following restrictions must be applied:
● Preconditions cannot be strengthened in the subclass
● Postconditions cannot be weakened in the subclass
Liskov substitution principle. Bad inheritance example.
==>
Liskov substitution principle. Condition example.
Interface segregation principle
● "Many specialized interfaces are better than one universal"
or
● "Clients should not depend on methods that they do not use."
Interface segregation principle
Dependency inversion principle
High-level modules should not depend on low-level
modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should
depend on abstractions.
Module - a logically interconnected set of functional elements.
Dependency inversion principle
Change in Е
Arrows show the direction of the relationship between the
modules.
Dependency inversion principle
Depend on abstractions,
not on details.
DataHandler
FilesystemStorage
Dependency inversion principle
Dependency inversion principle DataHandler
Storage
FilesystemStorage DropboxCloudStorage
Dependency inversion principle. Bad example.
Dependency inversion principle. Good example.
Obj-C & Swift Dependencies Visualizer
https://github.com/PaulTaykalo/
objc-dependency-visualizer
Most popular Dependency injection libraries
Swinject - https://github.com/Swinject/Swinject
Cleanse - https://github.com/square/Cleanse
Needle - https://github.com/uber/needle
Single responsibility - make modules smaller / simpler
Open/Closed - make modules extendable
Liskov substitution - inherit correctly
Interface segregation - split interfaces/protocols
Dependency inversion - use interfaces/protocols
Summary
https://www.scaledrone.com/blog/solid-principles-for-becoming-a-better-ios-
swift-developer/
https://marcosantadev.com/solid-principles-applied-swift/
https://medium.com/swift-india/solid-principles-part-1-single-responsibility-
ccfd4ff34a5a
https://www.vadimbulavin.com/refactoring-massive-app-delegate/
https://www.vadimbulavin.com/dependency-injection-in-swift/
Useful links

Clean code: SOLID