Fundamentals – Basic Principles
Program: Turner (ITI)
Subject: Fundamentals
Topic: Basic Principles
These notes give a quick, clear snapshot of the core ideas that underpin all programming and software‑engineering work. They’re meant to help you revise, test yourself, and build a mental framework you can apply to any language or project.
1. Simple Explanation
A basic principle is a foundational rule or concept that, once understood, lets you reason about more complex ideas.
In software, the most fundamental principles are:
| Principle | What it means | Why it matters |
|---|---|---|
| Abstraction | Hiding complexity behind a simpler interface. | Enables reuse and protects you from low‑level bugs. |
| Modularity | Splitting a system into discrete, interchangeable parts. | Makes code easier to understand, test, and maintain. |
| Encapsulation | Bundling data and the operations that manipulate it. | Provides data protection and clear ownership. |
| Separation of Concerns | Each component solves one problem. | Leads to clearer, more maintainable code. |
| DRY (Don’t Repeat Yourself) | Avoid duplicated logic. | Reduces bugs, eases changes. |
| KISS (Keep It Simple, Stupid) | Prefer simple solutions over clever ones. | Easier to read, test, and debug. |
| YAGNI (You Aren’t Gonna Need It) | Don’t add features until they’re needed. | Saves time and keeps the codebase lean. |
| Composition over Inheritance | Build behaviour by combining components. | Reduces tight coupling and improves flexibility. |
2. Key Points
| # | Point | Practical Tip |
|---|---|---|
| 1 | Identify the problem domain before coding. | Write a plain‑English description of what the code should do. |
| 2 | Choose the right level of abstraction. | e.g., a List abstraction hides the underlying array. |
| 3 | Keep functions small (≤ 20 lines, ≤ 5 parameters). | A small function does one thing well. |
| 4 | Use descriptive names for variables, functions, classes. | calculateTax() is better than calc() or f1(). |
| 5 | Write unit tests for every new feature. | “Test‑driven development” ensures correctness. |
| 6 | Document public interfaces. | Comments, docstrings, or Markdown files. |
| 7 | Handle errors gracefully. | Use exceptions or error codes consistently. |
| 8 | Prefer immutable data where possible. | Reduces side‑effects and bugs. |
| 9 | Refactor regularly. | Turn duplicated code into reusable functions. |
| 10 | Follow language idioms. | Don’t reinvent the wheel; use standard libraries. |
3. Worked Example
Scenario: Build a simple “Book Store” module that lets us add books, list them, and find a book by ISBN.
3.1 Problem Decomposition
| Problem | Component |
|---|---|
| Store books | Book class |
| Persist data | BookRepository interface |
| Business logic | BookService |
| User interface | BookController |
3.2 Code Skeleton (Python‑like)
# book.py
class Book:
def __init__(self, isbn: str, title: str, author: str):
self.isbn = isbn
self.title = title
self.author = author
# repository.py
from abc import ABC, abstractmethod
class BookRepository(ABC):
@abstractmethod
def add(self, book: Book) -> None: ...
@abstractmethod
def list(self) -> list[Book]: ...
@abstractmethod
def find_by_isbn(self, isbn: str) -> Book | None: ...
class InMemoryBookRepository(BookRepository):
def __init__(self):
self._books = []
def add(self, book: Book) -> None:
self._books.append(book)
def list(self) -> list[Book]:
return list(self._books)
def find_by_isbn(self, isbn: str) -> Book | None:
return next((b for b in self._books if b.isbn == isbn), None)
# service.py
class BookService:
def __init__(self, repo: BookRepository):
self._repo = repo
def register_book(self, isbn: str, title: str, author: str) -> None:
if self._repo.find_by_isbn(isbn):
raise ValueError("ISBN already exists")
book = Book(isbn, title, author)
self._repo.add(book)
def get_all_books(self) -> list[Book]:
return self._repo.list()
def get_book(self, isbn: str) -> Book:
book = self._repo.find_by_isbn(isbn)
if not book:
raise KeyError("Book not found")
return book
# controller.py
class BookController:
def __init__(self, service: BookService):
self._service = service
def create(self, data):
self._service.register_book(**data)
def list(self):
return [b.__dict__ for b in self._service.get_all_books()]
def read(self, isbn):
return self._service.get_book(isbn).__dict__
3.3 Applying Principles
| Principle | How it appears in the example |
|---|---|
| Abstraction | BookRepository hides persistence details. |
| Modularity | Separate Book, Repository, Service, Controller. |
| Encapsulation | Book holds its own data. |
| Separation of Concerns | Business logic in Service; data storage in Repository. |
| DRY | InMemoryBookRepository uses a single list for storage; logic reused. |
| KISS | Functions are short, clear, and focus on one task. |
| YAGNI | No caching or database layer added until needed. |
| Composition over Inheritance | BookService composes a repository rather than inheriting from it. |
4. Quick Revision Summary
| Concept | What to remember | Self‑check question |
|---|---|---|
| Abstraction | Simplify complex reality with a clean interface. | What interface does BookRepository provide? |
| Modularity | Split code into small, independent parts. | Which component owns the persistence logic? |
| Encapsulation | Keep |