Design Principles & Patterns (S.O.L.I.D Principles)
Design Principles & Patterns (S.O.L.I.D Principles)
What is Design
Design is a conceptual model of any process, product or object. We convert the
design into implementation and build the product.
Examples:
Software Design
If we consider the software as a product or object, the plan we will follow to build
it will be the design.
Software development has a life cycle or process. We have to design it. Again, we
need a design for the software or the product we will build. Thus, the software
design has two parts-
Process design
- The software development life cycle which plans the activities such as
selection of methodology, project execution plan, selection of tools,
documentation standards etc.
Product design.
Programing in small
Programming in large
Most real world software systems are large scale and complex, are of millions of
lines of code, divided into several classes, and distribute among many people,
expanded by many revisions. In such cases, individual style of writing program is
not applicable. Developers have to think of software systems at a higher level than
just coding, as it is not enough to make the code just work correctly. We have to
write the code in a way that makes it easy to develop and maintain further, hence
we have to follow a standard to specify some main principles and there comes
design principles.
We can write the code for the given requirement in two different forms using C
programming language
Solution 1:
int main()
scanf("%d",&day);
printf("\nAn invalid value for day. Give a value between 1 - 7\n\n");
else
{
switch(day)
{
case 1:
break;
case 2:
printf("It is Tuesday\n\n");
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
}
}
Solution 2:
In this approach , we will use array instead of the switch case construct.
#include
int main()
{
int day;
char
*daysOfWeek[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","
Saturday","Sunday"};
printf("Enter a number ");
scanf("%d",&day);
if (day<1 || day>7)
printf("\nAn invalid value for day. Give a value between 1 -
7\n\n");
else
printf("This is %s\n",daysOfWeek[day-1]);
}
If we compare the above two solutions, the second solution, seems to be compact
in terms of lines of code. In case of the first solution approach, Instead of days of
week, if we have to consider Months in an year or some other scenario like this ,
the cases in switch case construct will be more and if we have add some more
conditions later to support change requirement, we may have to add some more
lines of code. That means Contrary to that, in case of the second solution approach,
there will not be any change in the number of lines of statements, as simply we
have to increase the size of the array and add the additional elements in the array.
Hence, in this case the second solution approach is implementation of KISS.
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
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.
Reusability: 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.
Testability: 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.
YAGNI Design Principle
• You
• Aren’t/Ain’t
• Gonna
• Need
• It
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.
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 softwares.
• 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.
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
In different design principles discussed above, we have seen that there are some
common benefits of following them like readability, maintainability, scalability
etc. A programmer can achieve them through implementation of some basic
programming practices like, Top down approach, bottom up approach, structured
programming, information hiding, cohesion, coupling etc. Hence, it is important to
understand these basic programming practices. In addition, it is important to know
how to implement them to develop a robust program.
Top down Programming: In this approach, design takes the whole software
system as one entity and then decomposes it to achieve more than one sub-system
or component based on some characteristics. Each sub-system treated as a system,
is decomposed further. This process keeps on running until we achieve the lowest
level of system in the top-down hierarchy. We will start with a generalized model
of the system and keep on defining the more specific part of it.
The advantages of top down approach are fixed style, clarity, and productivity,
ease of testing and maintenance and ease of redesign. Structured programming
follows top down approach.
Bottom up Programming: In this approach, we will start with the most specific
and basic components and proceed composing the higher level of components until
the desired system is not built as single component. Here, the program modules are
more general and reusable than top-down programming. Thus, it is easier to add
new features
in a bottom-up approach than in a top-down programming approach.
Structured Programming
A computer program comprises of some basic structures like sequences, decisions,
and loops. Structured programming generally makes use of top-down design.
Therefore, during top down decomposition, we have to design a program as a top
down hierarchy of modules. The heart of structured programming is the idea of
using only the single
entry and single exit blocks. For every programming constructs such as individual
sequences, decisions and loops and so on so forth, we need to implement single
entry single exit principle.
A primitive statement
S;
Ex: int x=20;
Sequence of Statements
S1;
S2;
S3;
………
……..
Ex:
int number;
printf(“Enter a number “);
scanf(“%d”,number);
int sqr=number*number;
printf(“The square of the number is %d”,sqr);
Conditional Statement
while (condition) do S
Ex:
while (x>10)
{
printf(“%d “,x);
x--;
}
All these statements are single entry single exit, even the primitive statements like
assignments and sequences.
A primitive statement
x=10;
There is one entry and one exit for the block having one statement..
A repetitive statement – while – do loop
A repetitive statement – for loop of C programming language
A loop to print 1 to 10.
As seen in the above flow charts, we can construct a statement with single entry
and single exit. Finally, we can club together all these statements to build bigger
blocks. We can also build functions with function bodies without using ‘Goto’
statement and rely on the basic structured program principle.
Disadvantages of GOTO:
We should use GOTO only for low level or machine level programming. For
example, in machine language, we might use a ‘Goto’ or equivalent ‘jump’ to jump
around to a specific label or to a specific memory address.
{
int age =0;
printf(“Enter your age”);
scanf(“%d”,age);
if (age>=65) goto label1;
if (age>=20) goto label2;
if (age>=13) goto label3;
if (age>=5) goto label4;
printf(“You are minor”);
goto end;
label1: printf(“You are Senior citizen”);
goto end;
label2: printf(“You are an Adult”);
goto end;
label3:printf(“You are a teen”);
goto end;
label4:printf(“You are a child”);
goto end;
end: printf(“bye”);
}
This is not a structured code. We can rewrite the same code in a more structured
way using if - then-else single entry single exit construct.
int main()
{
int age =0;
printf(“Enter your age”);
scanf(“%d”,age);
if (age>=65)
In some situations, programmers need to use multiple exit forms to have better
clarity in the code. However, multiple entry forms are heavily discouraged.