Skip to content

Commit d03a7e8

Browse files
committed
feat: add defensive-programming core modules for csharp go and python
1 parent 36a6a5b commit d03a7e8

26 files changed

Lines changed: 486 additions & 18 deletions

File tree

LANGUAGE_PARITY_MATRIX.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This matrix tracks concept parity across C++, C#, Go, and Python.
44

55
- Canonical order is defined by the C++ track.
6-
- Current priority is to complete `02-core` in C#, Go, and Python.
6+
- Current priority is to begin `03-advanced` parity planning in C#, Go, and Python.
77
- Status labels:
88
- `Done`: module implemented with example, exercises, and README.
99
- `Planned`: module not implemented yet, already queued in order.
@@ -25,9 +25,9 @@ This matrix tracks concept parity across C++, C#, Go, and Python.
2525

2626
Current parity progress in non-C++ tracks:
2727

28-
- C#: `5/6` modules complete
29-
- Go: `5/6` modules complete
30-
- Python: `5/6` modules complete
28+
- C#: `6/6` modules complete
29+
- Go: `6/6` modules complete
30+
- Python: `6/6` modules complete
3131

3232
| Order | Module | C++ | C# | Go | Python |
3333
| --- | --- | --- | --- | --- | --- |
@@ -36,8 +36,8 @@ Current parity progress in non-C++ tracks:
3636
| 3 | file-io-basics | Done | Done | Done | Done |
3737
| 4 | sorting-and-searching | Done | Done | Done | Done |
3838
| 5 | maps-and-frequency-counting | Done | Done | Done | Done |
39-
| 6 | error-handling-and-defensive-programming | Done | Planned | Planned | Planned |
39+
| 6 | error-handling-and-defensive-programming | Done | Done | Done | Done |
4040

4141
## Advanced and Expert
4242

43-
`03-advanced` and `04-expert` currently exist only in the C++ track and are out of scope until `02-core` parity is complete in C#, Go, and Python.
43+
`03-advanced` and `04-expert` currently exist only in the C++ track. `02-core` parity is complete, so the next step is defining the first `03-advanced` parity batch.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ This repository teaches programming through small runnable examples and focused
2525
| Language | Current Levels | Module Coverage | Track Status |
2626
| --- | --- | --- | --- |
2727
| C++ | 00-setup, 01-foundations, 02-core, 03-advanced, 04-expert | Foundations, Core, Advanced, Expert, projects, assessments | Most complete and primary track |
28-
| C# | 01-foundations, 02-core (partial) | 8/8 foundations modules, 5/6 core modules | Foundations complete, core expansion in progress |
29-
| Go | 01-foundations, 02-core (partial) | 8/8 foundations modules, 5/6 core modules | Foundations complete, core expansion in progress |
30-
| Python | 01-foundations, 02-core (partial) | 8/8 foundations modules, 5/6 core modules | Foundations complete, core expansion in progress |
28+
| C# | 01-foundations, 02-core | 8/8 foundations modules, 6/6 core modules | Foundations and core complete |
29+
| Go | 01-foundations, 02-core | 8/8 foundations modules, 6/6 core modules | Foundations and core complete |
30+
| Python | 01-foundations, 02-core | 8/8 foundations modules, 6/6 core modules | Foundations and core complete |
3131

3232
Parity planning reference: [LANGUAGE_PARITY_MATRIX.md](LANGUAGE_PARITY_MATRIX.md)
3333

languages/csharp/02-core/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ This level starts the core track with defensive programming patterns.
99
3. [file-io-basics](./file-io-basics/README.md)
1010
4. [sorting-and-searching](./sorting-and-searching/README.md)
1111
5. [maps-and-frequency-counting](./maps-and-frequency-counting/README.md)
12+
6. [error-handling-and-defensive-programming](./error-handling-and-defensive-programming/README.md)
1213

1314
Track progress in [../CHECKLIST.md](../CHECKLIST.md).
1415

1516
## Study Tip
1617

