TechByteByByte

Function (Rule-based Logic)

The 'if this, then that' approach to software that came before Machine Learning — and why it still matters even in an AI-driven world.

#rule-based-logic#functions#foundations#traditional-programming

A machine following a recipe

A cooking recipe says:

If the water is boiling, add the pasta.
Cook for 10 minutes.
Then drain the water.

A computer rule works in a similar way. A person describes a condition and the action that should follow.

IF a condition is true
THEN perform an action
ELSE perform a different action

The machine does not need to discover the rule. A programmer or policy author supplies it.

Follow a tiny function

def ticket_price(age):
    if age < 12:
        return 100
    else:
        return 200

Now call ticket_price(10):

  1. The input 10 is placed into age.
  2. The computer checks 10 < 12.
  3. The condition is true.
  4. The function returns 100.
age = 10 → check rule → true → price = ₹100

Nothing was trained. No examples taught the function. The result came from an explicit human-written rule.

Where the rule stops working

What happens on a child’s twelfth birthday? The condition age < 12 becomes false, so the price changes to ₹200. This is a boundary case—a value at the edge between two rule outcomes.

What happens if the input is "ten" rather than 10? The function may fail because it expects a number. Rules still require correct input and careful testing.

Key terms

  • Function: A reusable block of instructions that can receive input and return output.
  • Condition: A question whose answer is true or false.
  • Branch: One path selected after checking a condition.
  • Deterministic: The same valid input follows the same rules and produces the same result.
  • Boundary case: An input at an important edge, such as exactly age 12.

Check your understanding

Does a long collection of if/else rules automatically become Machine Learning? No. It remains rule-based logic because humans supplied the decisions.

Are rules outdated now that AI exists? No. Rules remain ideal for exact policies, validation, permissions, calculations, and safety boundaries.

Before we go any further into how AI systems learn patterns, it’s worth slowing down and looking closely at the approach that came before learning — because you can’t fully appreciate what makes Machine Learning different until you’ve seen its predecessor clearly.

The simple definition

A function, in the rule-based sense, is a fixed, human-written set of instructions: if this condition is true, do that specific thing. It’s the classic building block of traditional programming — a recipe with no room for interpretation. Give it the same input twice, and it will produce the exact same output twice, forever, because nothing about it changes or adapts.

You’ve written or seen something like this even if you’ve never coded:

IF temperature > 30°C:
    output = "It's hot"
ELSE IF temperature > 15°C:
    output = "It's mild"
ELSE:
    output = "It's cold"

That’s rule-based logic in its purest form: a human decided the thresholds (30°C, 15°C), a human decided the categories, and the function just mechanically checks and reports. No learning happens anywhere in this process.

Why this needs its own place in the sequence

Back in the Artificial Intelligence article, we drew the core distinction of this entire field: rules written by a human, versus rules learned from data. This article is about the first half of that distinction — the approach that dominated software for decades before Machine Learning became practical, and one that’s still absolutely everywhere today, often working quietly alongside AI rather than being replaced by it.

ANALOGY vs. TECHNICAL REALITY

Analogy: Think of rule-based logic like a strict recipe card. “If the dough hasn’t risen after 2 hours, add more yeast next time.” Follow the card exactly, every time, and you get the exact same result every time. The recipe doesn’t improve on its own — it only changes if a human decides to rewrite it.

Where this breaks down: A cook occasionally improvises when something looks off, using judgment the recipe card doesn’t capture. A rule-based function has zero capacity for that kind of judgment. It only ever does exactly what’s written, which is both its greatest strength (total predictability) and its core weakness (total inflexibility) — a trade-off you’ll want to keep in mind for the rest of this article.

Where rule-based logic genuinely shines

It’s easy, in a glossary about AI, to make rule-based logic sound outdated. It isn’t. For clearly defined, unambiguous problems, it’s often the better choice, not just the older one:

  • Calculating sales tax on a purchase.
  • Validating that a password meets length and character requirements.
  • Deciding whether a bank account balance can support a withdrawal.
  • Converting a temperature from Celsius to Fahrenheit.

None of these need “learning.” The correct logic is fully known in advance, doesn’t change based on subtle patterns in data, and needs to be 100% predictable and auditable — properties that a learned model, as you’ll recall from the Machine Learning article, generally can’t guarantee. You’d never want a bank’s core balance calculation to be a probabilistic model that’s “usually right.”

Where it breaks down

