সফটওয়্যার ডিজাইন নীতি (Software Design Principles)

সফটওয়্যার ডেভেলপমেন্টে ডিজাইন প্রিন্সিপলস একটু বহুল আলোচিত ও গুরুত্বপুর্ন একটি শব্দ। সাধারনত ডিজাইন প্রিন্সিপালস বলতে বোঝানো হয় একগুচ্ছ গাইডলাইন বা নিয়ম কানুন যেগুলো মেনে কোড লিখলে বাজে ডিজাইন এড়ানো যায়। প্রশ্ন আসতে পারে যে একটি ডিজাইন কখন বাজে হতে পারে। ধরে নেই, কোন একটি সফটওয়্যারের ডেভেলপমেন্ট চলছে। মাঝপথে একটি নতুন ফিচার ইম্প্লিমেন্ট করতে গিয়ে দেখা গেল যে কোন একটি ক্লাসের একটি মেথডের সিগনেচার (মেথডের নাম, আর্গুমেন্ট গুলোর নাম, ডাটাটাইপ, সংখ্যা) পরিবর্তন করতে হবে। কিন্তু ইতিমধ্যে প্রায় ১০০ জায়গায় এই মেথডটি ব্যবহার করা হয়েছে। তার মানে এখন ওই ১০০ জায়গার মেথড কল করার সময় সিগনেচার পরিবর্তন করে দিয়ে আসতে হবে। অথবা ওই ক্লাসে নতুন একটি মেথড তৈরী করতে হবে। এটাকেই বাজে ডিজাইন বলা হচ্ছে। কারন নতুন একটি ফিচার ইমপ্লিমেন্ট করলে যদি আগের ইমপ্লিমেন্ট করা অন্যান্য ফিচারে বাজে প্রভাব ফেলে তাহলে ওই ডেভেলপমেন্টে সঠিক আর্কিটেকচার বা ডিজাইন মেনে কোড করা হয় নি বলেই ধরে নেওয়া হয়। রবার্ট মার্টিন (Robert Martin) নিজ লেখা বই “Agile Software Development: Principles, Patterns, and Practices” এ ডিজাইন প্রিন্সিপাল গুলো সম্পর্কে লিখেছেন।

রবার্ট মার্টিন এর মতে খারাপ বা বাজে ডিজাইন এর নিচের ৩ টি বৈশিষ্ট্য থাকে।

  • রিজিডিটি (Rigidity) : কোডে কোন অংশের পরিবর্তন যদি সিস্টেমের অন্য অনেক জায়গায় প্রভাব ফেলে তাহলে পরিবর্তন করা ব্যয়বহুল হয়ে পড়ে।
  • ফ্রা্গিলিটি (Fragility) : কোডের কোন অংশের পরিবর্তন যদি সিস্টেমের অন্য কোন ফিচার কে অনাকাঙ্খিত হবে অকেজো করে দেয়।
  • ইমোবিলিটি (Immobility) : কোডের অংশ এমনভাবে অ্যাপলিকেশন নির্ভর যে, অন্য কোন অ্যাপ্লিকেশনে কোডটুকু ব্যবহার করা কঠিন হয়।

সাধারন নিচের পাঁচটি নিয়ম মেনে কোড করলে উপরের বর্নিত সমস্যাগুলো এড়ানো যায়।
১। ওপেন ক্লোজ নীতি (Open Close Principle)
২। ডিপেন্ডেন্সি ইনভার্সন নীতি (Dependency Inversion Principle)
৩। ইন্টারফেস সেগ্রেগেশন নীতি (Interface Segregation Principle)
৪। সিঙ্গেল রেসপনসিবিলিটি নীতি (Single Responsibility Principle)
৫। লিসকভ’স সাবস্টিটিউশন নীতি (Liskov’s Substitution Principle)

পাঁচটি প্রিন্সিপালের বর্ননা পরের পোস্ট গুলোতে দেওয়া হবে। এরা প্রত্যেকেই আলাদা আলাদা পোস্টের দাবীদার। 🙂

