💻 Computer Science · Programming Concepts

Programming tricks that make OOP click

OOP, recursion, design patterns, and paradigms — clarified.

⌨️ Programming

Memory tricks

Proven mnemonics — fast to learn, hard to forget.

Recursion
Recursion: base case stops it, recursive case reduces toward base case
Recursion
A function that solves a problem by calling itself
Every recursive function needs: (1) base case that stops recursion, (2) recursive case moving toward base case. Factorial: n! = n × (n-1)!, base case: 0! = 1. Without base case → stack overflow.
DRY Principle
DRY: Don't Repeat Yourself — extract repeated logic into functions
DRY Principle
The most important coding principle — eliminate duplication
If you write the same logic twice, extract it. Changes only need to be made once. Related: SOLID principles. Opposite of WET (Write Everything Twice) code.
Complexity Trade-offs
Time vs Space complexity: often trade one for the other
Complexity Trade-offs
Algorithms often trade time for memory or vice versa
Caching (memoization): use more memory to save computation time. Streaming: process in chunks, save memory at cost of speed. Profile before optimizing — premature optimization wastes time.
SOLID Principles
SOLID: Single responsibility, Open/closed, Liskov, Interface segregation, Dependency inversion
SOLID Principles
Five object-oriented design principles for maintainable code
S: one class, one job. O: open for extension, closed for modification. L: subclasses should be substitutable for parent classes. I: don't force classes to implement interfaces they don't use. D: depend on abstractions, not concretions.
Functional Programming
Functional programming: pure functions (no side effects) + immutable data. Map, filter, reduce.
Functional Programming
A paradigm treating computation as mathematical function evaluation
Pure functions: same input always produces same output, no side effects. Immutable data: never modify, create new. First-class functions: functions as values — pass them, return them. Map: transform each element. Filter: keep elements matching condition. Reduce: combine elements into single value. Haskell, Clojure, Scala.
Common Design Patterns
Design patterns: Singleton (one instance), Factory (create objects), Observer (notify subscribers), Strategy (swap algorithms)
Common Design Patterns
Reusable solutions to common software design problems
Creational: Singleton (one instance, e.g., database connection), Factory (create objects without specifying class), Builder (construct complex objects step by step). Structural: Adapter (interface compatibility), Decorator (add behavior). Behavioral: Observer (publish-subscribe), Strategy (interchangeable algorithms), Iterator.
Singleton
One instance — database connections
Factory
Create objects without specifying class
Observer
Publish-subscribe — event listeners
Strategy
Swap algorithms at runtime
Version Control with Git
Version control: Git. Commit = snapshot. Branch = parallel development. Merge = combine branches.
Version Control with Git
How Git tracks changes and enables collaboration
Commit: snapshot of all tracked files at a point in time. Branch: independent line of development (feature/main). Merge: combine changes from two branches. Pull request: request to merge branch, enables code review. Common workflow: create branch → make changes → commit → push → pull request → merge.
Software Testing
Testing types: unit (single function), integration (modules together), end-to-end (full system), regression (old bugs)
Software Testing
Four levels of testing — each catching different types of bugs
Unit tests: test individual functions/methods in isolation — fast, pinpoint failures. Integration tests: test how modules work together — catches interface bugs. End-to-end (E2E): test complete user workflows — slow but high confidence. Regression: re-run tests after changes to ensure old bugs don't return.
Unit
Single function, fast, isolated
Integration
Multiple modules working together
E2E
Full user workflow
Regression
Prevent old bugs from returning
Space Complexity
Big O space complexity: track memory used. Recursive algorithms use O(n) stack space for n calls.
Space Complexity
Measuring memory usage — often overlooked but critical
Algorithms have both time AND space complexity. Iterative O(n) sum: O(1) space. Recursive O(n) sum: O(n) space (n stack frames). Merge sort: O(n) extra space for merging. In-place algorithms: O(1) extra space (bubble sort, selection sort). Space-time tradeoff: often use more memory to go faster.
APIs and REST
API: Application Programming Interface. REST: stateless, HTTP verbs, JSON. Endpoint = specific URL.
APIs and REST
How software systems communicate with each other
API: defined interface for software interaction. REST (Representational State Transfer): architectural style for web APIs. Stateless: each request contains all info needed. Resources identified by URLs. HTTP verbs: GET/POST/PUT/DELETE. Response typically JSON. Status codes: 200 OK, 404 Not Found, 500 Server Error.
Concurrency and Parallelism
Concurrency vs parallelism: concurrency = dealing with multiple things. Parallelism = doing multiple things simultaneously.
Concurrency and Parallelism
Two related but distinct concepts in multi-threaded programming
Concurrency: structuring program to handle multiple tasks — can be done on a single core by interleaving. Parallelism: actually executing multiple tasks simultaneously — requires multiple cores. Race condition: two threads access shared data simultaneously → unpredictable results. Mutex/lock: ensures only one thread accesses resource at a time.