DEV Community

Cover image for Framework vs Library (What are they?)
Aweys Ahmed
Aweys Ahmed

Posted on • Updated on

Framework vs Library (What are they?)

Using a Framework or library or both can help you build out applications. They are different despite these terms being using interchangeably.

What is a Library

A library is a collection of reusable code that has been created somewhere else. You end up calling these methods onto your code. The fact that you call the methods from the library into your application is a key differentiator from a framework.

Let's use React's render method to demonstrate how a library would be used in your code.

class Demo extends React.Component {
 render() {
   return <h1>We just used the render method</h1>
  }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we used the React's render method to return the jsx code that we have written.

We use a library for the predefined methods and features on the code that we create.

What is a Framework?

A Framework creates a skeleton or scaffold that calls on the code that you have written. Once you implement the Framework, you fill in the rest of the code with your code. The framework will then call on your code.

Let's use Rails which is a ruby framework to demonstrate this.

class ArticlesController < ApplicationController

  def index
    @articles = Article.all
  end

end

Enter fullscreen mode Exit fullscreen mode

Let's review the Ruby Controller above to see what the rails framework is doing.

I used rails to create this Articles controller and we have one method called index. The name of the method corresponds with a URL. When a user heads to the correct URL, rails will call the index method and run the code we defined in that method. So in the above example, every article will be stored in the @articles instances variable.

A framework calls on your code to execute whereas you as the developer call on methods from a library.

Inversion of Control

When you are using a library, you create your code and call on the library as you need it. With a Framework we have an Inversion of control and the Framework calls on your code to execute the unique features of your code.

Here is a quick analogy for Library vs Framework.

If you wanted to build a car from scratch you would need tools to get the job done. The tools that you would use to build the car from scratch would be your library.

If you were to use a Framework to construct a car, you would start with an already constructed chassis or vehicle frame and you would put in the unique finishing touches.

I hope this helped you getting a better understanding of Framework and Library.

Top comments (0)