Preparation for becoming a good Software Engineer/Developer

Why I am writing this post:
I just read a post in medium about how a 19 years old girl became successful developer which got myself into share some of my knowledge too. The first thing came into my mind is to share something about getting a job as a software engineer/developer in reputed companies and how to improve own skills. So, I am going to list some of the qualities recruiters and your lead/project managers expect from you. I will divide the post in two sections:

Section 1: PREPARATION FOR GETTING A GOOD JOB
1. Solve Problems
The most important requirement is the capability of solving problems. It doesn’t specifically mean solving problems of programming contests only. This refers to the capability of solving any kind of problems in real world. As a software developer/engineer, it is the only duty to solve client’s/user’s (specifically human’s) problem. Right?

How to test or improve problem solving skills? The most easiest way is to solve problems of programming contests. Because those problems are being prepared based on real life situations and also you don’t need to learn a lot of technologies to solve them. Just choose any one of programming languages and start solving problems. You will love how they judge your solution with “Wrong Answer”, “Time Limit Exceeded”, “Runtime Exception”, “Stack Overflow” etc. It will improve not only your problem solving skill, but also it will force you to think to find the best solution among thousands of solutions. Isn’t that cool? Let’s get started:
i. UVa
ii. SPOJ

Another option is to start developing few applications (desktop/web/mobile) on your own. Find some idea that can solve human problems. Or you can choose some existing problems. Then start developing them one by one in any of your favourite technologies (Java, Javascript, C#, C++, python, Android, iOS or whatever you want).

2. Be A Smart User
You will have to be a smart user of few things like the IDE you are working on, the Google, the technology you are working on. Let me explain these three:

i. The IDE
IDE stands for Integrated development kit. The tool you are using for writing your codes. For example, if you are a C# developer, it will be Visual Studio, if you work in PHP, it will be PhpStorm or WebStorm mostly, if you are a JAVA developer, it must be Netbeans or eclipse or android studio. Right? So, whatever the IDE is, you must have to be in good relationship with it. You must know basic features and keyboard shortcuts of them. For example, I am sure you will agree with me that debugging is a way to find out what a code snippet is really doing, and it can save a lot of time when something goes wrong. Even debugging will be useful when you need to understand codes written by someone else. So, you must be skilled in debugging in any tool you are using. For example: if you are writing a web application using C# and Javascript, you must know debugging in Visual Studio and also debugging Javascript using your browser.

ii. Version Control
You must use any version controlling tool for your development. For example, Git and SVN are widely used version controlling tool now-a-days.

iii. Issue Trackers
You must have use any issue tracker also. There are lots of issue trackers out there like bitbucket, jira, redmine, gitlab etc.

iv. Keep Pace With The World
Subscribe few technology related blogs or related facebook groups which might keep you updated with latest technologies. With knowing the name of a new technology, you must also know the features, drawbacks, and why this is better from existing techs.

3. Prepare a CV
In the CV you must be focusing on your skills, programming languages you know, tools you have worked on, projects you have worked on, Apps you have developed, Solved problems in different Online Judges and any other achievements you got. CV must not be lengthy with unnecessary informations and also it should be prepared maintaining standard conventions. You can contact with me directly if you need help about preparing a standard CV.

If you practice above items, you should be able to be good software developer in near future.

Section 2: GOT A GOOD JOB ALREADY, NEED TO IMPROVE YOURSELF?
Oh, you have got a job as a software developer/engineer (title may differ but your job is to develop applications). Now it’s time to improve yourself day by day.

1. Self-Evaluation
Yeah, you read it right. You need to be a self evaluator. Don’t wait for someone else (may be your boss (Lead)) to evaluate. Although, most of the good companies have tradition to evaluate workers based on few required criteria. For example: In the company I work on, the team lead evaluates each of the team member in 3 months interval on 23 established items. But still, before someone else evaluate you, you must evaluate yourself after you have taken every single decision, or after you have written every single line of code. Ask yourself following questions:

1. Am I doing the right thing to do?
2. Am I writing the best solution?
3. What other developers are doing in this situation?
4. What type of impact it can create in the existing features?
5. Because of this change, what can happen throughout the app?
6. Did I handled all situation and taken care of all types of exceptions? etc.

If you can make this a habit, you will be able to write beautiful codes too.

2. Become Lazy
You know what, a software developer is needed to be lazy. If you are lazy person, you must hate doing same thing more than once. You must hate to re-write same codes you have already written somewhere else in this application. When writing something, you will write it in a way so that it can be REUSED.

3. Follow Conventions
You must need to follow conventions and standards of existing app when writing codes. If you are developing an app from scratch, study about folder architecture, design patterns and other best practices.

4. Online Teachers (Documentation and Google) Are The Life Savers
So, if you are writing an app using XYZ technology, you must read their documentation at first. Take an idea about what they do, how they do, what APIs they are providing etc. When developing your APP, if you face any issues or may be you need something to do, re-visit the documentation at first. If the solution you need is common, you are going to get it there, believe me. Now if you need something to know (like how to do this in XYZ tech), let’s go to Google and ask it. Read what other peoples are saying about that. You will find a lot of answers. You have to evaluate those answers and find out the best solution. It’s not impossible that, after reading their solution, you can get an idea in your mind which is far better than existing solutions. Right?

5. Communication
Communication is the key. You have to be a good communicator. You will have to communicate with your co-workers or clients on regular basis to make them know about what you are doing, how you are doing, what troubles you are facing and how you have solved them.

I believe if we can follow above instructions in your daily life, we will be a great software developer in future. And also to keep our brain sharp, we need to read books on regular basis. I am gonna add some must-read book list in this post.

Thanks for your patience and reading this.
Ujjal Suttra Dhar

StringBuilder over String

Shovon was working on a task where he needed to traverse 1 lac of data sets and create a string appending some values from each row. But it takes 4-5 minutes just to build a string called “description” by iterating over DataSource.Rows having 1+ lacs rows in it. It shouldn’t take that much time just to iterating because processors are too much faster. So, We (me, Ananta) decided to look into the code.

Shovon implemented it in following manner:

string description = string.empty;
foreach(DataRow row in DataSource.Rows) {
description += string.Format("{0} {1} {2}", row[propA], row[propB], row[propC]);
}

So, what do you think? We are just iterating 1 lac of data and appending some values into a string. And the time increases exponentially with the number of rows.

Is this really should take that much time? My answer was “No”. It should not. Something is going wrong here which is taking this time. Ananta suddenly suggested to use StringBuilder instead of string for description field. So, we tried that in following way:

StringBuilder description = new StringBuilder();
foreach(DataRow row in DataSource.Rows) {
description.Append(string.Format("{0} {1} {2}", row[propA], row[propB], row[propC]));
}
// use as description.ToString()

Basically, instead of concatening strings, using Append method of StringBuilder here. Surprisingly, it didn’t take more than 2-3 seconds to construct that description field iterating over lac data this time. And now the taken time doesn’t increases with the increment of number of data sets.

Explanation:
Lets concatenate some string and see how it works.

string s = '01';
s += '23';
s += '45';
s += '67';

on the first concatenation it makes a new string of length four and copy “01” and “23” into it — four characters are copied. On the second concatenation it makes a new string of length six and copy “0123” and “45” into it — six characters are copied. On the third concatenation, it makes a string of length eight and copy “012345” and “67” into it — eight characters are copied. So far a total of 4 + 6 + 8 = 18 characters have been copied for this eight-character string. Keep going. Keep going till 1 lac of characters. Could you imagine how much data has been copied?

On the hand, StringBuilder doesn’t depend on copy. It actually uses mutable array, internal linked list and other mapping mechanism instead of creating doubled size array and copy. It doesn’t create copy with doubled size until it really necessary. Although the implementation of StringBuilder seems to be changed with versions and it is becoming improved day by day.
Click here if you really want to know how stringbuilder works