### Unix I/O Interface Example Source: https://github.com/zakirullin/cognitive-load/blob/main/README.md Illustrates the simple, five-call interface of the Unix I/O system, which hides significant underlying complexity. This serves as an example of a deep module with a shallow interface. ```python open(path, flags, permissions) read(fd, buffer, count) write(fd, buffer, count) lseek(fd, offset, referencePosition) close(fd) ``` -------------------------------- ### Complex Conditional Logic Source: https://github.com/zakirullin/cognitive-load/blob/main/README.agents.md Avoid deeply nested or complex conditional logic that exceeds human working memory capacity. This example illustrates a pattern that is difficult to parse. ```plaintext if val > someConstant // (one fact in human memory) && (condition2 || condition3) // (three facts in human memory), prev cond should be true, one of c2 or c3 has be true && (condition4 && !condition5) { // (human memory overload), we are messed up by this point ... } ``` -------------------------------- ### Simplify Nested If Statements with Early Returns in Go Source: https://github.com/zakirullin/cognitive-load/blob/main/README.md Demonstrates how to replace nested if statements with early return statements to focus on the 'happy path' and reduce cognitive load. This pattern is useful for handling preconditions and simplifying control flow. ```go if isValid { // 🧠+, okay nested code applies to valid input only if isSecure { // 🧠++, we do stuff for valid and secure input only stuff // 🧠+++ } } ``` ```go if !isValid return if !isSecure return // 🧠, we don't really care about earlier returns, if we are here then all good stuff // 🧠+ ``` -------------------------------- ### Simplified Conditional Logic with Intermediate Variables Source: https://github.com/zakirullin/cognitive-load/blob/main/README.agents.md Improve readability by extracting complex conditional expressions into intermediate variables with meaningful names. This approach keeps the human working memory clear. ```plaintext isValid = val > someConstant isAllowed = condition2 || condition3 isSecure = condition4 && !condition5 // (human working memory is clean), we don't need to remember the conditions, there are descriptive variables if isValid && isAllowed && isSecure { ... } ``` -------------------------------- ### Refactor Complex Conditionals in Go Source: https://github.com/zakirullin/cognitive-load/blob/main/README.md Introduces intermediate variables with descriptive names to simplify complex conditional statements, improving readability and reducing cognitive load. This approach is beneficial when dealing with multiple logical conditions that are hard to parse at once. ```go if val > someConstant // 🧠+ && (condition2 || condition3) // 🧠+++, prev cond should be true, one of c2 or c3 has to be true && (condition4 && !condition5) { // 🤯, we are messed up by this point ... } ``` ```go isValid = val > someConstant isAllowed = condition2 || condition3 isSecure = condition4 && !condition5 // 🧠, we don't need to remember the conditions, there are descriptive variables if isValid && isAllowed && isSecure { ... } ``` -------------------------------- ### Self-Descriptive JSON Error Code Source: https://github.com/zakirullin/cognitive-load/blob/main/README.md Use this JSON structure to return self-descriptive error codes in the response body. This avoids the need for frontend and QA engineers to memorize mappings between HTTP status codes and specific business logic errors. ```json { "code": "jwt_has_expired" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.