Table of Contents

OO vs Procedural programming

Object-oriented programming (OOP)

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects.
Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and implemented in code).

Alan Kay's (essential)version of the OOP

I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages (so messaging came at the very beginning – it took a while to see how to do messaging in a programming language efficiently enough to be useful).

(I'm not against types, but I don't know of any type systems that aren't a complete pain, so I still like dynamic typing.)
OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.
It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them. Alan Kay

Only Messaging: In OOP, the primary means of interaction between objects is through messages.
Instead of calling functions directly or manipulating data structures, objects communicate by sending messages to one another. This encapsulates behavior and promotes a more modular design.

Local Retention: This refers to the idea that an object should manage its own state internally.
Each object retains its own state (data) and is responsible for its own behavior, which helps to maintain integrity and reduces dependencies between objects.

Protection and Hiding of State-Process: This principle is about encapsulation, where the internal state of an object is hidden from the outside world. Objects expose only what is necessary through their public interface, protecting their internal workings and preventing external entities from directly modifying their state.

Extreme Late-Binding: Late binding (or dynamic binding) is a concept where method calls are resolved at runtime rather than compile time. This allows for more flexible and dynamic behavior in programs, as the specific method that gets invoked can depend on the actual object type at runtime, rather than being determined statically.

Can be Done in Smalltalk and in LISP: Kay points out that these principles of OOP can be implemented in various programming languages, particularly Smalltalk, which is known for its pure OOP model, and LISP, which supports functional programming and can also encapsulate OOP concepts.

Modern view on the OOP

OOP is primarily about behavior (functions) rather than just data.
Objects should be seen as "bags of functions," not just "bags of data.".

Encapsulation is crucial. Data should be hidden within the object, and access to it should be controlled through methods.

With help of inheritance,

Interaction between objects:

User user = new(30, "123456", "Alan", "Kay");
user.ChangeFirstName("Bob");
user.ChangePassword("password");

internal class FullName {
    private string firstName;
    private string lastName;

    public FullName(string firstName, string lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public void ChangeFirstName(string firstName)
    {
        this.firstName = firstName;
    }
}

public class User {
    private int age;
    private string password;
    private FullName fullName; 

    public User(int age, string password, string firstName, string lastName) {
        if (age < 0 || age > 150)
        {
            throw new ArgumentException("Invalid age.");
        }
        this.age = age;
        // Creating an object that will become a part only of the `User` object
        this.fullName = new FullName(firstName, lastName);
        this.password = password;
    }

    public void ChangePassword(string password) { 
        this.password = password;
    }

    public void ChangeFirstName(string firstName) {
        this.fullName.ChangeFirstName(firstName);
    }
}

Procedural programming

Procedural programming is a programming paradigm, that involves implementing the behavior of a computer program as procedures (a.k.a. functions, subroutines) that call each other and manipulates data structures directly.

Interaction between several Records and Procedures

User user = new();
AgeValidationService ageValidationService = new();
int age = 30;
ageValidationService.ValidateAge(age);
user.age = age;

user.password = "123456";
user.fullName = new FullName();
// Record can be changed directly
user.fullName.firstName = "Alan";
user.fullName.lastName = "Kay";
UserService userService = new();
// Record can be changed by methods
userService.ChangePassword(user, "password");
FullNameService fullNameService = new();
userService.ChangeFirstName(fullNameService, user, "Bob");

public class FullName
{
    public string firstName;
    public string lastName;
}

public class User
{
    public int age;
    public string password;
    public FullName fullName;
}

class FullNameService
{
    // Procedure
    public void ChangeFirstName(FullName fullName, string firstName)
    {
        fullName.firstName = firstName;
    }
}

class AgeValidationService
{
    // Procedure
    public void ValidateAge(int age)
    {
        if (age < 0 || age > 150)
        {
            throw new ArgumentException("Invalid age.");
        }
    }
}

class UserService
{
    // Procedure
    public void ChangePassword(User user, string password)
    {
        user.password = password;
    }

    // Procedure
    public void ChangeFirstName(FullNameService service, User user, string firstName)
    {
        service.ChangeFirstName(user.fullName, firstName);
    }
}

OOP vs PP

Procedural Object-oriented
Procedure Method
Record Object
Module Class
Procedure call Message

The biggest pitfall of procedural programming is that it is difficult to create well-supported code that is loosely coupled and easily extended. Changes made in one part of a project often break code in other parts. Procedures and records that are not intended for use in other "Services" are frequently used, complicating the entire system.
The advantages of procedural programming include faster overall program execution, as the runtime does not need to recreate chains of virtual methods to find the correct method to execute.

Anemic Domain Model

The anemic domain model is described as a programming anti-pattern where domain objects contain little or no business logic. Business logic is often separated from data and placed in separate "Service" classes. The anemic domain model resembles a procedural programming style when using an object-oriented language.