What Code Reuse is and Why We Use It

In computer programming, there are many techniques used by programmers to make their programming life easier and better. Various terms are used to convey the simplicity and compactness of a framework, such as Object-Oriented Programming. For example there are many 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 very criticized for its reduced reusability. But that is not the topic of discussion.

Reusability

In programming, reusable code is the use of similar code in multiple 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 reusability defines the methodology you can use to use similar code, without having to re-write it everywhere. As I have already said, not in a way to copy and paste it. There are many techniques to make code reusable in your applications. This technique follows the general programming philosophy of Don't Repeat Yourself (DRY) that states, “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system” and helps developers to maintain the structure of their applications from being messed up and frustrating when debugging the applications. There are many scenarios and methods to do that.

  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 the DRY rule is better and how it supports the developmental process and makes it easy to debug our applications efficiently. You should also know that I am not going to demonstrate the entire paradigm, I will just describe the use of the paradigm in code-reuse.

Inheritance

Inheritance has been in the programming world for 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 An Animal class is often used to describe this behavior. An Animal is used to inherit from for a Lion and a Human and many other types of Animals, each with their own different functions, like a Lion can roar and Humans can speak, but they all have something similar, they can all walk. So it is not necessary to write a walk function for all of these objects separately. You can instead create this function for an Animal and use it in your derived classes, Lion, Human and so on. Let us go through a code example of that.
  1. class Animal  
  2. {  
  3.     // properties  
  4.     int legs;  
  5.   
  6.     // functions  
  7.     void run()   
  8.     { /* code to run here.. */  
  9.     }  
  10. }  
Now you can then use the preceding class as the basic structure to define two other classes, with the same properties and functions, like this.
  1. class Animal  
  2. {  
  3.     public int Legs = 4;  
  4.     public void Run()  
  5.     {  
  6.         Console.WriteLine("Running...");  
  7.     }  
  8. }  
  9.   
  10. class Human: Animal {}  
  11.   
  12. class Lion: Animal {}  
I know this code hides inherited members and I should have used a virtual or abstract keyword inside the Animal class. But, you get the idea, don't you?

Now this is just a complex way of ensuring that the members of the Animal class are available under the Human and Lion classes also. If you can the function Run over a 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 various parts of your application. You create small code blocks of an algorithm or logic-based source code and provide it a name, identifier. That identifies your code and can describe the process too. We have the following functions:
  1. A return type that denotes what type of data would be returned or a void.
  2. An identifier that is used to make a call to this function.
  3. Function body that 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.
Functions are used to ensure that every logic is written once so that if there must be a change in the logic made then we make it once and it is updated everywhere in the application. For example, if you need to greet a user many times inside your application, instead of writing a greeting message everywhere, you can create a greeting function.
  1. void greet()   
  2. {  
  3.     // Greet user  
  4. }  
This can be then used in enormous places to greet the user. A good thing is that it can be edited in one location and the changes will be reflected everywhere you call this function. This was a very basic example, if you allow users to post something (such as status, photo and so on) then instead of writing post_update code everywhere, you can create a function in the business-logic-layer and call that function from whereever you want to call it. The good thing, once again, is that you get to update the function in one place only and the changes will be reflected everywhere.

Libraries

You might have heard of Dynamic Link Libraries? Yes, the highly used DLL files. These libraries are a set of compiled programs, most often used by larger and enterprise level software, to support the execution since 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 a .NET application or ASP.NET application, then chances are 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 allows support of creating third-party libraries the distribution of them using various methods. You can also note, that System (the 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 the 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 and so on.

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 various 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 the 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 help in programming, as to create robust and strong enterprise applications and has helped programmers maintain their hairs on their head and not in their hands as the application reaches the enterprise level of complexity.


Similar Articles