Programming Techniques / Styles – Understanding How Different Programming Paradigms Work

Programming Techniques / Styles – Understanding How Different Programming Paradigms Work
1. Introduction
Have you ever wondered why different programming languages use different approaches to solve the same problem?
For example, consider a simple requirement:
“Bring a book from the library.”
There are different ways to instruct a computer to accomplish this task.
- We can write step-by-step instructions.
- We can organize the solution using objects and their responsibilities.
- We can divide the solution into smaller reusable functions.
- We can simply declare the desired outcome and allow an underlying system to figure out how to achieve it.
These different approaches are known as Programming Paradigms or Programming Styles.
A programming paradigm defines the way developers structure, organize, and express solutions to computational problems.
2. Major Programming Paradigms
There are several programming paradigms. Four commonly discussed approaches are:
- Procedural / Logic-driven programming
- Object-Oriented Programming
- Functional Programming
- Declarative Programming
An important distinction: imperative programming is a broad category that includes procedural programming and many object-oriented programming approaches. Logic programming is another separate declarative paradigm.
Let’s understand each approach with practical examples.
3. Procedural Programming (Step-by-Step Instructions)
What is Procedural Programming?
Procedural programming is an approach in which developers write instructions in a sequential manner to accomplish a particular task.
The programmer defines:
- What problem needs to be solved.
- How the problem should be solved.
- Which instructions should execute.
- Under which conditions particular instructions should execute.
The developer has control over the execution flow using programming constructs.
Common programming constructs
1. Conditional statements
- if
- if-else
- switch-case
2. Looping statements
- for
- while
- do-while
- for-each
3. Other constructs
- Variables
- Methods / Functions
- Procedures
- Sequential execution
Real-world example: Bring a book from the library
Consider the following problem.
Requirement: Bring a particular book from the library to home.
Solution using procedural programming:
- Start.
- Travel to the library using a bicycle.
- Enter the library.
- Search for the required book.
- Check whether the book is available.
- If available:
- Pick up the book.
- Complete the checkout process.
- Otherwise:
- Ask the librarian for alternative recommendations.
- If recommendations are available, search for an alternative book.
- Otherwise, exit the library.
- If available:
- Return home.
The programmer explicitly defines the sequence of operations and decision-making process.
Programming language examples
- C
- Pascal
- FORTRAN
- COBOL
These languages are commonly associated with procedural programming, although some also support other programming features.
Advantages
- Easy to understand for straightforward problems.
- Provides control over execution flow.
- Suitable for algorithmic programming.
- Simple to implement for small applications.
Disadvantages
- Large applications can become difficult to maintain.
- Code reuse may require additional design.
- Complex business logic can become difficult to manage.
- Changes in one part of the program may affect other parts.
4. Object-Oriented Programming (OOP)
What is Object-Oriented Programming?
Object-Oriented Programming is a programming paradigm that organizes software around objects rather than only sequential instructions.
An object represents an entity that contains:
- Data (Properties / Attributes)
- Behaviour (Methods / Functions)
Instead of writing all the application logic in one place, developers divide the application into reusable classes and objects.
Example: Library Management System
Consider the same requirement: Bring a book from the library.
We can identify the following objects:
| Object | Responsibilities |
|---|---|
| Library | Manage books |
| Book | Maintain book information |
| User | Request a book |
| Librarian | Help users find books |
| Checkout | Handle book borrowing |
Each object has its own properties and behaviours.
For example:
Book Object
- Book ID
- Book Name
- Author
- Availability Status
Methods:
- getBookDetails()
- checkAvailability()
- updateAvailability()
Library Object
Methods:
- searchBook()
- addBook()
- removeBook()
The application uses interactions between these objects to accomplish the required functionality.
Four pillars of OOP
- Encapsulation: Combining data and methods into a single unit and controlling access to the data.
- Inheritance: Reusing and extending the functionality of existing classes.
- Polymorphism: Allowing different objects to respond to the same operation in different ways.
- Abstraction: Hiding implementation complexity and exposing essential functionality.
Programming language examples
- Java
- C++
- C#
- Python
- Ruby
Advantages
- Code reusability.
- Better modularity.
- Easier maintenance.
- Suitable for large enterprise applications.
- Better organization of business logic.
5. Functional Programming
What is Functional Programming?
Functional Programming is a programming paradigm in which developers build applications by composing and executing functions.
Instead of focusing primarily on changing the state of objects, functional programming emphasizes:
- Functions.
- Immutable data.
- Function composition.
- Predictable outputs.
- Minimizing side effects.
A function takes input, performs an operation, and returns an output.
Example: Library Management System
Consider the same requirement.
Instead of organizing the entire solution around objects, we can divide the problem into smaller functions.
public class LibraryFunctions {
public static Book searchBook(String bookName) { // Search for the book return findBook(bookName); }
public static boolean isAvailable(Book book) { return book != null && book.isAvailable(); }
public static Book checkout(Book book) { // Return updated book information return book.withAvailability(false); }
public static void main(String[] args) {
Book book = searchBook("Java Programming");
if (isAvailable(book)) { checkout(book); } }}Here, we divide the overall problem into smaller operations.
Each function is responsible for a particular task.
In pure functional programming, functions should avoid modifying external state and ideally produce the same output for the same input.
Important concepts
- Pure functions.
- Higher-order functions.
- Lambda expressions.
- Immutability.
- Function composition.
- Recursion.
- Map, Filter and Reduce.
Programming language examples
- Haskell
- Clojure
- Scala
- F#
- Elixir
Java, Kotlin, Groovy, Python and JavaScript also support functional programming features.
Important: A language supporting lambda expressions does not automatically make it a purely functional programming language.
Advantages
- Better code reusability.
- Easier unit testing for pure functions.
- Reduced side effects.
- Predictable program behaviour.
- Useful for concurrent and parallel processing.
6. Declarative Programming (Define What You Want)
Important concept for DevOps and Ansible
What is Declarative Programming?
Declarative programming is an approach where developers specify what result they want, rather than explicitly writing every instruction required to achieve that result.
The underlying framework, engine, or system determines how to accomplish the requirement.
In simple words:
We describe the desired outcome, and the underlying system takes responsibility for achieving it.
Imperative vs Declarative approach
Consider a requirement:
“Install and start the Nginx server.”
Imperative approach:
The developer writes instructions describing how to perform the operation.
sudo apt updatesudo apt install nginx -ysudo systemctl enable nginxsudo systemctl start nginxWe explicitly specify the commands that need to execute.
Declarative approach:
We define the desired state.
- name: Install and start Nginx hosts: webservers become: true
tasks: - name: Ensure Nginx is installed ansible.builtin.apt: name: nginx state: present
- name: Ensure Nginx is running ansible.builtin.service: name: nginx state: started enabled: trueHere, we declare:
- Nginx must be installed.
- Nginx must be running.
- Nginx must start automatically when the system boots.
We do not explicitly write all the internal installation and service-management commands.
Ansible modules contain the implementation logic required to perform these operations.
How does Ansible achieve this?
Ansible uses modules to perform infrastructure-management operations.
For example:
| Ansible module | Responsibility |
|---|---|
apt | Manage Debian/Ubuntu packages |
yum | Manage packages on supported RPM systems |
service | Manage services |
copy | Copy files |
file | Manage files and directories |
user | Manage users |
The developer specifies the desired state using module parameters.
Ansible executes the modules to move the target system towards that state.
Understanding Idempotency
One of the most important concepts in Ansible is Idempotency.
Idempotency means that executing the same operation multiple times produces the same intended final state.
For example:
- name: Ensure Nginx is installed ansible.builtin.apt: name: nginx state: presentWhat happens when we execute this task?
First execution:
- Checks whether Nginx is installed.
- If it is not installed, installs it.
- Reports a change.
Second execution:
- Checks whether Nginx is installed.
- Finds that it is already installed.
- Does not reinstall it.
- Reports no change.
This is how Ansible helps maintain the desired configuration of infrastructure.
Other examples of declarative approaches
- Ansible
- Terraform (declarative configuration)
- Kubernetes YAML manifests
- SQL queries
- HTML
For example, in SQL:
SELECT name, salaryFROM employeesWHERE salary > 50000;We specify what data we need. The database engine determines how to execute the query.
Advantages
- Less implementation detail in application configuration.
- Better readability.
- Easier infrastructure management.
- Consistency across environments.
- Supports idempotent operations when the underlying system is designed for it.
7. Imperative vs Declarative Programming – Key Differences
This is one of the most important comparisons to understand.
| Feature | Imperative | Declarative |
|---|---|---|
| Focus | How to do it | What is required |
| Instructions | Explicit steps | Desired outcome |
| Control | Programmer controls execution flow | Underlying system determines execution |
| Abstraction | Usually lower-level | Usually higher-level |
| Examples | C, Java, Python | SQL, HTML, Ansible configurations |
| Main responsibility | Define the process | Define the result |
Important clarification: Object-Oriented Programming and Functional Programming can both be used in imperative or declarative ways. They are not mutually exclusive categories.
Groovy, for example, is a multi-paradigm language. It supports imperative, object-oriented and functional programming styles. It is not exclusively a functional language.
8. How Are These Programming Styles Related?
Let’s understand the overall relationship.
Programming paradigms can be understood through two broad approaches:
A. Imperative Programming
The programmer describes how the system should perform operations.
Common approaches:
- Procedural Programming.
- Object-Oriented Programming (often imperative).
- Imperative functional programming.
B. Declarative Programming
The programmer describes the desired result, and the underlying system determines how to achieve it.
Common approaches:
- Logic Programming.
- Functional Programming (often declarative).
- Query languages and configuration-based approaches.
These categories overlap because a single language can support multiple programming paradigms.
9. Real-World Comparison: Same Requirement, Different Approaches
Let’s take one common requirement:
Requirement: Bring a book from the library.
| Programming style | How we approach the problem |
|---|---|
| Procedural | Write each step: travel, search, check availability, checkout and return. |
| Object-Oriented | Create Library, Book, User and Checkout objects with responsibilities. |
| Functional | Create small functions to search, check availability and process checkout. |
| Declarative | Specify the desired result, such as obtaining an available book, and let an underlying system determine the execution process. |
Notice that the objective remains the same, but the way we express the solution changes.
10. Why Should Java Developers Understand These Paradigms?
Understanding programming paradigms is important because modern software development uses multiple approaches together.
For example, consider a Spring Boot microservices application.
| Technology | Programming approach |
|---|---|
| Java classes and objects | Object-Oriented |
| Java Streams API | Functional |
| Java loops and conditions | Imperative |
| SQL queries | Declarative |
| Ansible deployment | Declarative configuration |
| Kubernetes manifests | Declarative configuration |
As a Java developer, you may already be using several programming paradigms without explicitly identifying them.
For example:
List<String> names = employees.stream() .filter(employee -> employee.getSalary() > 50000) .map(Employee::getName) .toList();This example uses functional programming concepts through Java Streams and lambda expressions.
However, Java itself is a multi-paradigm language.
Understanding these differences helps developers select appropriate approaches when designing scalable applications.
11. Key Takeaways
- Programming paradigms represent different approaches to solving computational problems.
- Procedural programming focuses on step-by-step instructions.
- Object-Oriented Programming organizes applications around objects and their behaviours.
- Functional Programming emphasizes functions, immutability and minimizing side effects.
- Declarative Programming focuses on defining the desired outcome rather than implementation steps.
- Imperative programming focuses on describing how operations should be performed.
- Ansible uses declarative configuration and modules to manage infrastructure.
- A programming language can support multiple paradigms.
Final thought: A good programmer should not just know how to write code. A good programmer should understand which programming approach is suitable for solving a particular problem.
12. Interview Questions
Use these questions to test your understanding.
- What is a programming paradigm?
- What is the difference between procedural and object-oriented programming?
- What are the four pillars of OOP?
- What is functional programming?
- What are pure functions?
- What is the difference between imperative and declarative programming?
- Why is SQL considered a declarative language?
- Is Java a purely object-oriented programming language?
- Can Java support functional programming?
- Why is Ansible considered a declarative automation tool?
- What is idempotency? How does Ansible implement it?
- Is Groovy a functional or imperative programming language?