Rule-based logic struggles exactly where Machine Learning excels: tasks with too much variation, ambiguity, or complexity to write down as a finite set of conditions. Recall the spam-filter example from earlier articles — you could write “IF email contains ‘lottery’, THEN mark as spam,” but spammers adapt around fixed rules almost immediately, and no human can keep writing new rules fast enough to keep pace. The moment a task’s underlying pattern is too fuzzy or fast-changing to fully specify in advance, rule-based logic starts to break down, and that’s the exact gap Machine Learning was built to fill.

How they actually work together in real systems

A common misconception is treating “rule-based” and “AI” as two competing camps where one replaces the other. In real production systems, they’re usually teammates. Take the fraud detection example from the Output article: the model produces a probability score (learned, pattern-based). But the decision “flag anything above 0.7 for review” is a plain rule — a human-set threshold, rule-based logic, sitting right on top of the model’s output. Most real AI-powered products are layered exactly like this: learned models handling the fuzzy pattern-recognition part, rule-based logic handling the clear-cut decision and safety-check part around it.

flowchart LR
    A[Input] --> B[Learned Model: pattern recognition]
    B --> C[Rule-based logic: threshold / decision]
    C --> D[Final Output]

Follow one rule step by step

Consider a school-entry rule: students may enter when they have an ID card and arrive before 8:30 AM.

def can_enter(has_id_card, arrival_hour, arrival_minute):
    arrived_on_time = (arrival_hour, arrival_minute) <= (8, 30)

if has_id_card and arrived_on_time:
        return "Allow entry"

return "Send to the office"

Execution for can_enter(True, 8, 20):

has ID card? ──► yes
on time?     ──► yes
both true?   ──► yes
output       ──► "Allow entry"

The computer does not learn this policy or understand why it exists. A human chose the conditions, and the function applies them exactly.

Function, rule, algorithm, and model

  • A function is a reusable block that accepts input and returns an output or performs an action.
  • A rule states what should happen under a specific condition.
  • An algorithm is a sequence of steps for solving a problem.
  • A model is a learned or designed representation used to make predictions or generate results.

A function can contain rules, implement an algorithm, or call a Machine Learning model. These words describe different aspects of a system.

Rules and models in one production flow

Imagine an online payment system:

flowchart LR
    A[Payment input] --> B{Hard rule: invalid card?}
    B -->|Yes| C[Reject]
    B -->|No| D[Fraud model produces risk score]
    D --> E{Rule: score above threshold?}
    E -->|Yes| F[Human review]
    E -->|No| G[Approve]

Fixed rules handle facts the business knows with certainty. The model handles fuzzy patterns that would require thousands of brittle rules. More rules can also surround the model to validate input, enforce permissions, limit actions, and provide safe fallbacks.

When rules are the better choice

Use rules when:

  • The policy is explicit, stable, and complete.
  • The same input must always produce the same decision.
  • Auditors or users must see exactly why a decision occurred.
  • An error is unacceptable and uncertainty adds no value.

Rules become difficult when exceptions multiply, conditions change constantly, or the task involves language, images, or subtle patterns. A giant rule system can become a tangled “if/else forest” that is hard to test and maintain.

Common rule-based mistakes

  • Forgetting boundary cases such as exactly 8:30
  • Writing overlapping or contradictory rules
  • Hiding business rules throughout the code instead of keeping them testable
  • Assuming user input is valid or safe
  • Adding thousands of exceptions when a learned model may fit the fuzzy part better
  • Using a probabilistic model where a short, exact rule would be safer

The same problem solved with a rule and a model

Rule-based alert:
if temperature > 38°C → show "high temperature"

Learned risk estimate:
temperature + age + symptoms + history → model → illness risk

The rule is easier to inspect and guarantees the written threshold. The model can combine complicated patterns, but its answer is uncertain and must be evaluated. Real medical software may use the model to estimate risk and fixed rules to prevent unsafe actions.

A rule is deterministic when the same input and the same external state are used. If the function reads the current time, a changing database, or a random value, apparently identical user input can still produce a different result.

Where this fits in what comes next

Function and rule-based logic represent the “before” picture in AI’s story — precise, predictable, but rigid. The next article, Pattern, goes in the opposite direction: what a pattern actually is, and why finding patterns in data is the whole reason Machine Learning exists in the first place. Holding both of these side by side — fixed rules vs. discovered patterns — is what makes the rest of this glossary click into place.

In one sentence

Rule-based logic is software built entirely from human-written “if this, then that” instructions — perfectly predictable and still essential today, but fundamentally unable to handle the fuzzy, high-variation problems that Machine Learning was built to solve instead.

Author
TechByteByByte Editorial Team
Reviewed by
TechByteByByte Admin
Published
Last reviewed