Skip to content

01 - Feat/defer exercises - #91

Merged
zhravan merged 4 commits into
zhravan:mainfrom
kawpii:feat/defer-exercises
Oct 1, 2025
Merged

01 - Feat/defer exercises#91
zhravan merged 4 commits into
zhravan:mainfrom
kawpii:feat/defer-exercises

Conversation

@kawpii

@kawpii kawpii commented Oct 1, 2025

Copy link
Copy Markdown
Collaborator

Summary

Describe the change and its motivation.

Checklist

  • Tests pass: make verify or golearn verify <slug> (CLI does not work as said in an issue, but tested locally using go test.)
  • Docs updated (README/CONTRIBUTING) if needed
  • No large new dependencies

Screenshots / Output (if CLI UX)

Paste before/after where helpful.

Related issues

Fixes #75

Summary by CodeRabbit

  • New Features
    • Added a new concept exercise: Deferred Execution (28_defer), focusing on using defer to close files.
  • Refactor
    • Renumbered project slugs: 28–36 → 101–109 (e.g., text_analyzer → 101_text_analyzer, http_server → 104_http_server). Update any saved links or references.
  • Documentation
    • Enhanced hints for the Epoch project (now 109_epoch), including clearer guidance on time/epoch parsing and conversions.

@coderabbitai

coderabbitai Bot commented Oct 1, 2025

Copy link
Copy Markdown

Walkthrough

Introduces a new “Deferred Execution” concept and exercise (28_defer). Updates catalog entries, including adding the new concept and renumbering several project slugs. Adds template code, utilities, and tests for the defer exercise, plus a corresponding solution implementation.

Changes

Cohort / File(s) Summary
Catalog updates
internal/exercises/catalog.yaml
Adds concept 28_defer with hint about using defer for closing files. Renumbers project slugs: 28_…101_… through 36_epoch109_epoch. Expands hints for epoch project.
Defer exercise templates
internal/exercises/templates/28_defer/defer.go, internal/exercises/templates/28_defer/defer_test.go, internal/exercises/templates/28_defer/utils.go
Adds package deferr templates: CreateFile() singleton via os.CreateTemp; WriteToFile(*os.File) writes "ABC" using a created file; test asserts file is closed (second write errors) after WriteToFile.
Defer exercise solution
internal/exercises/solutions/28_defer/defer.go
Adds solution: WriteToFile(*os.File) creates file, defers Close(), writes "data" via fmt.Fprintln.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant T as Test (defer_test.go)
  participant U as CreateFile (utils.go)
  participant W as WriteToFile (defer.go)
  participant F as os.File
  note over T: Setup
  T->>U: CreateFile()
  U-->>T: *os.File (singleton)
  note over T,W: Exercise under test
  T->>W: WriteToFile(f)
  W->>U: CreateFile()
  U-->>W: *os.File
  rect rgba(230,245,255,0.6)
    note right of W: defer f.Close()
  end
  W->>F: Fprintln("data"/"ABC")
  W-->>T: return (deferred Close pending)
  note over F: Close executes after return
  rect rgba(255,235,230,0.6)
    T->>F: Fprintln("again")
    F-->>T: error (file closed)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I tapped the file with gentle care,
defer, my charm, will close it fair.
A whisper: write—then quietly end,
the handle sleeps, no bytes to send.
New numbers march, the catalog neat—
I thump my paws; this change is sweet. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Out of Scope Changes Check ⚠️ Warning The pull request also renames and renumbers numerous unrelated exercise slugs in internal/exercises/catalog.yaml, which is beyond the scope of issue #75 that only required adding the defer exercise, templates, tests, and hints. Extract the slug renaming changes into a separate PR so that this one remains focused solely on adding the defer exercise and its related files.
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues Check ✅ Passed The pull request adds the defer exercise templates and utils under internal/exercises/templates/28_defer, includes a unit test in internal/exercises/templates/28_defer/defer_test.go, updates internal/exercises/catalog.yaml with the 28_defer entry, and provides a hint for deferred file closing, satisfying all acceptance criteria from issue #75.
Title Check ✅ Passed The title highlights the primary change—adding defer exercises—and correctly references the feature under development.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
internal/exercises/templates/28_defer/defer.go (1)

10-10: Consider whether to handle the Fprintln error.

fmt.Fprintln returns an error that is currently ignored. For teaching purposes, you may want to either handle it or add a comment explaining why it's acceptable to ignore in this exercise context.

internal/exercises/templates/28_defer/utils.go (1)

13-23: Singleton temporary file lacks explicit cleanup.

The singleton pattern ensures a single shared file across all calls, which is appropriate for this exercise. However, the temporary file is never explicitly removed and will persist until program termination or OS cleanup.

