Commit 1c52d58
committed
docs(paradigms): add detailed explanation of Declarative Programming with Java examples
What
- Documented the concept of **Declarative Programming** vs **Imperative Programming**.
- Highlighted key characteristics:
- Focus on *what* to achieve, not *how* to achieve it.
- High-level abstraction with less boilerplate code.
- Execution strategy left to underlying system (e.g., SQL engine, Streams).
- Provided concrete examples across multiple domains:
- **SQL** → `SELECT * FROM students WHERE marks > 90` (declarative query).
- **Java Streams** → `list.stream().filter(...).map(...).toList()` (functional transformations).
- **HTML/CSS** → structure and styling declaratively described.
- Outlined benefits:
- Conciseness, readability, maintainability.
- Easier parallelization via declarative APIs.
- Added **Java-specific comparison**:
- Imperative sum of even numbers (loops + conditions).
- Declarative sum using Streams API (`filter`, `mapToInt`, `sum`).
Why
- Declarative programming is central to modern Java (Streams, Lambdas, functional style).
- Developers often confuse *control flow* with *business logic*; declarative style separates the two.
- This doc clarifies when and why declarative approaches are preferred in enterprise apps.
Logic
1. **Definition**
- Declarative = specify outcome, not steps.
- Contrasted with imperative = specify exact flow (loops, state changes).
2. **Key Characteristics**
- Abstraction of execution.
- Focus on results instead of algorithmic details.
- Reduction of verbosity and control structures.
3. **Examples**
- SQL → declare query logic without procedural fetch.
- Streams → pipelines describe transformation, not iteration.
- HTML → structure declaration, not rendering process.
4. **Java Focus**
- Streams API embodies declarative patterns (map/filter/reduce).
- Shows improved readability and parallelization compared to traditional loops.
Real-life applications
- **Database queries (SQL)**: write intent-driven queries, let DBMS handle execution plan.
- **Back-end APIs (Java Streams)**: declarative transformations for business rules.
- **UI layer (HTML/CSS)**: declaratively specify layout and style, browsers handle rendering.
- **Parallel data processing**: declarative pipelines allow frameworks to optimize multithreaded execution.
Notes
- Declarative ≠ “no control,” but rather offloading control to libraries/engines.
- Imperative code is sometimes necessary for low-level optimization, but declarative style is preferred for clarity.
- Java’s `Stream.parallel()` demonstrates how declarative style enables automatic performance improvements.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent b5d3a21 commit 1c52d58
File tree
1 file changed
+52
-0
lines changed- Java 8 Crash Course/Java 8 Streams/Declarative Programming/src
1 file changed
+52
-0
lines changedLines changed: 52 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
0 commit comments