{"id":18400894,"url":"https://github.com/magicmark/programming-protips","last_synced_at":"2026-03-19T04:11:49.010Z","repository":{"id":69482273,"uuid":"318313043","full_name":"magicmark/programming-protips","owner":"magicmark","description":"programming protips for programmers","archived":false,"fork":false,"pushed_at":"2021-01-20T19:20:19.000Z","size":20,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-15T04:29:00.329Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magicmark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-03T20:35:11.000Z","updated_at":"2023-03-04T06:02:50.000Z","dependencies_parsed_at":"2023-03-15T22:00:19.989Z","dependency_job_id":null,"html_url":"https://github.com/magicmark/programming-protips","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/magicmark/programming-protips","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprogramming-protips","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprogramming-protips/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprogramming-protips/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprogramming-protips/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magicmark","download_url":"https://codeload.github.com/magicmark/programming-protips/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprogramming-protips/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28680623,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T04:33:33.518Z","status":"ssl_error","status_checked_at":"2026-01-23T04:33:30.433Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T02:37:11.677Z","updated_at":"2026-01-23T04:56:55.278Z","avatar_url":"https://github.com/magicmark.png","language":null,"readme":"# 💡 Programming Protips\n\n_Got something to add? [Send a PR](https://github.com/magicmark/engineering-protips/pulls)!_\n\n### Limit the amount of logic on a single line of code\n\nTODO: Add rationale\n\n### Limit the amount of logic in a try/catch block\n\nKeep the contents of try/catch blocks to a minimum.\n\n**Bad Example**\n\n```js\ntry {\n  const config = yaml.safeLoad(fs.readFileSync(path.join(process.cwd(), configFile), 'utf8'));\n  const { projectName, scanPaths } = config;\n  const resolvedScanPaths = [];\n  scanPaths.forEach(scanPath =\u003e {\n    resolvedScanPaths.push(path.join(__dirname, scanPath));\n  });\n  ...\n  ...\n  27 lines later\n  ...\n} catch (e) {\n  console.error('Could not read config file, assuming defaults.');\n}\n```\n\n**Prefer**\n\n```js\nlet config;\n\ntry {\n  config = fs.readFileSync(path.join(process.cwd(), configFile), 'utf8'));\n} catch (e) {\n  console.error('Could not read config file, assuming defaults.');\n}\n```\n\n#### Why?\n\nThere's _lots_ of things could throw inside our original try block. We may accidentally silence and ignore unrelated errors.\n\n(e.g. Maybe the config file _was_ found, but `scanPaths` wasn't specified - so `scanPaths.forEach` throws, but we silenced it! Uh oh!)\n\n### Limit what you catch\n\nBe specific when catching an error. Rethrow all other errors.\n\n**Bad Example**\n\n```js\ntry {\n  result = divideNumbers(3, 0);\n} catch (e) {\n  console.error(\"You can't divide by zero!\");\n}\n```\n\n**Prefer**\n\n```js\ntry {\n  result = divideNumbers(3, 0);\n} catch (e) {\n  if (e instanceof DivideByZeroError) {\n    console.error(\"You can't divide by zero!\");\n  } else {\n    throw e;\n  }\n}\n```\n\n#### Why?\n\nLots of things could throw!\n\nThe message displayed to users or recovery logic inside the catch block may only apply to a certain type of error. But the catch block may be triggered with more errors types than you expect! (e.g. Someone accidentally renames the divideNumbers function, and now we're also catching a `ReferenceError`!)\n\nAvoid \"catch all\" blocks that gobble up errors we didn't intend to catch.\n\nMore reading: https://gist.github.com/jehugaleahsa/f3c43d41e68a6b4bc73d2d6cbaee876a#within-a-limited-scope\n\n### Use user-defined error codes\n### DRY\n\nTODO: Add rationale\n\n### DRY\n\nTODO: Add rationale\n\n### Prefer writing pure functions where applicable\n\nsomething something avoid side effects / global state\n\nTODO: Add rationale\n\n### Preserve prior stack traces when throwing errors\n\nWhen throwing a new error from inside a try/catch block, preserve the stack from the caught error.\n\n**Bad Example**\n\n```js\ntry {\n  config = readFileSync(configFilePath, 'utf8');\n} catch (e) {\n  throw new Error(\"Could not read config file - ensure this file exists!\");\n}\n```\n\n**Prefer**\n\n```js\ntry {\n  config = readFileSync(configFilePath, 'utf8');\n} catch (e) {\n  // Print both stack traces\n  throw new MultiError([\"Could not read config file - ensure this file exists!\", e]);\n}\n```\n\n#### Why?\n\nWe don't want to gobble up the stack trace from the caught error as it contains potentially useful information.\n\nIn our example, the original stack trace will show the location on disk that we tried to read the config file from. Throwing this information away makes debugging harder.\n\nJavaScript libraries to help:\n- https://github.com/joyent/node-verror\n- https://www.npmjs.com/package/aggregate-error\n\n### Don't blindly copy/paste code\n\nTODO: Add rationale\n\n### Make custom error messages as useful as possible\n\nTODO: Add rationale\n\n**Further Reading / Prior Art**\n\n- https://twitter.com/swyx/status/1329171738215686145\n\n### Return early\n\nTODO: Add rationale\n\n### Don't try and outsmart the typechecker\n\nTODO: Add rationale\n\n### Read your stack trace. Scroll up.\n\n### Don't pass functions too many arguments\n\n- **Further Reading / Prior Art**\n\n- http://wiki.c2.com/?TooManyParameters\n\n### Don't pass functions the whole object\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicmark%2Fprogramming-protips","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagicmark%2Fprogramming-protips","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicmark%2Fprogramming-protips/lists"}