AlignMinds Technologies logo

Principles of Writing SOLID Code: A Guide for Beginners

This blog post will help you to understand the basic principles of writing solid code.

Key principles of writing SOLID code

  • Single Responsibility Principle
  • Open/Closed Principle
  • Liskov substitution principle
  • Interface Segregation Principle
  • Dependency Inversion Principle

These are general principles that you want to always have in the back of your mind as you design your software. For the obvious reason, the first five are called SOLID. As in “we write SOLID code”. They are not always cut-and-dried; they are rarely 100% achievable. But active awareness of these issues will help you to avoid common coding errors.

1. Single Responsibility Principle

“A class should have a single responsibility”.

I prefer to think of ‘responsibility’ here as ‘purpose’ or ‘job to do’, but it’s called SRP. This is basically just another statement about modularity. Find the logical units and encapsulate them in a class. Don’t take classes that do some of this and some of that. You’ll get greater re-use of the component since the component wasn’t bundled with several other responsibilities the upstream consumer doesn’t want or need;

An alternative conceptualization of the SRP: A class should have a single reason to change.

This principle refers to the impact of a change of requirements on the code. For example, a class should either talk to the database, or format output for the UI, not both. Classes that deal with the database won’t need to be updated if the UI changes, and vice versa. When requirements change, modification is kept to just those spots directly affected, and these changes don’t have unanticipated consequences elsewhere in classes that whose responsibilities have leaked over into ours.

2. Open/Closed Principle

“Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”

Construct your classes so that requirements changes can be managed by adding code, not by modifying existing code. Once you have written code that works, you should ideally never touch it again, because if you don’t touch it, you can’t break it. If you’re writing nice modular code, you probably have objects being used by other objects all over the place (code reuse = good). If you now break that class, you break everybody who is using it (==bad).

This is a very simple little pizza ordering app. There is a TPizza class with an addTopping method and a computeCost method in this application. In this example, we’re interested in the TPizza class itself.

private void button1_Click(object sender, EventArgs e) {TPizza currPizza = new TPizza(); if (ckOlives.Checked)currPizza.addTopping(ETopping.Olives); if (ckPepperoni.Checked)currPizza.addTopping(ETopping.Pepperoni); if (ckMushrooms.Checked)currPizza.addTopping(ETopping.Mushrooms); int pizzaCost = currPizza.computeCost();lblCost.Text = pizzaCost.ToString();}

Here is the button code. Again, we’re not interested in the interface code, so this is just to show how TPizza is used. So, all very straightforward. Now, let’s look at TPizza class.

enum ETopping { Olives, Pepperoni, Mushrooms } class TPizza{ETopping[] toppingArray; int nToppings; public TPizza(){toppingArray = new ETopping[10]; nToppings = 0; } public void addTopping(ETopping newTopping){toppingArray[nToppings] = newTopping;nToppings++; }

So, it’s got a nice enum type and a nice array of toppings and an addTopping method and all is good.

Here is computeCost method.

public int computeCost() { int totalCost = 15; for (int i = 0; i < nToppings; i++) { switch (toppingArray[i]) { case ETopping.Olives: totalCost += 1; break; case ETopping.Pepperoni: totalCost += 2; break; case ETopping.Mushrooms: totalCost += 2; break; } } return totalCost; } }

This works great. But the problem arises when I want to add cheese topping. I can’t add cheese without modifying existing code in the TPizza class. This gives me a chance to break the TPizza class. It could be worse, of course. If there are other places in the class where we have used the same approach (printing out an invoice, sending instructions to the pizza oven, drawing a picture of the pizza) their code will also have to be changed.

How would you have written that architecture so that you could incorporate the change in requirements (adding cheese topping) by adding code, rather than changing existing code? (Answer=> Use a topping class, who knows its cost and type. Or, if you’re really feeling elaborate, a base topping class and descendants). Not that this failure is also a violation of the SRP. It should not be the job of the TPizza to keep track of the cost of individual toppings.

