Basic Software Engineering Concepts¶
Chomsky Hierarchy¶
The Chomsky Hierarchy is a classification of formal grammars (and the languages they generate) into four types, organized from the most general (most expressive) to the most restricted (least expressive).
It’s a core concept in formal language theory, automata theory, and compiler design — originally introduced by Noam Chomsky in 1956 to describe the structure of natural languages but now widely used in computer science.
The Four Levels of the Chomsky Hierarchy¶
Type | Grammar Name | Language Class | Equivalent Automaton | Rules / Restrictions |
---|---|---|---|---|
Type 0 | Unrestricted grammar | Recursively enumerable languages | Turing Machine | No restrictions on productions: α → β, where α and β are any strings of terminals/nonterminals and α ≠ ε. |
Type 1 | Context-sensitive grammar | Context-sensitive languages | Linear bounded automaton (Turing machine with tape limited to input length) | Productions of the form αAβ → αγβ, where γ ≠ ε (length of output ≥ length of input) |
Type 2 | Context-free grammar (CFG) | Context-free languages | Pushdown Automaton (PDA) | Productions of the form A → γ, where A is a single nonterminal |
Type 3 | Regular grammar | Regular languages | Finite State Automaton (FSA) | Productions of the form A → aB or A → a (right-linear or left-linear) |
Why It Matters¶
-
Compiler design:
-
Regular languages → Lexical analysis (regex, tokenizing)
- Context-free languages → Syntax analysis (parsing)
- Theoretical computer science: Helps in classifying computational problems by their complexity.
- Natural language processing: Models grammatical structures of human languages.
Example of Restrictions¶
Let’s see an example where a grammar moves down the hierarchy:
- Type 0:
S → aSb | ε
(No restrictions — works fine in Type 0 and all below if it matches the form.)
- Type 1: Must be length non-decreasing:
AB → AC
C → b
- Type 2: Only single nonterminal on LHS:
S → aSb | ε
- Type 3: Only one terminal on RHS and at most one nonterminal:
S → aA
A → bS | ε
Programming Paradigms¶
Programming paradigms are fundamental styles or approaches to programming that shape how solutions are designed and implemented. Below are some of the major paradigms:
1. Object-Oriented Programming (OOP)¶
Key Concepts: Objects, Classes, Inheritance, Encapsulation, Polymorphism Languages: Java, C++, Python, C#, Ruby Description: OOP organizes code around objects, which are instances of classes—blueprints that define attributes and behaviors. It promotes code reuse, modularity, and scalability, making it ideal for large and complex software systems.
2. Functional Programming (FP)¶
Key Concepts: Pure Functions, Immutability, First-Class Functions, Higher-Order Functions Languages: Haskell, Lisp, Elixir, Scala, JavaScript (partial) Description: FP emphasizes declarative code using pure functions and avoids mutable state and side effects. It’s rooted in mathematical functions and is well-suited for concurrent and parallel programming.
3. Procedural Programming (PP)¶
Key Concepts: Procedures (Functions), Sequence, Selection, Iteration Languages: C, Pascal, Fortran, BASIC Description: Procedural programming structures programs as a series of instructions grouped into procedures or routines. It is a subset of imperative programming and is suitable for applications with a clear step-by-step logic.
4. Declarative Programming (DP)¶
Key Concepts: What to Do (Not How), Abstraction, Constraints Languages: SQL, HTML, Prolog, Haskell Description: Declarative programming expresses the logic of computation without describing its control flow. The focus is on what the program should accomplish rather than detailing how to achieve it.
5. Logic Programming (LP)¶
Key Concepts: Facts, Rules, Queries, Inference Languages: Prolog, Datalog Description: Logic programming is based on formal logic. Programs consist of a set of facts and rules, and computation is performed through logical inference. It is widely used in AI, natural language processing, and expert systems.
Code Component Levels¶
1. Expression / Statement¶
Description: The most basic unit of code that performs an action or produces a value. Examples:
x = 5 # Statement
y = x + 2 # Expression within a statement
2. Function¶
Description: A reusable block of code designed to perform a specific task, often with inputs (parameters) and outputs (return values). Example:
def add(a, b):
return a + b
3. Class¶
Description: A blueprint for creating objects. Classes group related data and methods (functions) together, supporting object-oriented design. Example:
class Calculator:
def add(self, a, b):
return a + b
4. File¶
Description:
A physical file (e.g., .py
, .js
) containing source code, which may include multiple functions, classes, or even runnable scripts.
Example:
calculator.py
might contain all calculator-related classes and functions.
5. Module¶
Description: A file or collection of files that define a namespace and can be imported into other files. Modules allow encapsulation and reuse. Example:
import math
from calculator import add
6. Sub-package, Name-space package¶
Description:
A sub-package is a package contained within another package (a nested directory structure). It helps organize larger packages into logical groups, e.g., utils/text/
under utils/
.
A namespace package is a logical package that can be spread across multiple directories on sys.path
(in Python, enabled by PEP 420). Unlike regular packages, a namespace package may not have a single __init__.py
file and can be extended by separate distributions.
Examples:
Regular package with sub-packages:
myapp/
utils/
__init__.py
text/
__init__.py
tokenize.py
math/
__init__.py
stats.py
from myapp.utils.text import tokenize
Namespace package (pieces located in multiple roots on sys.path):
/src/pkg_a/acme/plugins/foo.py
/src/pkg_b/acme/plugins/bar.py
# When both /src/pkg_a and /src/pkg_b are on sys.path
from acme.plugins import foo, bar
7. Package¶
Description:
A collection of related modules organized in a directory with an optional __init__.py
file. Packages help group functionality logically.
Example:
numpy
, scikit-learn
, or your own directory like myutils/
8. Project¶
Description: A complete application or system that may consist of multiple packages, configurations, tests, and documentation. Example: A Django web app or a machine learning pipeline.
9. Platform¶
Description: A larger ecosystem or environment where projects are deployed, integrated, or distributed. May include OS, runtime environments, SDKs, or cloud infrastructure.
Layers in Software Architecture¶
Software systems are often organized into logical layers, each responsible for a specific concern. This separation helps manage complexity, supports scalability, and improves testability.
1. Data Layer (Persistence Layer)¶
Responsibility: Manages storage, retrieval, and manipulation of data from databases, file systems, or external APIs. Key Components:
- Database access (SQL, ORM)
- Data models / entities
- Repositories / DAOs Examples:
- PostgreSQL, MongoDB
- SQLAlchemy, Hibernate
UserRepository
,ProductDAO
2. Application / Business Layer (Service Layer)¶
Responsibility: Implements business logic and rules. It coordinates between the data and presentation layers, encapsulating core operations of the application. Key Components:
- Services
- Use cases
- Domain logic Examples:
OrderService
,AuthManager
- Validating input, calculating discounts, enforcing rules
3. Presentation Layer (UI Layer)¶
Responsibility: Handles user interaction and displays output. This layer communicates with the application layer to send input and present results. Key Components:
- Web or mobile UI
- Controllers / ViewModels
- Frontend frameworks Examples:
- HTML, React, Flutter
LoginController
,DashboardPage
Optional Extensions¶
If you're interested in a more complex architecture (like enterprise systems), you might also include:
- API Layer: For REST/GraphQL endpoints that expose services to clients.
- Integration Layer: Manages communication with external systems and services (e.g., third-party APIs).
- Security Layer: Handles authentication, authorization, and encryption across layers.