LAME Question of the Day: What is the Difference Between a Library and a Framework?

Introduction

Before writing this article, I believed there is no significant difference between a library and a framework. They both seem to be the same to me. But I was wrong; there is in fact a significant difference between the two and as programmers, we must understand the differences. These two terms are frequently used in the software industry but still are highly subjective to answer.

Library and a Framework

Let me ask you a simple question. Is jQuery a library or a framework? You may know the answer but can you justify your answer. Hold your thoughts for the end of this article and until then, let me simplify the terms for you.

Question of the Day

Our lame question of the day is: "What is the difference between a Library and a Framework?”

Analysis

To analyze the question properly and come up with an answer, let us consider the following cases.

  • Case 1: The “text-book” definitions.

    If you need to learn something, the first thing you probably do is to check its definition. The same for me. Before getting into the differences, let's first understand these terms.

    Library

    According to the Wikipedia, “A library is a collection of implementations of behavior, written in terms of a language that has a well-defined interface by which the behavior is invoked”.

    In simple terms, a library is a piece of reusable code that we can access in our application through some mechanism and execute its functionality. It usually focus on a single piece of functionality. A library contains methods or class definitions written by other developers to perform specific, well-defined operations in a domain specific area, which in general, simplify our coding experience. For example, there are some libraries of mathematics that can let developers just call the function without redoing the implementation of how an algorithm works.

    Framework


    According to the Wikipedia, "A framework is a reusable software environment that provides specific functionality as part of a larger software platform to facilitate the development of software applications, products and solutions.”

    In simple terms, a framework dictates the overall architecture of your application. Once you chose a framework to build your application, you need to follow the framework's code and design methodologies. The framework provides you with many pre-written libraries, scripts, or any other functionalities of software you need to develop your application.

  • Case 2: The “actual” distinction

    If we need to follow the preceding definitions, the difference lies between the flows of control. When we use a class library in our code, we are in charge; but when we write our code inside a framework, a framework dictates our code. There is a popular term that can depict the distinction among them called the Inversion of Control Principle.

    The Inversion of Control principle states that, “You use a class library in writing your code, but you code within a framework”.

  • Case 3: Real World Example

    Take a simple case of Random Numbers. If I ask you to write a simple program that generates a series of random numbers, what will you do? You write an algorithm in your choice of programming language and build the program. Let us say, you choose C# and write a function to generate random numbers. It took you some time to write the logic for random numbers, but you'll eventually write the program.

    Here is the interesting aspect. What if I tell you that I have a prewritten class named Random.cs, tried and tested by developers that can generate the random numbers for you? You only need to call it in your program. Isn't this helpful and time-saving? So, you add the class in your program, instantiate it and bingo! Your program is ready to generate random numbers. Another interesting thing here is that you have full control over the Random class, you called the class and you can use it however you want.

    Another day, comes another requirement. Now, I ask you to build a fully functional Calculator along with a button to generate random numbers. What will you do? This time you cannot simply write the algorithm and the deal is done. You need to consider several other things as well. Primarily, the look and feel of your application; the mathematical operations; scientific calculations and also a button to generate random numbers on a click. This time you will need more and more prewritten classes to get the work done easily. What about the UI? You need to provide your application a proper look and feel and at the same time accuracy for calculations.

    What if this time I tell you that there is a thing called "X" that can do the work for you just like the case of random numbers, but, under one strict condition. You need to work accordingly to the "X". This time you aren't the in-charge. You need to learn about its working and then work accordingly to it. Under X's environment, you have a number of pre-written classes that can do the work. So, if you work under X, you can build the Calculator in no time.

    To sum up, the so called "X" I'm talking about is known as a framework and inside the framework you have several pre-written classes called libraries. The flow of control among them can be describe in words of Martin Flower's Inversion of Control principle as: "Your code calls a library, but a framework calls your code".
Conclusion



The conclusion from all the preceding cases state that the difference between a framework and library is the flow of control among them. I asked you a question in the beginning of the article about jQuery. I hope after reading the article, you can answer that question. Your feedback and constructive criticism is always appreciated, keep it coming. Until then, try to put a ding in the universe!