Object Oriented Design Principles
Ashok Athukuri
Software Architect | Linux | IoT | Cloud | Networking | Docker | Kubernetes | Embedded Software
Here are few Design Principles should be followed in Object Oriented programming, These are not specific to any programming language.
- STUPID
- DRY
- YAGNI
- High Cohesion & Low Coupling
- Inversion of Control
- Dependency Injection
- SOLID principles
- SLAP your methods
- Anti-Patterns
1. STUPID is a set of principles to avoid while writing code
Singleton
Tight Coupling
Untestability
Premature Optimization
Indescriptive Naming
Duplication
2. DRY ( Don’t Repeat Yourself)
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
The DRY principle states that these small pieces of knowledge may only occur exactly once in your entire system.
example: Lot of copy-paste code found in most of the source code
3. YAGNI (You Aren't Gonna Need It)
Always implement things when you actually need them, never when you just foresee that you need them.
4. High on Cohesion, Low on Coupling
Cohesion is an indication of how related and focused the responsibilities of an software element are.
Cohesion is the indication of the relationship within module.
- Readability
- Maintainability
- Re-usability
Coupling refers to how strongly a software element is connected to other elements.
Coupling is the indication of the relationships between modules.
5. Inversion of Control (IoC)
Inversion of control refers to the inversion of the flow of control (the order in which individual statements, function calls, etc. are executed) in a software.
“Inversion of Control is an abstract principal describing an aspect of some software architecture design in which the flow of control of a system is inverted in comparison to procedural programming.”
IoC is also known as "Hollywood Principle" with its essence in the statement "Don't call us, we'll call you“
6. Dependency Injection
- It eliminates tight coupling between objects.
- Makes objects and application more flexible.
- It facilitates creating more loosely coupled objects and their dependencies.
7. S.O.L.I.D principle
SOLID is very popular and widely followed design principles
Single Responsibility Principle
–Every Class should have single responsibility, there should never be more than one reason for a class to change
Open/Closed Principle
–Every Software Entity should be open for extension but closed for modification
LiskovSubstitution Principle
–Objects should always be replaceable with instances of their subtypes without altering their correctness
Interface Segregation Principle
–Many client specific interfaces are better than one general purpose interface, this leads to loose coupling and high cohesion
Dependency Inversion Principle
–Abstraction should not depend upon details, details should depend upon
8. SLAP (Single Level of Abstraction Principles)
SLAP your methods example:
9. Anti-Patterns
- An anti-pattern is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive.
- An anti-pattern is a way of not solving a problem. But there is more to it: it is also a way that can frequently be seen in attempts to solve the problem.
- A pattern is an idea of how to solve a problem of some class. An anti-pattern is an idea of how not to solve it because implementing that idea would result in bad design.
Examples of Anti-Patterns:
?God Class/Object -Does everything in One Object
?Copy-Paste programming -Repeated copy paste code
?Spaghetti Code
?Magic Numbers and Strings
good read for anti-patterns: https://sahandsaba.com/nine-anti-patterns-every-programmer-should-be-aware-of-with-examples.html
Heading IoT Device Platform and Edge-Cloud Connectivity | Data Acquisition & Device Management Platform | next-gen Edge Devices | Digital Transformation essentials.
5 年Nice capture! Thank you.
Ingenieur développement logiciel embarqué expert
5 年perfect pointers on how? to save? post programming time. looking forward for more articles .
Product Design & Development for Industrial Automation.
5 年Please drop these theories and start actual work ??