Commit 132e986
committed
feat: demonstrate enum usage with ordinal(), equality checks, and switch-case
WHAT was added:
- Defined an `enum Status { Running, failed, Pending, Success }` to represent task states.
- Showcased:
- Printing enum constants directly.
- Using `ordinal()` to get the constant’s position.
- Comparing enum values with `==` for conditional logic.
- Using `switch` on enum constants.
WHY this matters:
- Enums provide type-safe constants, making code more readable and less error-prone than using raw integers or strings.
- `ordinal()` shows the position of the enum in its declaration (e.g., `Running → 0`, `Success → 3`).
- Equality (`==`) and `switch-case` improve clarity when checking state transitions.
HOW it works:
1. Print constants:
- `Status.Running → Running`
- `Status.Success → Success`
2. Print ordinals:
- `Status.Running.ordinal() → 0`
- `Status.Success.ordinal() → 3`
3. Conditional check with `if-else` to print messages depending on status.
4. `switch(s)` statement executes matching case block (e.g., `Running → "All Good"`).
REAL-LIFE APPLICATIONS:
- Workflow states → `PENDING`, `RUNNING`, `FAILED`, `SUCCESS`.
- Order status → `PLACED`, `SHIPPED`, `DELIVERED`, `CANCELLED`.
- Payment lifecycle → `INITIATED`, `PROCESSING`, `SUCCESS`, `FAILED`.
- Job scheduling → `WAITING`, `RUNNING`, `COMPLETED`.
NOTES / BEST PRACTICES:
- Avoid relying on `ordinal()` for persistence since reordering constants changes values.
- Prefer `name()` or custom fields for saving state in databases.
- Enum in `switch` improves readability compared to multiple `if-else` chains.
KEYWORDS:
enum, ordinal, switch-case, workflow states, type-safe constants, real-world enum usage.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9866223 commit 132e986
1 file changed
+22
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
30 | | - | |
| 29 | + | |
31 | 30 | | |
32 | 31 | | |
33 | 32 | | |
| |||
45 | 44 | | |
46 | 45 | | |
47 | 46 | | |
48 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
0 commit comments