3. Liskov Substitution Principle

Subclasses should be substitutable for their base class. All members of an inheritance hierarchy should fulfil the same behavioural contract. If they don’t then your “is-a” abstraction is probably wrong.

The LSP helps us to avoid misusing inheritance and consequently running into the problems that result when this occurs. A user of a base class should continue to function properly if any derivative of the base class is passed to it. Failure to follow the LSP almost always leads to problems with the OCP, as you wiggle around coding in special cases to your class family.

void runWildLifeSimulator(TDuck d) { d.swim(); d.quack();; }

runWildLifeSimulator is what is called a ‘consumer’ function of TDuck. That is, it uses a TDuck instance. TDuck has made a contract – it will implement swim, quack and fly. Anybody who wants to be a TDuck needs to implement those, and it should make sense.

Surely, a Rubber Duck is a kind of duck. But because a Rubber Duck doesn’t fly and a TDuck does, this isn’t a good class hierarchy. Think about what you would have to do to the consumer function.

void runWildLifeSimulator(TDuck d) { d.swim(); d.quack(); if (TDuck.getType() != TRubberDucky); }

Why should a function that wants a TDuck has to know anything about THE DESCENDENTS of TDuck?  What if there are other violations in the class hierarchy? A switch statement? And of course, what happens if you need to change something about these ducks.

There is a workaround for this, of course. It is? (Let TRubberDuckie override fly to { do nothing }

There are workarounds for all violations of good design. If you hit the peg with a big enough hammer, it will go into the hole.  But these sorts of problems have been showed to produce rigidity and inflexibility and general goofiness in code. Try to avoid them.

4. Interface Segregation Principle

The dependency of one class to another one should depend on the smallest possible interface. Clients should not be forced to depend on methods they do not use. This one is very closely related to the SRP: don’t stuff everything into one big garbage multipurpose class. Changes to code are isolated to those classes that are logically affected.

interface Imessage { public bool SendSms(String message); public bool SendEmail(String message); }

In this case, all the classes that inherit this interface are forced to write methods for sending email and SMS. If some clients are interested only in emails, the issue arises.

The solution is,

interface Isms { public bool SendSms(String message); } interface Iemail { public bool SendEmail(String message); } interface Imessage : Isms { public bool SendSms(String message); }

5. Dependency Inversion Principle

Program to the most abstract class possible. High-level modules should not depend on (concrete) low-level modules.

Consequences of ignoring these core principles

These core OOAD (Object-oriented analysis and design) principles will help you to write SOLID code. But, what are the consequences of ignoring these principles?

As a result of ignoring these principles, the system will be,

1. Rigid

Changing one part of the code causes or requires a change to many other parts of the code.

2. Fragile

Changes in one part of the code break other parts of the code.

3. Immobile

The components/parts of the code cannot be easily reused, because they are tangled.

 –  Albin Antony

How to Handle NSOperation in Your Mobile App?

Each day at our work consists of a sequence of tasks that fill our working hours. Same way, when you create an application, all the interface components (table views, UI controls, alerts, etc.) are run inside the main thread of your application. At some point in your application, you will want to populate these views with data. And this data can be retrieved from the disk, the web, a database, etc.

The issue that we face when we want to populate or handle a huge amount of data is

‘How would you efficiently load this data into your application interface while still allowing the user to have control of the application without any disturbance?’

Many applications in the app store simply ‘freeze’ while their application data is loaded. This will disappoint the user interaction. The secret to making apps without this problem is to move the unnecessary work (the activities which can take place without user interaction) to the background as possible.

The iOS developer has two options here.

  • Grand Central Dispatch
  • NSOperation

Let me explain about NSOperation.


NSOperationQueue manages the concurrent execution of code operations in Xcode. It acts as a priority queue because operations are executed in a First-In-First-Out manner, with higher-priority (NSOperation.queuePriority) ones getting to the lower-priority ones.


NSOperation is a single unit of work. It’s an abstract class that provides a useful, thread-safe structure for programming.NSOperation will perform network requests, text processing, or any other repeatable long-running task that produces associated state or data.

There are two different operations you can create, which are prebuilt in once which are NSInvocationOperation and NSBlockOperation.


All operations may not be equally important. Setting the queuePriority property will promote or defer an operation in an NSOperationQueue according to the following rankings:


typedef enum : NSInteger { NSOperationQueuePriorityVeryLow = -8, NSOperationQueuePriorityLow = -4, NSOperationQueuePriorityNormal = 0, NSOperationQueuePriorityHigh = 4, NSOperationQueuePriorityVeryHigh = 8 } NSOperationQueuePriority;

The following enumerated values are used to denote the priority of operation. Operations are considered based on priority.


typedef enum : NSInteger { NSQualityOfServiceUserInteractive = 0×21, NSQualityOfServiceUserInitiated = 0×19, NSQualityOfServiceUtility = 0×11, NSQualityOfServiceBackground = 0×09, NSQualityOfServiceDefault = -1 } NSQualityOfService;


NSBlockOperation always executes a block. NSInvocationOperation executes an NSInvocation ( a method defined by selector, target or object).


NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(printNumbers) object:@”operation1″]; operation1.queuePriority = NSOperationQueuePriorityLow; operation1.qualityOfService = NSOperationQualityOfServiceBackground; [myQueue addOperation:operation1]; NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(PrintAlphabets) object:@”operation2″]; operation2.queuePriority = NSOperationQueuePriorityHigh; operation2.qualityOfService = NSOperationQualityOfServiceBackground; [myQueue addOperation:operation2]; -(void)printNumbers { for (int i = 0; i<10; i++) { NSLog(@”%d”,i); } } -(void)PrintAlphabets { for (char a = ‘a’; a <= ‘z’; a++) { NSLog(@”%c”,a); } }













NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^(void){ [self PrintAlphabets]; }]; operation1.queuePriority = NSOperationQueuePriorityLow; operation1.qualityOfService = NSOperationQualityOfServiceBackground; [myQueue addOperation:operation1]; NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^(void){ [self printNumbers]; }]; operation2.queuePriority = NSOperationQueuePriorityHigh; operation2.qualityOfService = NSOperationQualityOfServiceBackground; [myQueue addOperation:operation2];













When an NSOperation completes, it will execute its completionBlock only once. This provides a way to customize the behaviour of operation when used in a model or view controller.

NSOperation *operation = …; operation.completionBlock = ^{ NSLog(“Completed”); }; [[NSOperationQueue mainQueue] addOperation:operation];

I hope the above tips shared based on my experience help you when you need to handle NSOperation in your next project.

Happy Coding!

Bibin Binny Mathew

Stuck In The Middle of A Project? What’s Next?

Do you get stuck with a line of code very often?

Are you someone who always find it difficult to write the next line of code during the development process?

Do you need help in development, designing or coding domains?

How often have you stuck while coding as you have no idea on how to achieve the required function?

I know the answer may be a big ‘Yes’ from so many.

Stuck while coding. It’s the programmer’s worst enemy – it hurts your productivity, your confidence, your happiness and your peace of mind. It doesn’t mean that you are a failure or inefficient. It’s just that the problem you are facing is complicated and you need help. So, take this situation as a learning opportunity.

First, make sure that you do not get angry and frustrated on the situation, these will not guide you to the proper destination. So just take a break or do something else for a while and back with a fresh mind. You can make wonders with fresh eyes and a cool mind.

A good developer comes through proper practice and experience. They utilize all possible resources including Google search, Forums and any other available resources.

Seeking help from other resources doesn’t make anyone a bad or lazy developer. But, a worse developer is one who didn’t know where to look for help for resolving a problem.

There is a wide range of problem-solving tools and Forums available today. The most crucial thing is how we use it once information been found. Ideally, we should avoid copy and pasting and instead we must read and understand the code before incorporating int into the program we are developing. 

Well, let’s see some excellent forums and sites which help to wipes out the obstacles in your work!

