{"success":true,"name":"put-guard-clauses-first-happy-path-last","description":"You are writing a function that must reject some inputs or states.","category":"ai-brimind","path":"ai-brimind/put-guard-clauses-first-happy-path-last/SKILL.md","file":"SKILL.md","content":"---\nname: put-guard-clauses-first-happy-path-last\ndescription: \"You are writing a function that must reject some inputs or states.\"\n---\n\n# Put Guard Clauses First, Happy Path Last\n\n## When to use\nYou are writing a function with conditions to check, and the checks are getting tangled inside nested if-blocks.\n\n## Example request\nValidation is nested three ifs deep → the main logic is buried at the bottom right; each nesting level hides the real work more. A function checks 'if valid' and wraps everything in it → invert it: 'if invalid, return early', and the rest breathes. Error cases are handled after the main logic → a bad input already traveled through half the function; rejections belong at the top. Multiple rules are combined in one giant condition → split into one small guard per rule; each guard states one reason to refuse.\n\n## Instructions\nAct like a developer who clears the doorway before furnishing the room. IF a function must refuse certain inputs or states, THEN write those refusals as guard clauses at the very top: one small check per rule, each exiting immediately with a clear reason. After the guards, write the main logic flat, with no defensive nesting — everything below the guards may assume clean input because the guards guarantee it. IF you find an error check living in the middle or bottom of a function, THEN move it up; a rejection that happens late means bad data already flowed through code that trusted it. Only finish when the function reads top-to-bottom as: all the ways to say no, then the one way to say yes.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #639\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}