Skip to content

Commit 1c52d58

Browse files
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

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
1. What is Declarative Programming?
2+
• Declarative programming is a style of programming where you focus on what the program should achieve,
3+
rather than how to achieve it step by step.
4+
• You describe the logic of computation, not the control flow.
5+
6+
• Contrast:
7+
• Imperative style: “How to do it” (explicit loops, conditions, assignments).
8+
• Declarative style: “What result do you want” (expressions, rules, queries).
9+
10+
11+
12+
2. Key Characteristics
13+
• High-level abstraction: You don’t manage low-level steps.
14+
• Focus on a desired outcome: The program reads like a description of the result.
15+
• Less boilerplate: Fewer lines of code compared to imperative.
16+
• Underlying system decides execution strategy.
17+
18+
19+
20+
3. Examples in Package2.Practice
21+
• SQL:
22+
• Declarative: SELECT * FROM students WHERE marks > 90;
23+
(You specify what you want, not how to fetch row by row.)
24+
• Functional Programming (Java Streams, Lambdas):
25+
• Declarative: list.stream().filter(x -> x > 10).map(x -> x*2).toList();
26+
(You describe transformations, not write for loops.)
27+
• HTML/CSS:
28+
• HTML declares what the page contains, not how to render.
29+
30+
31+
32+
4. Benefits:
33+
• Conciseness – less code, fewer bugs.
34+
• Readability – reads like “business logic” or natural language.
35+
• Maintainability – easier to change rules/logic without touching flow.
36+
• Parallelism – declarative constructs (e.g., Streams) can optimize execution internally.
37+
38+
39+
40+
5. In Java
41+
• Java supports declarative programming mainly through Streams API, Lambdas, and functional style constructs.
42+
• Instead of loops and mutating state, you chain operations (map, filter, reduce).
43+
• Example: Summing even numbers in a list
44+
• Imperative: loop + if + accumulator.
45+
• Declarative: list.stream().filter(x -> x % 2 == 0).mapToInt(Integer::intValue).sum();.
46+
47+
48+
49+
Quick Recap:
50+
✔ Declarative = what to do, Imperative = how to do.
51+
✔ SQL, Streams, HTML are all declarative approaches.
52+
✔ Declarative style improves readability, reduces boilerplate, and makes parallelism easier.

0 commit comments

Comments
 (0)