Files
AgentCompany/builtin-skills/software-engineer/clean-code/references/func-one-thing.md
T
ZacharyZhang-NY e74dafcd36 Silence render-disposed spam, unbreak bundled skills, drop board wake
Three independently ugly bugs that all fed the same log flood:

* Electron's `WebFrameMain.send` writes every "Render frame was disposed"
  to stderr via `console.error`. When the worker fans a second
  agent-event past a reloading renderer, we produced thousands of
  identical lines and stalled the main loop on stderr I/O. Added a
  `mainFrame.detached` guard so the send is short-circuited before
  Electron's own catch block fires, and installed a targeted filter in
  `suppress-warnings.ts` (now actually imported from the entry point)
  so the specific `console.error("Error sending from webFrameMain:"…)`
  noise never reaches the terminal. Any other payload still surfaces.

* `dispatchAgentMessage` accepted the human-reader sentinel as a wake
  target when an agent replied to `__board__` — `wakeAgentIfPossible`
  then called `db.getAgent("__board__")`, threw, and the dispatcher
  kept repeating on every send. Strip `HUMAN_SENDER_ID` at
  `resolveMessageWakeTargets`, and keep a defense-in-depth guard in the
  heartbeat so a stray caller (future automation fan-out) cannot
  reintroduce the crash.

* Pi's skills loader rejects any SKILL.md whose frontmatter `name`
  diverges from its parent directory. The four bundled `my-writing-style`
  copies declared `name: writing-style-skill`, so every install emitted
  a warning and dropped the skill. They also lived as orphan gitlinks
  (mode 160000 without .gitmodules), and the global `*.md` gitignore
  hid every other SKILL.md from version control. Fixed the frontmatter
  to match the dir, converted the dangling submodules to plain trees,
  and allowlisted `builtin-skills/**/*.md` so a fresh clone carries the
  catalogue instead of silently losing it.
2026-04-20 08:13:48 -04:00

78 lines
2.5 KiB
Markdown

---
title: Functions Should Do One Thing
impact: CRITICAL
impactDescription: enables understanding functions at a glance
tags: func, single-responsibility, cohesion, focus
---
## Functions Should Do One Thing
Functions should do one thing. They should do it well. They should do it only. If a function does multiple things, extract those things into separate functions.
**Incorrect (function does multiple things):**
```java
public void processEmployee(Employee employee) {
// Validate employee
if (employee.getName() == null || employee.getName().isEmpty()) {
throw new IllegalArgumentException("Name required");
}
if (employee.getSalary() < 0) {
throw new IllegalArgumentException("Salary cannot be negative");
}
// Calculate tax
double tax = employee.getSalary() * 0.3;
employee.setTax(tax);
// Save to database
Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO employees VALUES (?, ?, ?)");
stmt.setString(1, employee.getName());
stmt.setDouble(2, employee.getSalary());
stmt.setDouble(3, tax);
stmt.execute();
// Send email notification
EmailService.send(employee.getEmail(), "Welcome!");
}
```
**Correct (each function does one thing):**
```java
public void processEmployee(Employee employee) {
validateEmployee(employee);
calculateTax(employee);
saveEmployee(employee);
sendWelcomeEmail(employee);
}
private void validateEmployee(Employee employee) {
validateName(employee);
validateSalary(employee);
}
private void calculateTax(Employee employee) {
double tax = employee.getSalary() * TAX_RATE;
employee.setTax(tax);
}
private void saveEmployee(Employee employee) {
employeeRepository.save(employee);
}
private void sendWelcomeEmail(Employee employee) {
emailService.sendWelcome(employee.getEmail());
}
```
**Test:** If you can extract another function from it with a name that is not merely a restatement of its implementation, the function is doing more than one thing.
**When NOT to split:**
- If the extracted function's name would just restate its body (e.g., `calculateTax` containing only `salary * TAX_RATE`), the extraction adds indirection without clarity.
- Orchestration functions that call a sequence of steps at the same abstraction level (like `processEmployee` above) are doing "one thing" — orchestrating. Don't try to extract the orchestration further.
Reference: [Clean Code, Chapter 3: Functions](https://www.oreilly.com/library/view/clean-code-a/9780136083238/)