DRY – Don’t repeat yourself

DRY – Don’t repeat yourself.
DIE – Duplication is evil

The above principles are applied in Software engineering to reduce the complexity, increase the quality and maintainability of the software(or any work product comes under the system). This principle coined by Andy Hunt and Dave Thomas in their book called “The Pragmatic Programmer” which is one of my highly recommended book for every software developer.
Every piece of knowledge should have a single, unambiguous, authoritative representation within a system.
This is applicable in the entire life cycle of a product from the design, to documentation, coding to test cases. This approach makes the software (or work product) high in quality and lower the maintenance effort.
For example, in my programmers’ life I’ve reviewed several code where we can find the same code is repeated across multiple files, or even multiple functions in the same file. The problem is the logic may have bugs and later if this part want to be removed or modified, we’ve to take care all part of the system to ensure we’ve properly modified it and ensure nothing is broken. This is increasingly complex if the project is too large and several people working on it. But if we define it in a single meaningful way and make the entire system make use of this unique piece of source code, that’s the best way tot manage complexity and maintainability. The cost for fixing such issues are very high. The bugs are always cost effective during the development period. Once if it’s released to the market, the cost to deploy the updates are too high and risky.
Not only Duplication, mostly the developers leave orphan functions which may cause confusions. for e.g. ProcessPresetChange() and OnPresetChange(). (Take it, I’ve see such kind of code several times). These functions doing the same stuff. How does it created? First a  simple version of this function was implemented. Later due the poor quality of code, the new programmer has learned things himself and implemented another function which is invoked from a different user action. The previous function was written for test purpose. Anyway the problem solved temporally. Later, when another person wants to modify the same function, which one he will modify? The previous programmer left the orphan functions in the same source file. The new person has to find out where the functions are being called and replace it well. This is extremely costly and confusing.
The duplication can arise in several situations.
1. Developer is forced to duplicate the information as he has no other way.
2. Developer unknowigly duplicate the information without having proper understanding of what he do. The senior developers should orient the team members towards the data flow, architecture and the way to accomplish things before people start to work on the products.
class Line
{

int start, end, distance;

…..

};
The above class may find good enough to get things done. but what if it was written like this,
class Line
{

int start, end;

public:

GetLength(){ return end – start;}

};
3. Lazy programmers find duplication is the easiest way to get things. They’re the real evils.
4. When collaboratively working with large number of team, the duplication can be occurred from different resources. In this context the teams should share the design and other knowledge enough to work well each other. Reusable components are the solution to solve such kind of problems.
I’ve mainly talked in programmer’s point of view. This applicable everywhere like Database management, documentation etc.
On closing, Keep in mind “Don’t Repeat Yourself”

Clarity is better than cleverness

“Clarity is better than cleverness” This is one of the core UNIX philosophies.

Code that is graceful and clear, on the other hand, is less likely to break — and more likely to be instantly comprehended by the next person to have to change it. This is important, especially when that next person might be yourself some years down the road.

Never struggle to decipher subtle code three times. Once might be a one-shot fluke, but if you find yourself having to figure it out a second time — because the first was too long ago and you’ve forgotten details — it is time to comment the code so that the third time will be relatively painless.

– Henry Spence

In his classic book, Code Complete, Steve McConnell has a more elaborated version of this philosophy as a theme of software craftsmanship.

In the early years of programming, a program was regarded as the private property of the programmer. One would no more think of reading a colleague’s program unbidden than of picking up a love letter and reading it. This is essentially what a program was, a love letter from the programmer to the hardware, full of the intimate details known only to partners in an affair. Consequently,programs became larded with the pet names and verbal shorthand so popular with lovers who live in the blissful abstraction that assumes that theirs is the only existence in the universe. Such programs are unintelligible to those outside the partnership.

— Michael Marcotty

The readable code covers all good aspect of a program like comprehensibility, Reviewable, error rate, Debugging, maintainability etc.
For a small performance gain, if we make the code so complex that will become a port of bugs and hard to maintain in the future. When we pass the work to someone else for late maintenance, the clever un-maintainable code can give you a bad name even it served your purpose and satisfied your head during the time you handled it. McConnel has explained it in very details including the conventions, usage of logic etc in his book other than just proposing this as philosophy. As he said, let’s all write programs for people first, computers second.

Versioning Systems and 3 Way merge process

