Clean code is subjective and every developer has a personal take on it. There are some ideas that are considered best practice and what constitutes clean code within the industry and community, but there is no definitive distinction.
7. Meaningful Names
● Variables, functions, class names should
answer all the big questions:
~ why it exists?
~ what it does?
~ how it is used?
8. ● If a name requires a comment, then the name
doesn’t reveal its intent.
int d; // elapsed time In days
● Avoid Disinformation
- don’t use type information in names
personList
9. ~> Don’t be afraid to make a name long. A long descriptive name
is better than a short enigmatic name.
~> A long descriptive name is better than a long descriptive
comment.
17. OBJECTS
● Hide data and expose functions
● Easy to add new objects
● Hard to add new behaviors
18.
19. DATA STRUCTURES
● Expose data and have no meaningful functions
● Easy to add new behaviors
● Hard to add new data structures
Choose the approach that is best for the job.
22. The Law of Demeter
● It says our function should only access the
classes/objects that it has direct access to
which are:
i. Objects in class parameter
ii. Object in function parameter
iii. Object in class members
iv. Objects created inside function body.
23. Examples
● In short, the Law of Demeter aims to keep you from
doing things like this:
objectA.getObjectB().doSomething();
or even worse, this:
objectA.getObjectB().getObjectC().doSomething();