Object Oriented Design Principles Learning Resources

Object Oriented Design Principles Learning Resources

·

11 min read

In this quick note I'll share some resources for learning all the 5 OOD principles by Uncle Bob.

This article might not be complete. :)

  1. KISS (Keep It Simple Stupid)
  2. DRY (Don't Repeat Yourself)
  3. WET (Write Everything Twice) {Opposite to DRY}
  4. YAGNI (You Ain't Gonna Need It)
  5. SOLID

KISS (Keep It Simple Stupid)

Keep it simple, do not apply circus.

According to this principle,
Simplicity should be a key goal in design. In programming, unnecessary complexity should be avoided. Simple systems work best than a complex one. A solution that follows the KISS principle might look dull or even “stupid” but simple and understandable.

Nowadays programming languages, frameworks, and APIs have powerful means to create sophisticated solutions for even simple kinds of problems. Developers might feel tempted to write “clever” solutions that use all these complex features and this may lead to complexities later.

KISS principle states we should not use the fancy features from the programming languages only because the language lets us use them. We should use such features only when there are perceptible benefits to the problem we are solving.

Advantages

Simple solutions has increased readability, understand-ability, and changeability. Hence are easier to maintain. Specifically when developer is not the maintainer.

Following are the benefits of KISS principle for developers.

– Will be able to solve more problems, faster.
– Will be able to produce code to solve complex problems in fewer lines of code
– Will be able to produce higher quality code
– Will be able to build larger systems, easier to maintain
– Code base will be more flexible, easier to extend, modify or refactor when new requirements arrive
– Will be able to work in large development groups and large projects since all the code is stupid simple

How to Achieve the KISS principle

Keep simple: Try to write the simplest code.
Find alternatives: Think of alternative solutions to your problem, choose the best one, and transform that into your code.
Decompose code: Wherever there is lengthy code, divide that into multiple methods.
Single block Single task: Try to write small blocks of code, which do a single task.

Resources:

Wikipedia

DRY (Don't Repeat Yourself)

Do not Repeat Yourself

According to this principle,
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
Have Single Source of Truth.
Where “duplication” is required use code generation. E.g. Table in Database from one place, maybe ER diagram
Practice: Forget that ctrl-c ctrl-vexists when coding
Use Refactoring to remove duplication in code.
Once And Only Once programming Vs Clone And Modify Programming

Duplication of code is the root cause of most of the maintenance problems, poor factoring, and logical contradictions. It is also a waste, waste of effort and time .

According to DRY, in an application, we should write a piece of logic once only .

How to Achieve DRY Principle

· Divide and Reuse: Divide your system into pieces. Ask yourself a question “am I going to do this again?” If yes, make it a reusable unit or method. Use this method by calling it wherever required. Thus, you will divide your code into smaller units and reuse.

· Keep Short: Keep methods short, divide logic and try to use the existing piece of code in your method.

Advantages of DRY Principle

Maintainability: Ensures easy maintenance. There is no repeat of code, hence change in single place works for multiple instances.
Readability: Code is more structured hence increases the readability.
Re-usability: DRY promotes reuse of code. We write a single block of code or method to replace two or more instances of repeating code and reuse the same block or method for multiple instances.
Cost Effective: DRY insists on minimized repetition and maximized reuse, which promotes, less time to develop, debug and maintain. Thus saves human effort and time.
Test-ability: If there are more paths or functions for a single behavior in code, more number of test cases are required. If code is without repetition, we have to test only one for single behavior.

Disadvantages of DRY

Implementation of DRY without proper care may make the code complex and make difficult to read and understand. One good example of the DRY principle is the helper class in enterprise libraries, in which every piece of code is unique in the libraries and helper classes.

Resources

Wikipedia

YAGNI (You Ain't Gonna Need It)

As per this principle

• Do the simplest thing that will work
• Do not implement a feature/functionality until it is deemed necessary. E.g. caching
• Extreme Code tweaking for Performance Tuning.

YAGNI is a principle of extreme programming (XP) .This principle promotes to ensure the following quote by XP co-founder Ron Jeffries “Always implement things when you actually need them, never when you just foresee that you may need them.” As per this principle, we should not implement anything, which is not required now. This minimizes the amount of unnecessary work, which is a great way to improve developer productivity and product simplicity.

Advantages of YAGNI

• Facilitates Agile Implementation: This principle becomes important in projects being executing the agile methodology and projects requiring tight schedule. In Agile projects, team will have a release plan and a product backlog having list of user stories. Team has to complete each User Story in a specific sprint or iteration. Under the current sprint plan, team member will working on a task or functionality having an estimated effort that the member has signed up for, in support of one of the iteration's User Stories.

Resources

Wikipedia

SOLID

SOLID Design Principle

Robert C. Martin in the early 2000s defined five design principles for Object Oriented Programming. Later, Michael Feathers introduced SOLID principles acronym for these five principles. These principles are essential for building robust working software.

The five principles of the acronym SOLID are as follows.

• Single Responsibility Principle – Do One Thing At all levels (infrastructure, application, module, class, method, and attribute, variable). The objective of this principle is to reduce coupling and increase cohesion.

• Open Closed Principle - Open for Extension closed for modification means we should be able to modify the behaviour of any entity by extending it but never by modifying its source code. We should write a class or module in such a way that it could be used as it is or can be extended if needed, but it can never be modified.

• Liskov Substitution Principle - Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.

In case of OOP, it means, the child class should be able to substitute parent classes easily without disturbing the application.

• Interface Segregation (Loose Coupling) Principle - Every client should use an interface instead of actual implementation. Many client interfaces are better than one general-purpose interface. ISP says, we should avoid fat interfaces. A fat interface, an interface with additional useless methods lead to unintentional coupling between classes.

• Dependency Inversion Principle - High-level modules should not depend on low-level modules, rather both should depend on abstraction. Abstraction should not depend on details; rather detail should depend on abstraction. This principle is primarily concerned with reducing dependencies amongst the code modules and it helps in understanding how we can integrate the system together correctly.

Advantages of SOLID

• Helps design systems that are robust
• Helps design systems that are easy to debug
• Helps design systems that are flexible
• Helps design systems that exhibit ease of maintenance
• Helps design system that do not cost much to maintain
• Helps design systems that are easily, cheaply and quickly extensible
• Helps design systems that are scalable

As per good programming practices, it is better to minimize coupling and increase cohesion in codes. The five SOLID principles help us in ensuring the same in our code.

Resources

Wikipedia Article Video