Top resources to solve your coding deadlocks

Stack Overflow

If you are a developer or designer, can you imagine days without Stack Overflow? Obviously, it’s difficult, right? If you are getting stuck, the best ever destination will be none other than Stack Overflow. So, this is one of the most active technical Question &Answer sites and it was evoked by Jeff Atwood and Joel Spolsky in 2008.

Today the Stack Overflow can be rated as Programmer’s Heaven with its excellent tips on problem-solving.  There are thousands, if not lakhs, of programmers who can help us to resolve a problem.  

If you are going to ask a question, first, you should do a proper search since a similar question may be answered by someone already on the platform. If you need an answer to something new, then you can ask a question. So, when you get stuck with an error, invest time to convey the question in a proper way. For example, you can post the exact text with the error or post the code you have already tried. Also mention the software version you are using along with the question.

Before going to ask a question, keep on mind about:

  • Invest Time
  • Be on Topic
  • Start questioning yourself
  • Be prepared to communicate

In short, Stack Overflow is extremely helpful, and it’s absolutely fit to be declared as ‘Programmer’s Heaven’.


Quora is an excellent question/answer (Q&A) forum permitting its members to post questions and answers. Users can also search for questions and answers for a quick fix.

You can get the best code written by the experts on Quora. It’s a huge treasure with millions of fascinating answers on each and every topic that may be interesting to you. Quora can be used as a quick reference for any kind of programming doubts you have in your mind.

One of our team implemented coco3D with guidance from an expert on Quora and we made it a great success. It’s not only used as a helping forum by people, but also it’s a popular platform to share your valuable knowledge. So, get ready to share some of your great ideas. Maybe it would help others who may be looking for an answer or idea that they need in their life.


GitHub is awesome for finding info on coding related topics. I’ve used it for a lot of research purposes and got excellent bits of help on many topics. GitHub helps you to find that perfect set of code that will help you to solve the programming problem you are facing.  It provides its users with a platform to discuss the problems that they are trying to solve.

GitHub’s tagging system can be considered as a great advantage. Another advantage is the GitHub repository.   People can surely reap benefits from it. Availability of the complete code is the highlight of this site. In this competitive programming world, GitHub will surely help to win the race.

Creator’s own Forums

Are you a kind of person who likes to seek help from the creator’s domain? Here creator means the expert(s) who is the founder of a technology. I know some of my colleagues who usually choose this path whenever they get into any kind of deadlocks in programming. Finding solutions from the creator’s page will surely outrank the benefits of referring to any other resource.

Be sure to first visit WordPress own forum for any support on WordPress related problems. On this platform, every question are answered quickly. If you are facing any issues related to Android Development, then you must visit Android Support center and for iOS, you can get expert advice from iOS professional support team. So, start referring to Creator’s forum and solve your programming related issues with ease and in the most perfect way.

General Forums

Just like the listed resources above, there are many other knowledge treasures on the web as well.  Let’s have a look at some helpful websites which may help you with solving your programming related queries.

Site Point

Site Point is one among the most popular online forum for developers and designers today. It is recommended as the best place to get expert solution for your problem and it has around 250,000 members.

Digital Point

Digital point is an excellent forum which is professionally managed by experienced technical leaders. It is one of the biggest webmaster communities for web developers and designers.


MacRumors provides the latest news related to Apple along with rumours related to the same topic. It is also an active iPhone and iPad development Forum. Every thread is replied to and there exist a wide variety of topics. It is worth to post your programming related queries there.

Experts Exchange

We can meet thousands of experts on Experts Exchange to answer every technical question, and it contributes to the success of professionals all over the world.

Dynamic Drive Forum

Dynamic Drive Forum is described as the programmers’ favourite place on the web because it helps them to find fault in their code and also the solution for the same.

So, next time you are stuck with an issue during the development of a program, don’t get depressed but explore these resources. You will surely find the light to come out of the cave you are trapped in.

– Jisna Mathew