DEV Community

Cover image for The Blueprint of Logic
Mohamed Khaled Yousef
Mohamed Khaled Yousef

Posted on

The Blueprint of Logic

Hello developer! glad to see you again 👋

Introduction: The Intersection of Philosophy and Software

At first glance, software engineering might seem like a purely technical field. After all, it’s about writing code, creating systems, and solving practical problems, right? But when you look deeper, you’ll find that software engineering is deeply rooted in some of the same fundamental questions that philosophers have been asking for centuries:

  • What is logic?
  • How do we know something is true?
  • How can complex ideas be broken down into simpler, understandable parts?

These questions form the foundation of both philosophy and software engineering. Just as philosophers seek to understand understanding in general, they seek to understand the nature of reality, thought and how things related together, software engineers use logic and abstraction to build the systems that power the digital world.

The Blueprint of Logic

At the heart of software engineering lies logic, a concept that predates modern computing by thousands of years. It was philosophers like Aristotle who first formalized logical reasoning, creating the basis for what we now call formal logic — the foundation of every software system.

In software, logic is what allows us to define clear, step-by-step processes that computers can follow. It’s the guiding framework for writing algorithms, designing systems, and solving problems.

The Foundations of Logic
In philosophy, logic is about reasoning and ensuring that conclusions follow from premises. There are different types of logic, but two forms are particularly relevant to software engineering:

1. Deductive Logic:

  • This is the process of drawing conclusions based on premises that are known to be true. So deductive logic deals with certainty. In software, this is akin to creating functions or methods that always yield predictable outcomes based on given inputs. So, This often mirrors the predictable flow of functions, where if certain conditions are met, the output will always follow specific rules.

  • Example 1:

function isEven(number) {
  return number % 2 === 0;
}
Enter fullscreen mode Exit fullscreen mode
  • General Premise: The function isEven accepts one number and return boolean value true or false. isEven must meet certain condition.
  • Specific Case: If isEven satisfies this condition ( number % 2 === 0 ), it is an even number.
  • Conclusion: Based on the premises, the function returns true for even number or false otherwise.

In this function, the logic is simple and deductive: if the input is a number divisible by 2, the number will be even number and the output will always be true.

  • Example 2:

In TypeScript, deductive logic is often used to infer types and ensure that functions receive and return values of the expected types.

function addNumbers(a: number, b: number): number {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode
  • General Premise: The function addNumbers accepts two numbers and returns a number.
  • Specific Case: If we pass 5 and 10 as arguments, they must both be numbers.
  • Conclusion: The function will return 15, a number, according to the premise.

In both examples, the logic is deductive because the conclusion follows necessarily from the premises.

2. Inductive Logic:

  • While deductive logic deals with certainty, inductive logic deals with probability. Inductive logic works from specific examples or data to create a general rule or pattern. In software engineering, this is similar to machine learning, where patterns are discovered from data rather than predetermined rules so systems infer general rules from specific examples.

In recommender systems, like those used by Netflix or Spotify, they use patterns from past data to predict future user behavior. inductive logic is used to suggest content to users based on their behavior. For example, if a user frequently watches science fiction movies, the system might recommend more science fiction content. So the system "learns" through induction, refining its predictions over time based on observed trends.

Here, the conclusion is probable but not certain. The user might like other genres, but based on past behavior, science fiction is the best bet.

From Philosophy to Code: Abstraction

Another core principle shared by philosophy and software engineering is abstraction. Philosophers use abstraction to understand understanding and think about concepts like justice, beauty, and truth — things that can’t be easily defined in physical terms. In software, abstraction helps manage complexity, allowing engineers to focus on high-level ideas without getting bogged down in the details.

  • Abstraction in Philosophy: Plato’s idea of forms is a great example. According to Plato, everything we see in the physical world is an imperfect version of a perfect "form" or idea. For instance, there might be many different kinds of chairs, but they all share the essence of "chair-ness."

This kind of abstraction helps philosophers talk about big, complex ideas like beauty, justice, or truth without needing to focus on every individual instance.

  • Abstraction in Software: Object-Oriented Programming (OOP), for example, is an exercise in abstraction. Just as philosophers abstract concepts from the real world, software engineers abstract real-world entities into classes and objects.

Similarly, in OOP, we might have different kinds of classes, like Car, Truck, and Bicycle, but they all inherit from the abstract class Vehicle. This allows us to create a general model (Vehicle) and then define specific behaviors (Car, Truck) without repeating code.

So, in software engineering, abstraction helps developers manage complex systems by focusing on high-level designs without worrying about lower-level details.

  • Example 1: What is an Animal class? ( OOP Abstractions )
class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    return `${this.name} makes a sound!.`;
  }
}

class Dog extends Animal {
  speak() {
    return `${this.name} barks.`;
  }
}

class Cat extends Animal {
  speak() {
    return `${this.name} meow.`;
  }
}

const myDog = new Dog('Jo');
const myCat = new Cat('Saeed');
console.log(myDog.speak());
console.log(myCat.speak());
Enter fullscreen mode Exit fullscreen mode

The Animal class is an abstraction representing the idea of any animal. It defines the essential behavior (speaking), but each specific animal (like Dog or Cat) can provide its own implementation.

  • Example 2: How do I interact with API ( APIs Abstractions )
const apiKey = 'YOUR_PRIVATE_API_KEY'; 
const city = 'Cairo';
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;

fetch(url)
  .then(response => response.json())
  .then(data => {
    console.log(`The weather in ${city}:`);
    console.log(`Temperature: ${(data.main.temp - 273.15).toFixed(2)}°C`);
    console.log(`Weather: ${data.weather[0].description}`);
  })
  .catch(error => console.error('Error fetching weather data:', error));

Enter fullscreen mode Exit fullscreen mode

An API (Application Programming Interface) abstracts the complexity of an entire system or service. For example, when we use a weather API, we don’t need to know how it gathers data from satellites, processes it, or computes weather predictions.

The API provides an interface that abstracts away the complexity of weather data processing. You simply make a request, get the result and bind the data as you like in your page.

  • Example 3: Database Abstraction ( ex Sequelize ORM )
User.findOne({ where: { id: 1 } })
  .then(user => {
    console.log(user.name);
  });
Enter fullscreen mode Exit fullscreen mode

In many web applications, we don’t need to know exactly how data is stored or retrieved in the database. Instead, we interact with an abstraction layer, such as an Object Relational Mapping (ORM) system like Sequelize or Hibernate, which provides methods to work with data in terms of objects.

The developer doesn’t need to write SQL queries or understand how the database works at a low level. The ORM abstracts the database, allowing the developer to work with familiar objects like the user object in our example and methods not the database directly.

Algorithms: The Roadmaps of Thought

An algorithm is, at its core, a series of logical steps to solve a problem. In many ways, algorithms are the modern software engineer’s version of syllogisms, a form of logical reasoning introduced by Aristotle.

For example, a classic syllogism looks like this:

1- All men are mortal.
2- Socrates is a man.
3- Therefore, Socrates is mortal.

Another example,
1- All mammals are animals.
2- All elephants are mammals.
3- Therefore, all elephants are animals.

This is a clear logical process, and algorithms function in a similar way. Let’s break down a factorial example:

function factorial(n) {
  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}
Enter fullscreen mode Exit fullscreen mode
  • Logical Breakdown:
  1. If the number n is 0, return 1.
  2. Otherwise, multiply n by the result of factorial(n - 1). Like the syllogism, this algorithm follows a clear and logical path, providing a solution to the problem of calculating the factorial of a number.

result
factorial of 0 => 1
factorial of 1 => 1
factorial of 2 => 2
factorial of 3 => 6
factorial of 4 => 24
factorial of 5 => 120

Logic Gates and Boolean Algebra

In philosophy, logic is relate to mathematic. and about making distinctions between true and false. The binary nature of modern computing — where everything is either a 1 (true) or a 0 (false) — has its roots in Boolean algebra, named after 19th-century mathematician and logician George Boole.

AND, OR, and NOT: These basic logical operations are the foundation of how computers make decisions. For instance:

  • AND: Both conditions must be true for the result to be true.
  • OR: At least one condition must be true for the result to be true.
  • NOT: The condition must be false for the result to be true.

In software, these logical gates are essential for controlling the flow of a program. For example, in conditional statements ( ex, using if condition in programming ), you might use Boolean logic to determine whether to execute a block of code:

if (isLoggedIn && hasPermission) {
  accessGranted();
}

if (!name || !email) {
  signUp();
}

Enter fullscreen mode Exit fullscreen mode

So, This simple line of code is built on the philosophical principles of Boolean logic.

Logic Gates and Boolean Algebra

Conclusion: The Mind Behind the Code

When we write code, we aren’t just creating instructions for a machine; we’re engaging in a process of structured thought. Just as philosophers rely on logical frameworks to explore truth and reason, software engineers use logic to build and maintain the digital world.

Understanding the philosophical roots of software engineering can help us see beyond the syntax and frameworks we use every day. It reveals the universal principles that underpin both our thinking and our code. Logic is more than just a tool — it’s the blueprint of everything we build, and it helps us bring clarity and order to the complex problems we solve.

By exploring these connections, we can elevate our craft from engineering to a thoughtful and reflective practice.


Thank you for reading and any contribution is more than welcome in the threads below!

Top comments (0)