If you’re a developer you must know how important is a file comparison tool. Especially when working in a team with a source/versioning system. It’s quite often we compare several versions of source files. The usual comparison and merging with two different version of files are called 2-Way merge process.
To talk about the 3 way merging, first we must get a basic essence of versioning system and merge process. Because 3 way merge is part of it and became popular with it.
The conventional configuration management systems like Visual Source Safe provides the basic functionalities of configuration management. Things have changed much in these years. In the new age of software development, people rely on versioning system where individual developers can work independent with his own branch and merge the changes to main server, merge to other branches etc. The version of files controls everything. This helps us to work in collaborative work environment. If you know or not, even geographically distributed teams are working well with new age versioning systems.
Various versioning systems are available in the market today. Both free and commercial tools like IBM Rational Clearcase, CVS, Subversion, Git are the popular among the tools available.
Let’s see how versioning system works. When we check-in the files, obviously we’re creating a new version of the same file. In conventional source management systems, everyone works with the latest version of the files. We will be connecting to a the source management server and do the check-ins and checkouts but we’re getting limited with lot of other facts.

  • Other developers are unable to checkout the files and they keep waiting.
  • Everyone working in the latest version of the files available in the fileserver and making it’s hard to work with a different version of systems.
  • Increasing number of local copies and which is totally unsecure.
  • Less flexible to merge different versions of software.
  • Too much limitations of imposed by source controlling systems.

Ok Let’s think in a broader way. What if

  • We get a source control systems, where we can work independently on the replica of the latest software where every developer are independent and working in their own branch. Get the latest version of unmodified files easily?
  • Are you happy if you can work on a specific time based branch and work according to your convenience?
  • What if you many other developers modify the same file and easy way to merge everything back and forth with flexible tools?
  • What if you can easily get a tree representation of the versions of the files for flexible comparison and detailed information?
  • What if you’re free to work geographically distributed and work with same source and same team effectively?

If your answers are yes, the modern versioning systems are the answer for these queries. Versioning , branching and merging are the core features of a systems.
The following figure represents basic concept of a versioning system (Taken from Wikipedia.org)

image

In a versioning system, there will be a mainline/trunk which contains the latest version of the product. This will be maintained high in quality. One or more developers are working on the replica of mainline called branch during their development time. The modifications in the branch is independent and mainline in not affected. Once developer finish testing and merging, it’s the time to merge the code to mainline.

The red colored arrow to the trunk is called merging process which is explained below.

Trivial Merge

For each file in the branch, it’s checked out from specific version of the same file from parent branch (mainline). Once we checkout, other developers can also checkout the same file and merge to the mainline. But during this time, the file we checked out remains intact. If no other new versions of the same files are created in the mainline during the merge process, it’s called, trivial merge. This is something like a simple comparison. The different part of source code will be merged to the main.

Non Trivial Merge

There are different kind of situations for a trivial merge. Consider the following situations.

1. Developer working on a prior version of software and other developers updated the same files with a latest version in the mainline.

2. Get the modification of same files from another developer from his branch, which may created based on different base versions.

When you get two different versions of same file and ask to do the merge process, you’re just taking your comparison tools and make the best possible assumption to merge the files. This requires intensive human effort and also error prone. Some people do an alternative by asking to put tags while coding. Which is totally conventional.

Most of the versioning system provides a merge tool associated with it. This can smooth up the merge processes. Independent merge tools are also available in market, both free and commercial.

As I mentioned before, there will be a mainline/trunk where you can get the latest version of the product. Each files under the configuration management will have the version history of it’s own.Consider the following situation.

image

You can see Joe and Dave checkout same file, based on Version 1 in the mainline. Dave finished his work and checked in the file to mainline and version 2 created in mainline. During this time, Joe continue his development based on version 1. Finally before her merge back/during development time itself, he identifies there’s latest version of file is created. Now he required get the modifications of Dave to continue his development or merge back. If Joe blindly merge back, to mainline, Dave’s modification will be lost. Joe can make best assumptions on the conflicting points when he compare Dave’s modification and create the new version (2) which contains both his and latest modifications but this is error prone.

A three way merge can help us in this regard.

  • We’ve a base version Main(1)
  • We’ve a latest version Main(2)
  • Joe have his own modification Joe’s branch(1).

The merge tools can easily find the changed points based on the base version. It can easily create a modification with latest and latest local version without losing any modification. There would be some conflicting points where both developers merged same lines of source code. In this case, human interference is required. We’ve to choose the source code by seeing base, latest, latest local. User can easily identify which code he need to persist in new version (2).

The three way merge process is less error prone and requires only less human effort. The tools like KDiff3, Beyond Compare 3, Perforce provides merge tools for effectively do a three way merge process. All these tools can also attach to your existing source control system. However, most of the revision control software itself provides its own merge tool with their package.

 

An example merge process screenshot from KDiff3 is attached here(Click to Expand).

dirmergebig

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.