Essentials of Object Oriented and Functional Programming: A Guide to Modular Code
Tags: beginners, programming, productivity
Published: 2023-07-27 14:29:08

## What is an Object in Programming?

When someone asks, "What is an object in programming?" I like to explain it in a simple way that even Grandma can understand.

**Example 1:** Imagine a room where every person who enters receives a package with a box of pencils and an empty sheet of paper. People take a pencil from the box, draw on the paper, put the pencil back, take another one, and when they finish drawing, they throw the package in a garbage basket to keep the room clean.

Here are the key points to understand:

1. **Initialization**: When someone gets a package with pencils and a blank sheet of paper, we call it "initialization." This happens for everyone in the same way.
2. **Functions**: Each pencil used to draw shapes is called a "function," and it can be reused for different drawings.
3. **Data**: The sheet of paper inside the package is the "data," and the function-pencil changes its state by creating drawings.
4. **Equality**: Two drawings are considered "equal" if their shapes are the same, similar to using a comparison operator in programming.
5. **Object**: The entire package, including both the data (paper) and the functions (pencils), is called an "object."
6. **Garbage Collection**: After finishing their drawings, people dispose of the packages in a garbage basket. A serviceman ("garbage collector") comes periodically to empty the basket and free up space.

![That's it](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0tvi6bbtn7pyru95gix.jpg)

In summary, objects in programming are like these packages containing data and functions, helping us organize and manage data efficiently.

## What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects.

In **Example 1**, we have a basic package that allows people to draw on a blank sheet of paper. To embrace OOP, we need to extend the package's capabilities:

1. **Extending the Package**: We enhance the package to reset the paper (clean data), change its size, and store information on available free space. This way, the package becomes more versatile, adapting to different needs.
2. **Designing Software around Data States**: Embracing OOP means designing the software model around the data and its states. The extended package becomes a powerful tool with various functionalities and data states.

OOP is prevalent in frameworks offering objects, making it easier to apply these principles in real-world applications.

## What is the Opposite of Object-Oriented Programming?

![Opposite side](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4actgnu67o29f849t7n.jpg)

The opposite of OOP is **Functional Programming** (FP). In FP, the concept of states and orders is discarded. Let's consider a few examples:

1. **Mathematical Calculations**: In FP, the order of mathematical calculations doesn't matter; the result remains the same. For example, 1 + 2 + 3 is equivalent to 3 + 2 + 1. FP focuses on the collection of functions and their return values.
2. **Multithreading and Multiprocessing**: FP is evident in scenarios involving multithreading and multiprocessing, where communication between processes is achieved through remote procedure calls (RPC). FP emphasizes passing data and obtaining results without managing application states.
3. **Web Services**: Web services often use FP principles by utilizing APIs to send and receive data without concerning themselves with underlying state changes.

In contrast to OOP, where objects have inheritance and the availability of methods and data depends on the object's state, FP disregards the concept of state and focuses on the collection of functions and their results. This fundamental difference shapes how software is designed and implemented in each paradigm.

## Modularity

Another example of **Functional Programming** is communication via interfaces. For instance, in C++, we can declare an interface and use it in other classes:

```cpp
class Interface
{
public:
    virtual void foo() = 0;
	virtual void bar() = 0;
};

class Object
{
public:
   Object(Interface & api) : _api(api)
   {
   }

private:
	Interface & _api;
};
```

Here, our class `Object` can call methods of `Interface`, focusing on the API rather than the implementation. Any instance of `Interface` can be used, and the `Object` may not know anything about the internal data and its states.

This showcases how **Functional Programming** promotes modularity, focusing on the API provided by modules rather than their internal states. Modular software is easier to use and test, as the emphasis is on function calls and return values. Functional tests can be performed, testing multiple modules together, leading to better-tested and scalable applications.

## Software Examples

Here are a few examples of libraries related to Object-Oriented Programming (OOP) and Functional Programming (FP) used in application development:

- **OOP Libraries**: STL (Standard Template Library), Qt, JCL (Java Class Library), and .NET FCL (Framework Class Library) are collections of Objects used in application development. They offer a wide range of functionalities and are designed with a focus on OOP principles.

- **FP Libraries**: [gRPC](https://github.com/grpc/grpc), [ZeroMQ](https://github.com/zeromq/libzmq), and [AREG](https://github.com/aregtech/areg-sdk) are examples of libraries with a special focus on providing possibilities for Interprocess Communication. Developed using C++, they facilitate communication through predefined APIs, emphasizing functional programming concepts.

## What is Better?

![What is better](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbhy8cxr24jnl43z9nx7.png)

The question of whether OOP or FP is better depends on the software and architecture being used. OOP and FP are application design patterns that can coexist and complement each other. Some scenarios may benefit more from one paradigm than the other.

For example, OOP might be suitable for building complex systems with varying functionalities and data states, while FP might excel in situations where modularity and focus on APIs are critical. Ultimately, the choice depends on the specific needs and requirements of the software being developed. Both paradigms have their strengths and can be powerful tools in the hands of skilled developers.