What is code reuse and why use it?

In computer programming, there are many techniques used by programmers to make their programming life easier and better, different terms are used to denote the simple and compactness of a framework, such as, Object-oriented programming language. For example there are quite possible methods to let you re-use the same functions and properties in your code, by inheriting the actual class and using those members; functions or properties, inside your derived class. But OOP has been criticized a lot for its less reusability. But that is not the topic of discussion

Re usability

In programming, code re usability is using the similar code in different functions… No, not by copying and then pasting the same code from one block to another and from there to another and so on. Instead, code re usability defines the methodology that you can use to use the similar code, without having to re-write it everywhere; like I already said not in a way to copy and paste it. There are a lot different techniques that you can use in order to make use of code reusability in your applications. This technique follows the general programming DRY (Don’t repeat yourself) rule, which states, “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” and helps the developers to maintain the structure of their applications from being messed up and frustrating while debugging the applications. There are quite different scenarios and methods to use,

  1. Inheritance
  2. Functions
  3. Libraries
  4. Forking

And it continues with many other frameworks being formulated every day and new paradigms being designed every decade and being used. But I would like to explain a few of these and why they’re better and so on. Finally, it would be clear, why DRY rule is better and how does it support the developmental process and makes it easy to debug our applications efficiently. You should also know that I am not going to demonstrate entire paradigm, I will just describe the use of paradigm in code-reuse.

Inheritance

Inheritance has been into programming world since quite a long time and is overly used in Object-oriented programming paradigms. This paradigm lets you use the base-class’s functions and members in other classes, most of the times an Animal class is used to describe this behavior, Animal is used to inherit from for a Lion and Human and many other types of the Animals. Who have their own different functions, like Lion can roar, human can speak, but they have something similar… Such as walk. So  it is not necessary to write walk function for all of these objects separately instead you can create this function for an Animal and use it in your derived classes; Lion, Human etc. Let us go through a code example of this scenario…

class Animal {
   // properties
   int legs;
   
   // functions
   void run () { /* code to run here.. */ }
}

Now you can then use the above class as the basic structure to define two other classes, with same properties and functions, like this.

class Animal {
   public int Legs = 4;
   public void Run() { 
       Console.WriteLine("Running..."); 
   }
}

class Human : Animal { }

class Lion : Animal { }

I know that this code hides inherited members and I should have used virtual or abstract keyword inside the Animal class. But, you get the idea don’t you?

Now this is just a complex way, to ensure that the members of Animal class are available under Human and Lion classes also. If you can the function Run over Lion or Human object, you will see that it will Run. Although making the functions work for different objects differently is a totally different aspect of this, you need to implement virtual or abstract keywords to avail such functionality to allow the developer to override the functions (or members more specifically) and add the body of their own; to write “Human running…” instead of just “Running…”.

Function calls

Function calls are another way of reusing the code from different parts of your application. You create small code blocks of an algorithm or logic-based source code, and give it a name, identifier. Which identifies your code; can describe the process too. Functions have,

  1. A return type, which denotes what type of data would be returned or a void.
  2. An identifier, which is used to make a call to this function.
  3. Function body, which is the code block containing the code to be executed when this function is called.
  4. Zero or more parameters to provide values to the code block variables.

Function are used to make sure that every logic is written once, and so that if there has to be made a change in the logic, we make it once and it is updated everywhere in the application. For example, if you have to greet a user many times inside your application, instead of writing a greeting message everywhere, you can create a greeting function

void greet() {
   // Greet user
}

This can be then used in enormous places to greet the user. Good thing is that it can be edited at one location, and changes will be reflected through where-ever you call this function. This was a very basic example, if you allow users to post something (such as status, photo etc.) then instead of writing post_update code everywhere, you can create a function in the business-logic-layer and call that function from where-ever you want to call it. Good thing, once again, is that you get to update the function at one place only and the changes will be reflected everywhere.

Libraries

You might have heard of using Dynamic-link library? Yes, the most used .dll files. These libraries are a set of compiled programs, majorly used by larger and enterprise level software, to support the execution as they can be executed upon call.

If you have ever used NuGet packages, tried using .dll files, or added any assembly to your bin folder in .NET application or ASP.NET application, then chances are that you’ve already used libraries before and are well-known with these libraries. Most of the packages that are distributed to be used by other clients, are libraries that can be linked to your executable for purposes that they are defined for.

Most of the framework allow support of creating third-party libraries and to distribute them through different methods. You can also note, that System (namespace of the .NET framework) is also present in the System.dll library.

Forking

Forking, well if you’re a git geek, then you should know it better than me because I use Github’s GUI version and I don’t have much knowledge about underlying git. But, forking is a process of using someone else’s source code and making changes to it to fit your own needs, to distribute, use, share etc.

You can also fork someone else’s source code and write some more details to it. Make some bug fixes, add some patches and share it. This terminology and method has been widely used by different companies, to fork open source projects, make a few changes to the “Under the hood” and then distribute it with their own label and tag.

In my opinion, Forking is not one of the code reuse methods. You do not just reuse the code, you use it from ground up, building another framework of it and so on. But everyone has their own opinion, I have mine and so I cannot say anything about your opinions. Although, these methods have proved a lot helpful in programming, as to create robust and strong enterprise applications and have helped programmers maintain their hairs on their head and not in their hands as the application reaches the enterprise level of complexity.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s