17-
Use `maps-and-frequency-counting` after `sorting-and-searching` to practice aggregation and grouped reporting patterns.
18+
Use `error-handling-and-defensive-programming` last to consolidate validation and guard-style thinking across all core modules.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Error Handling and Defensive Programming (C#)
2+
3+
This module teaches robust validation checks and safe failure behavior.
4+
5+
## Quick Run
6+
7+
~~~bash
8+
dotnet run --project example/error-handling-and-defensive-programming-example.csproj
9+
~~~
10+
11+
## Topics Covered
12+
13+
- Defensive input validation with retry loops.
14+
- Early returns for invalid states.
15+
- Guard conditions before risky operations.
16+
- Producing clear and actionable error messages.
17+
18+
## Common Pitfalls
19+
20+
- Continuing execution after detecting an invalid state.
21+
- Performing division without zero checks.
22+
- Printing vague errors that do not help recovery.
23+
24+
## Exercise Focus
25+
26+
- exercises/01.cs: validate CSV-like row format.
27+
- exercises/02.cs: safe division utility with retries.
28+
29+
### Exercise Specs
30+
31+
1. exercises/01.cs
32+
- Input: one row with format `name,age,city`.
33+
- Output: parsed fields or invalid format message.
34+
- Edge cases: missing commas; empty fields.
35+
36+
2. exercises/02.cs
37+
- Input: pairs of numbers for division until valid.
38+
- Output: quotient or error/retry message.
39+
- Edge cases: divisor zero; non-numeric input.
40+
41+
## Checkpoint
42+
43+
- [ ] I can guard risky operations with clear checks.
44+
- [ ] I can stop invalid program paths early.
45+
- [ ] I can produce useful error feedback for users.
46+
- [ ] I completed exercises/01.cs.
47+
- [ ] I completed exercises/02.cs.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>disable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Example purpose: show the module flow with clear, beginner-friendly steps.
2+
3+
using System;
4+
5+
class Program
6+
{
7+
static bool TrySafeDivide(double left, double right, out double result)
8+
{
9+
// Intent: block invalid operations early before the main logic continues.
10+
if (right == 0.0)
11+
{
12+
result = 0.0;
13+
return false;
14+
}
15+
16+
result = left / right;
17+
return true;
18+
}
19+
20+
static void Main()
21+
{
22+
// Program flow: run a fixed set of cases to show both safe and unsafe paths.
23+
(double left, double right)[] scenarios = new[]
24+
{
25+
(42.0, 6.0),
26+
(10.0, 0.0)
27+
};
28+
29+
foreach ((double left, double right) in scenarios)
30+
{
31+
// Intent: print scenario input so behavior is easy to verify.
32+
Console.WriteLine($"Input: {left} {right}");
33+
34+
if (!TrySafeDivide(left, right, out double quotient))
35+
{
36+
Console.WriteLine("Cannot divide by zero.");
37+
continue;
38+
}
39+
40+
// Intent: print deterministic final output for quick verification.
41+
Console.WriteLine($"Result: {quotient}");
42+
}
43+
}
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
Console.Write("Enter row (name,age,city): ");
8+
string row = Console.ReadLine() ?? string.Empty;
9+
10+
int firstComma = row.IndexOf(',');
11+
if (firstComma < 0)
12+
{
13+
Console.WriteLine("Invalid format. Missing commas.");
14+
return;
15+
}
16+
17+
int secondComma = row.IndexOf(',', firstComma + 1);
18+
if (secondComma < 0)
19+
{
20+
Console.WriteLine("Invalid format. Missing second comma.");
21+
return;
22+
}
23+
24+
string name = row.Substring(0, firstComma);
25+
string age = row.Substring(firstComma + 1, secondComma - firstComma - 1);
26+
string city = row.Substring(secondComma + 1);
27+
28+
if (name.Length == 0 || age.Length == 0 || city.Length == 0)
29+
{
30+
Console.WriteLine("Invalid format. Empty field detected.");
31+
return;
32+
}
33+
34+
Console.WriteLine($"Name: {name}");
35+
Console.WriteLine($"Age: {age}");
36+
Console.WriteLine($"City: {city}");
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
while (true)
8+
{
9+
Console.Write("Enter two numbers to divide (a b): ");
10+
string raw = Console.ReadLine() ?? string.Empty;
11+
string[] parts = raw.Split(' ', StringSplitOptions.RemoveEmptyEntries);
12+
13+
if (parts.Length != 2 ||
14+
!double.TryParse(parts[0], out double a) ||
15+
!double.TryParse(parts[1], out double b))
16+
{
17+
Console.WriteLine("Invalid input type. Try again.");
18+
continue;
19+
}
20+
21+
if (b == 0.0)
22+
{
23+
Console.WriteLine("Divisor cannot be zero. Try again.");
24+
continue;
25+
}
26+
27+
Console.WriteLine($"Result: {a / b}");
28+
break;
29+
}
30+
}
31+
}

languages/csharp/CHECKLIST.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [ ] Complete `02-core/file-io-basics`.
1919
- [ ] Complete `02-core/sorting-and-searching`.
2020
- [ ] Complete `02-core/maps-and-frequency-counting`.
21+
- [ ] Complete `02-core/error-handling-and-defensive-programming`.
2122

2223
## Parity Goals
2324

languages/csharp/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
## Scope and Status
44

5-
This track currently covers `01-foundations` and has started `02-core`.
5+
This track currently covers `01-foundations` and `02-core`.
66

77
- 8/8 foundations modules implemented.
8-
- 5/6 core modules implemented (`input-validation`, `algorithms-basics`, `file-io-basics`, `sorting-and-searching`, `maps-and-frequency-counting`).
8+
- 6/6 core modules implemented (`input-validation`, `algorithms-basics`, `file-io-basics`, `sorting-and-searching`, `maps-and-frequency-counting`, `error-handling-and-defensive-programming`).
99
- Same module naming as C++, Python, and Go for parity.
1010

1111
## Prerequisites
@@ -43,6 +43,7 @@ dotnet run --project 01-foundations/types-and-io/example/types-and-io-example.cs
4343
- [file-io-basics](./02-core/file-io-basics/README.md)
4444
- [sorting-and-searching](./02-core/sorting-and-searching/README.md)
4545
- [maps-and-frequency-counting](./02-core/maps-and-frequency-counting/README.md)
46+
- [error-handling-and-defensive-programming](./02-core/error-handling-and-defensive-programming/README.md)
4647

4748
## Progress Tracking
4849

0 commit comments

Comments
 (0)