Classes

Darshan kadu
2 min readOct 11, 2020

Today we will cover Chapter 10 : Classes(from book Clean Code). The purpose of this post is to make a summary of the chapter so that you can revisit it quickly. I strongly recommend to read the chapter entirely, this is just for revision purposes.

Class Organisation :

  1. Java conventions : Classes should start with list of variables(priority : pubic static>private static>private instances ).
  2. Public functions should start with list of variables and put private method right after the public method (stepdown rule)

Classes Should be small:

  1. Classes should not be very large, kinda same as functions. Ambiguous names of classes like Processor or Manager don’t give class the scope or responsibilities.
  2. SRP: The Single Responsibility Principle states that class should have only one reason to change and one responsibility. So whenever we need to change the classes we can ask this question whether all the methods in it belongs to one responsibility. If the class contains 5 method, 3 dealing with username and 2 with address then we can scope the responsibilities and create 2 classes out of this one class.
  3. Cohesion: Classes should have small number of instance variables and each method should use those variables. More the method using the class variable more cohesive it is to the class. Having large number of instance variables and method interacting with them is bad. Also, if we break the functions into smaller one, we can get more instance variables by limiting the arguments we will pass to those private method. We can just breakdowns the class by grouping the method which uses those instance variables. Hence we have now got the new smaller classes with more cohesion with less instance.

Organising for Changes:

  1. Adding changes to the classes makes them susceptible for breaking, hence we should not allow the design to have multiple reasons to change.
  2. OCP: Classes should be open for extension and close for modification i.e. we should break the classes enough that we don’t need to change them in majority of the cases, adding new features can result in adding new classes so that we don’t affect the old classes.

Hope you liked it,
If you have any feedback, please feel free to comment or reply to the email.
If you like this and want to read more, please share this and subscribe Newsletter .
Find me @darshan_kadu

--

--