For exercise purposes this is acceptable, but consider whether to add a cleanup function or document this behavior in a comment.

internal/exercises/solutions/28_defer/defer.go (2)

8-12: Defer usage is correct, but parameter remains unused.

The solution correctly demonstrates defer f.Close() for resource cleanup. However, like the template, the *os.File parameter is never used. Consider aligning the function signature with its actual usage.


10-10: Close() error is ignored.

In production code, you should check the error returned by Close(), especially after writes, as buffered data may fail to flush. For this teaching exercise, the simplified approach may be acceptable.

Example of proper error handling:

defer func() {
    if err := f.Close(); err != nil {
        // Handle close error
    }
}()
internal/exercises/templates/28_defer/defer_test.go (1)

9-10: Clarify the unused parameter pattern.

The test passes f to WriteToFile(f), but WriteToFile ignores this parameter and calls CreateFile() internally. Since both return the same singleton instance, the test works, but the parameter passing is confusing.

Consider either:

  • Removing the parameter from WriteToFile entirely
  • Having WriteToFile use the passed parameter instead of calling CreateFile()
  • Adding comments explaining the pedagogical reason for this design
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b99da22 and 660b7ea.

📒 Files selected for processing (5)
  • internal/exercises/catalog.yaml (1 hunks)
  • internal/exercises/solutions/28_defer/defer.go (1 hunks)
  • internal/exercises/templates/28_defer/defer.go (1 hunks)
  • internal/exercises/templates/28_defer/defer_test.go (1 hunks)
  • internal/exercises/templates/28_defer/utils.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
internal/exercises/solutions/28_defer/defer.go (2)
internal/exercises/templates/28_defer/defer.go (1)
  • WriteToFile (8-11)
internal/exercises/templates/28_defer/utils.go (1)
  • CreateFile (14-23)
internal/exercises/templates/28_defer/defer_test.go (3)
internal/exercises/templates/28_defer/utils.go (1)
  • CreateFile (14-23)
internal/exercises/solutions/28_defer/defer.go (1)
  • WriteToFile (8-12)
internal/exercises/templates/28_defer/defer.go (1)
  • WriteToFile (8-11)
internal/exercises/templates/28_defer/defer.go (2)
internal/exercises/solutions/28_defer/defer.go (1)
  • WriteToFile (8-12)
internal/exercises/templates/28_defer/utils.go (1)
  • CreateFile (14-23)
🔇 Additional comments (6)
internal/exercises/templates/28_defer/utils.go (1)

18-18: Panic is acceptable for exercise code.

Using panic for file creation errors is appropriate for teaching exercises where simplicity is valued. In production code, you'd typically return the error to the caller.

internal/exercises/templates/28_defer/defer_test.go (1)

8-17: Test correctly validates defer/close behavior.

The test verifies that WriteToFile closes the file by attempting a write afterward and expecting an error. This effectively tests whether students have added the defer f.Close() statement.

internal/exercises/catalog.yaml (3)

137-141: New defer concept entry looks good.

The new 28_defer concept entry is well-structured with an appropriate hint pointing students toward using defer with f.Close(). This aligns with the template and test implementations.


185-192: Expanded hints for epoch exercise.

The 109_epoch entry now includes helpful hints about time.Unix(), t.Unix(), and time.Parse. These additions improve the exercise guidance.


144-184: All renamed project slugs verified. No remaining references to the old slugs were found; directory names and catalog.yaml entries correctly use slugs 101–109.

internal/exercises/templates/28_defer/defer.go (1)

8-11: Clarify the unused *os.File parameter and add defer f.Close().

  • The function signature never uses its *os.File argument (it always calls CreateFile()); remove the parameter or document its pedagogical intent.
  • After opening the file, students must defer f.Close()—omitting it currently causes the test to fail.

@kawpii kawpii changed the title Feat/defer exercises 01 - Feat/defer exercises Oct 1, 2025
@zhravan zhravan added the patch Bug fixes and small improvements label Oct 1, 2025
@zhravan
zhravan merged commit f2a6f14 into zhravan:main Oct 1, 2025
zhravan added a commit that referenced this pull request Oct 1, 2025
zhravan added a commit that referenced this pull request Oct 1, 2025
@zhravan

zhravan commented Oct 1, 2025

Copy link
Copy Markdown
Owner

@kaushalyap: Can you re-raise PR for this? Please join community to discuss and take this forward: https://join.ohmyscript.com

Apologies for inconvenience here, needed to set the release cycle properly hence would be great if you could raise this again, I will let you know next steps accordingly

@kawpii

kawpii commented Oct 2, 2025

Copy link
Copy Markdown
Collaborator Author

@zhravan Okay I'll reraise it and join the community

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch Bug fixes and small improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Concept] Defer - add exercise templates

2 participants