Introduction
A framework is a collection of reusable code that provides the overall structure of an application, defining how different pieces fit together, while leaving specific gaps for the programmer to fill in with their own logic. The key distinguishing idea, often summarized as inversion of control, is that a framework calls your code at the appropriate moments, rather than your code calling the framework's functions whenever it wants, which is how a plain library works.
Cricket analogy: A domestic cricket league's fixed format — overs, innings, and match rules already set by the governing body — is like a framework: the structure calls each team's specific tactics into play at the right moment, rather than each team inventing the whole match format themselves, mirroring inversion of control.
Explanation
With a plain library, the programmer is in control: their code decides when and how to call a library function, such as calling a math library's square root function whenever the program needs it. With a framework, control is inverted: the framework's main loop or lifecycle decides when to call the programmer's code, such as a web framework calling a specific handler function automatically whenever a matching HTTP request arrives, without the programmer's code ever explicitly calling that framework internally to trigger it.
Cricket analogy: A batter choosing exactly when to call for a physio break during a net session, on their own schedule, is like calling a library function directly, whereas a match umpire calling for a mandatory drinks break at a fixed over is like a framework calling the programmer's code at its own chosen moment.
Because a framework defines the overall structure, it also imposes conventions the programmer must follow, such as where to put files, what to name certain functions, or in what order lifecycle steps happen. This constraint is a deliberate trade-off: giving up some freedom in exchange for not having to design and maintain that structure from scratch, which is especially valuable for common, well-understood application shapes like a typical web app or mobile app.
Cricket analogy: A domestic league's fixed rules about over limits and squad sizes constrain what a captain can do tactically, but in exchange no team has to invent match rules from scratch every season, the same trade-off a framework's conventions make for a programmer's freedom.
Example
# Using a library (you call it directly, whenever you want)
import math
result = math.sqrt(16) # your code calls the library function
# Using a web framework (it calls your code, when a request arrives)
from flask import Flask
app = Flask(__name__)
@app.route("/hello")
def hello(): # the framework calls this function for you
return "Hello, world!"
# app.run() starts the framework's own loop, which decides
# when to invoke hello(), not the programmer directlyCommon examples of frameworks include Django and Flask for web backends, React and Angular for frontend interfaces, and Spring for Java applications. Each defines the overall application structure and calls your specific code at defined points, such as rendering a component or handling a route.
Key Takeaways
- A framework provides the overall structure of an application, filling in the pieces you leave as gaps.
- Inversion of control means the framework calls your code, rather than your code calling it directly.
- A plain library, by contrast, is called by your code whenever your code decides to use it.
- Web frameworks like Flask or Django call your handler functions when matching requests arrive.
- Examples of frameworks include Django, Flask, React, Angular, and Spring.
Practice what you learned
1. What is the key idea that distinguishes a framework from a plain library?
2. With a plain library, who decides when a function is called?
3. In a web framework, what typically triggers your handler function to run?
4. Which of the following is an example of a framework mentioned in the topic?
5. What does a framework leave for the programmer to fill in?
Was this page helpful?
You May Also Like
What Is an IDE
How an Integrated Development Environment bundles an editor, compiler or interpreter, and debugger into one tool for writing code.
Pseudocode
How pseudocode expresses an algorithm's logic in structured, language-agnostic plain language before writing real code.
Data Structures Intro
An overview of arrays, lists, stacks, queues, and maps, and how choosing the right one affects a program's performance.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics