@booklib/core 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (374) hide show
  1. package/.cursor/rules/booklib-standards.mdc +40 -0
  2. package/.gemini/context.md +372 -0
  3. package/AGENTS.md +166 -0
  4. package/CHANGELOG.md +226 -0
  5. package/CLAUDE.md +81 -0
  6. package/CODE_OF_CONDUCT.md +31 -0
  7. package/CONTRIBUTING.md +304 -0
  8. package/LICENSE +21 -0
  9. package/PLAN.md +28 -0
  10. package/README.ja.md +198 -0
  11. package/README.ko.md +198 -0
  12. package/README.md +503 -0
  13. package/README.pt-BR.md +198 -0
  14. package/README.uk.md +241 -0
  15. package/README.zh-CN.md +198 -0
  16. package/SECURITY.md +9 -0
  17. package/agents/architecture-reviewer.md +136 -0
  18. package/agents/booklib-reviewer.md +90 -0
  19. package/agents/data-reviewer.md +107 -0
  20. package/agents/jvm-reviewer.md +146 -0
  21. package/agents/python-reviewer.md +128 -0
  22. package/agents/rust-reviewer.md +115 -0
  23. package/agents/ts-reviewer.md +110 -0
  24. package/agents/ui-reviewer.md +117 -0
  25. package/assets/logo.svg +36 -0
  26. package/bin/booklib-mcp.js +304 -0
  27. package/bin/booklib.js +1705 -0
  28. package/bin/skills.cjs +1292 -0
  29. package/booklib-router.mdc +36 -0
  30. package/booklib.config.json +19 -0
  31. package/commands/animation-at-work.md +10 -0
  32. package/commands/clean-code-reviewer.md +10 -0
  33. package/commands/data-intensive-patterns.md +10 -0
  34. package/commands/data-pipelines.md +10 -0
  35. package/commands/design-patterns.md +10 -0
  36. package/commands/domain-driven-design.md +10 -0
  37. package/commands/effective-java.md +10 -0
  38. package/commands/effective-kotlin.md +10 -0
  39. package/commands/effective-python.md +10 -0
  40. package/commands/effective-typescript.md +10 -0
  41. package/commands/kotlin-in-action.md +10 -0
  42. package/commands/lean-startup.md +10 -0
  43. package/commands/microservices-patterns.md +10 -0
  44. package/commands/programming-with-rust.md +10 -0
  45. package/commands/refactoring-ui.md +10 -0
  46. package/commands/rust-in-action.md +10 -0
  47. package/commands/skill-router.md +10 -0
  48. package/commands/spring-boot-in-action.md +10 -0
  49. package/commands/storytelling-with-data.md +10 -0
  50. package/commands/system-design-interview.md +10 -0
  51. package/commands/using-asyncio-python.md +10 -0
  52. package/commands/web-scraping-python.md +10 -0
  53. package/community/registry.json +1616 -0
  54. package/hooks/hooks.json +23 -0
  55. package/hooks/posttooluse-capture.mjs +67 -0
  56. package/hooks/suggest.js +153 -0
  57. package/lib/agent-behaviors.js +40 -0
  58. package/lib/agent-detector.js +96 -0
  59. package/lib/config-loader.js +39 -0
  60. package/lib/conflict-resolver.js +148 -0
  61. package/lib/context-builder.js +574 -0
  62. package/lib/discovery-engine.js +298 -0
  63. package/lib/doctor/hook-installer.js +83 -0
  64. package/lib/doctor/usage-tracker.js +87 -0
  65. package/lib/engine/ai-features.js +253 -0
  66. package/lib/engine/auditor.js +103 -0
  67. package/lib/engine/bm25-index.js +178 -0
  68. package/lib/engine/capture.js +120 -0
  69. package/lib/engine/corrections.js +198 -0
  70. package/lib/engine/doctor.js +195 -0
  71. package/lib/engine/graph-injector.js +137 -0
  72. package/lib/engine/graph.js +161 -0
  73. package/lib/engine/handoff.js +405 -0
  74. package/lib/engine/indexer.js +242 -0
  75. package/lib/engine/parser.js +53 -0
  76. package/lib/engine/query-expander.js +42 -0
  77. package/lib/engine/reranker.js +40 -0
  78. package/lib/engine/rrf.js +59 -0
  79. package/lib/engine/scanner.js +151 -0
  80. package/lib/engine/searcher.js +139 -0
  81. package/lib/engine/session-coordinator.js +306 -0
  82. package/lib/engine/session-manager.js +429 -0
  83. package/lib/engine/synthesizer.js +70 -0
  84. package/lib/installer.js +70 -0
  85. package/lib/instinct-block.js +33 -0
  86. package/lib/mcp-config-writer.js +88 -0
  87. package/lib/paths.js +57 -0
  88. package/lib/profiles/design.md +19 -0
  89. package/lib/profiles/general.md +16 -0
  90. package/lib/profiles/research-analysis.md +22 -0
  91. package/lib/profiles/software-development.md +23 -0
  92. package/lib/profiles/writing-content.md +19 -0
  93. package/lib/project-initializer.js +916 -0
  94. package/lib/registry/skills.js +102 -0
  95. package/lib/registry-searcher.js +99 -0
  96. package/lib/rules/rules-manager.js +169 -0
  97. package/lib/skill-fetcher.js +333 -0
  98. package/lib/well-known-builder.js +70 -0
  99. package/lib/wizard/index.js +404 -0
  100. package/lib/wizard/integration-detector.js +41 -0
  101. package/lib/wizard/project-detector.js +100 -0
  102. package/lib/wizard/prompt.js +156 -0
  103. package/lib/wizard/registry-embeddings.js +107 -0
  104. package/lib/wizard/skill-recommender.js +69 -0
  105. package/llms-full.txt +254 -0
  106. package/llms.txt +70 -0
  107. package/package.json +45 -0
  108. package/research-reports/2026-04-01-current-architecture.md +160 -0
  109. package/research-reports/IDEAS.md +93 -0
  110. package/rules/common/clean-code.md +42 -0
  111. package/rules/java/effective-java.md +42 -0
  112. package/rules/kotlin/effective-kotlin.md +37 -0
  113. package/rules/python/effective-python.md +38 -0
  114. package/rules/rust/rust.md +37 -0
  115. package/rules/typescript/effective-typescript.md +42 -0
  116. package/scripts/gen-llms-full.mjs +36 -0
  117. package/scripts/gen-og.mjs +142 -0
  118. package/scripts/validate-frontmatter.js +25 -0
  119. package/skills/animation-at-work/SKILL.md +270 -0
  120. package/skills/animation-at-work/assets/example_asset.txt +1 -0
  121. package/skills/animation-at-work/evals/evals.json +44 -0
  122. package/skills/animation-at-work/evals/results.json +13 -0
  123. package/skills/animation-at-work/examples/after.md +64 -0
  124. package/skills/animation-at-work/examples/before.md +35 -0
  125. package/skills/animation-at-work/references/api_reference.md +369 -0
  126. package/skills/animation-at-work/references/review-checklist.md +79 -0
  127. package/skills/animation-at-work/scripts/audit_animations.py +295 -0
  128. package/skills/animation-at-work/scripts/example.py +1 -0
  129. package/skills/clean-code-reviewer/SKILL.md +444 -0
  130. package/skills/clean-code-reviewer/audit.json +35 -0
  131. package/skills/clean-code-reviewer/evals/evals.json +185 -0
  132. package/skills/clean-code-reviewer/evals/results.json +13 -0
  133. package/skills/clean-code-reviewer/examples/after.md +48 -0
  134. package/skills/clean-code-reviewer/examples/before.md +33 -0
  135. package/skills/clean-code-reviewer/references/api_reference.md +158 -0
  136. package/skills/clean-code-reviewer/references/practices-catalog.md +282 -0
  137. package/skills/clean-code-reviewer/references/review-checklist.md +254 -0
  138. package/skills/clean-code-reviewer/scripts/pre-review.py +206 -0
  139. package/skills/data-intensive-patterns/SKILL.md +267 -0
  140. package/skills/data-intensive-patterns/assets/example_asset.txt +1 -0
  141. package/skills/data-intensive-patterns/evals/evals.json +54 -0
  142. package/skills/data-intensive-patterns/evals/results.json +13 -0
  143. package/skills/data-intensive-patterns/examples/after.md +61 -0
  144. package/skills/data-intensive-patterns/examples/before.md +38 -0
  145. package/skills/data-intensive-patterns/references/api_reference.md +34 -0
  146. package/skills/data-intensive-patterns/references/patterns-catalog.md +551 -0
  147. package/skills/data-intensive-patterns/references/review-checklist.md +193 -0
  148. package/skills/data-intensive-patterns/scripts/adr.py +213 -0
  149. package/skills/data-intensive-patterns/scripts/example.py +1 -0
  150. package/skills/data-pipelines/SKILL.md +259 -0
  151. package/skills/data-pipelines/assets/example_asset.txt +1 -0
  152. package/skills/data-pipelines/evals/evals.json +45 -0
  153. package/skills/data-pipelines/evals/results.json +13 -0
  154. package/skills/data-pipelines/examples/after.md +97 -0
  155. package/skills/data-pipelines/examples/before.md +37 -0
  156. package/skills/data-pipelines/references/api_reference.md +301 -0
  157. package/skills/data-pipelines/references/review-checklist.md +181 -0
  158. package/skills/data-pipelines/scripts/example.py +1 -0
  159. package/skills/data-pipelines/scripts/new_pipeline.py +444 -0
  160. package/skills/design-patterns/SKILL.md +271 -0
  161. package/skills/design-patterns/assets/example_asset.txt +1 -0
  162. package/skills/design-patterns/evals/evals.json +46 -0
  163. package/skills/design-patterns/evals/results.json +13 -0
  164. package/skills/design-patterns/examples/after.md +52 -0
  165. package/skills/design-patterns/examples/before.md +29 -0
  166. package/skills/design-patterns/references/api_reference.md +1 -0
  167. package/skills/design-patterns/references/patterns-catalog.md +726 -0
  168. package/skills/design-patterns/references/review-checklist.md +173 -0
  169. package/skills/design-patterns/scripts/example.py +1 -0
  170. package/skills/design-patterns/scripts/scaffold.py +807 -0
  171. package/skills/domain-driven-design/SKILL.md +142 -0
  172. package/skills/domain-driven-design/assets/example_asset.txt +1 -0
  173. package/skills/domain-driven-design/evals/evals.json +48 -0
  174. package/skills/domain-driven-design/evals/results.json +13 -0
  175. package/skills/domain-driven-design/examples/after.md +80 -0
  176. package/skills/domain-driven-design/examples/before.md +43 -0
  177. package/skills/domain-driven-design/references/api_reference.md +1 -0
  178. package/skills/domain-driven-design/references/patterns-catalog.md +545 -0
  179. package/skills/domain-driven-design/references/review-checklist.md +158 -0
  180. package/skills/domain-driven-design/scripts/example.py +1 -0
  181. package/skills/domain-driven-design/scripts/scaffold.py +421 -0
  182. package/skills/effective-java/SKILL.md +227 -0
  183. package/skills/effective-java/assets/example_asset.txt +1 -0
  184. package/skills/effective-java/evals/evals.json +46 -0
  185. package/skills/effective-java/evals/results.json +13 -0
  186. package/skills/effective-java/examples/after.md +83 -0
  187. package/skills/effective-java/examples/before.md +37 -0
  188. package/skills/effective-java/references/api_reference.md +1 -0
  189. package/skills/effective-java/references/items-catalog.md +955 -0
  190. package/skills/effective-java/references/review-checklist.md +216 -0
  191. package/skills/effective-java/scripts/checkstyle_setup.py +211 -0
  192. package/skills/effective-java/scripts/example.py +1 -0
  193. package/skills/effective-kotlin/SKILL.md +271 -0
  194. package/skills/effective-kotlin/assets/example_asset.txt +1 -0
  195. package/skills/effective-kotlin/audit.json +29 -0
  196. package/skills/effective-kotlin/evals/evals.json +45 -0
  197. package/skills/effective-kotlin/evals/results.json +13 -0
  198. package/skills/effective-kotlin/examples/after.md +36 -0
  199. package/skills/effective-kotlin/examples/before.md +38 -0
  200. package/skills/effective-kotlin/references/api_reference.md +1 -0
  201. package/skills/effective-kotlin/references/practices-catalog.md +1228 -0
  202. package/skills/effective-kotlin/references/review-checklist.md +126 -0
  203. package/skills/effective-kotlin/scripts/example.py +1 -0
  204. package/skills/effective-python/SKILL.md +441 -0
  205. package/skills/effective-python/evals/evals.json +44 -0
  206. package/skills/effective-python/evals/results.json +13 -0
  207. package/skills/effective-python/examples/after.md +56 -0
  208. package/skills/effective-python/examples/before.md +40 -0
  209. package/skills/effective-python/ref-01-pythonic-thinking.md +202 -0
  210. package/skills/effective-python/ref-02-lists-and-dicts.md +146 -0
  211. package/skills/effective-python/ref-03-functions.md +186 -0
  212. package/skills/effective-python/ref-04-comprehensions-generators.md +211 -0
  213. package/skills/effective-python/ref-05-classes-interfaces.md +188 -0
  214. package/skills/effective-python/ref-06-metaclasses-attributes.md +209 -0
  215. package/skills/effective-python/ref-07-concurrency.md +213 -0
  216. package/skills/effective-python/ref-08-robustness-performance.md +248 -0
  217. package/skills/effective-python/ref-09-testing-debugging.md +253 -0
  218. package/skills/effective-python/ref-10-collaboration.md +175 -0
  219. package/skills/effective-python/references/api_reference.md +218 -0
  220. package/skills/effective-python/references/practices-catalog.md +483 -0
  221. package/skills/effective-python/references/review-checklist.md +190 -0
  222. package/skills/effective-python/scripts/lint.py +173 -0
  223. package/skills/effective-typescript/SKILL.md +262 -0
  224. package/skills/effective-typescript/audit.json +29 -0
  225. package/skills/effective-typescript/evals/evals.json +37 -0
  226. package/skills/effective-typescript/evals/results.json +13 -0
  227. package/skills/effective-typescript/examples/after.md +70 -0
  228. package/skills/effective-typescript/examples/before.md +47 -0
  229. package/skills/effective-typescript/references/api_reference.md +118 -0
  230. package/skills/effective-typescript/references/practices-catalog.md +371 -0
  231. package/skills/effective-typescript/scripts/review.py +169 -0
  232. package/skills/kotlin-in-action/SKILL.md +261 -0
  233. package/skills/kotlin-in-action/assets/example_asset.txt +1 -0
  234. package/skills/kotlin-in-action/evals/evals.json +43 -0
  235. package/skills/kotlin-in-action/evals/results.json +13 -0
  236. package/skills/kotlin-in-action/examples/after.md +53 -0
  237. package/skills/kotlin-in-action/examples/before.md +39 -0
  238. package/skills/kotlin-in-action/references/api_reference.md +1 -0
  239. package/skills/kotlin-in-action/references/practices-catalog.md +436 -0
  240. package/skills/kotlin-in-action/references/review-checklist.md +204 -0
  241. package/skills/kotlin-in-action/scripts/example.py +1 -0
  242. package/skills/kotlin-in-action/scripts/setup_detekt.py +224 -0
  243. package/skills/lean-startup/SKILL.md +160 -0
  244. package/skills/lean-startup/assets/example_asset.txt +1 -0
  245. package/skills/lean-startup/evals/evals.json +43 -0
  246. package/skills/lean-startup/evals/results.json +13 -0
  247. package/skills/lean-startup/examples/after.md +80 -0
  248. package/skills/lean-startup/examples/before.md +34 -0
  249. package/skills/lean-startup/references/api_reference.md +319 -0
  250. package/skills/lean-startup/references/review-checklist.md +137 -0
  251. package/skills/lean-startup/scripts/example.py +1 -0
  252. package/skills/lean-startup/scripts/new_experiment.py +286 -0
  253. package/skills/microservices-patterns/SKILL.md +384 -0
  254. package/skills/microservices-patterns/evals/evals.json +45 -0
  255. package/skills/microservices-patterns/evals/results.json +13 -0
  256. package/skills/microservices-patterns/examples/after.md +69 -0
  257. package/skills/microservices-patterns/examples/before.md +40 -0
  258. package/skills/microservices-patterns/references/patterns-catalog.md +391 -0
  259. package/skills/microservices-patterns/references/review-checklist.md +169 -0
  260. package/skills/microservices-patterns/scripts/new_service.py +583 -0
  261. package/skills/programming-with-rust/SKILL.md +209 -0
  262. package/skills/programming-with-rust/evals/evals.json +37 -0
  263. package/skills/programming-with-rust/evals/results.json +13 -0
  264. package/skills/programming-with-rust/examples/after.md +107 -0
  265. package/skills/programming-with-rust/examples/before.md +59 -0
  266. package/skills/programming-with-rust/references/api_reference.md +152 -0
  267. package/skills/programming-with-rust/references/practices-catalog.md +335 -0
  268. package/skills/programming-with-rust/scripts/review.py +142 -0
  269. package/skills/refactoring-ui/SKILL.md +362 -0
  270. package/skills/refactoring-ui/assets/example_asset.txt +1 -0
  271. package/skills/refactoring-ui/evals/evals.json +45 -0
  272. package/skills/refactoring-ui/evals/results.json +13 -0
  273. package/skills/refactoring-ui/examples/after.md +85 -0
  274. package/skills/refactoring-ui/examples/before.md +58 -0
  275. package/skills/refactoring-ui/references/api_reference.md +355 -0
  276. package/skills/refactoring-ui/references/review-checklist.md +114 -0
  277. package/skills/refactoring-ui/scripts/audit_css.py +250 -0
  278. package/skills/refactoring-ui/scripts/example.py +1 -0
  279. package/skills/rust-in-action/SKILL.md +350 -0
  280. package/skills/rust-in-action/evals/evals.json +38 -0
  281. package/skills/rust-in-action/evals/results.json +13 -0
  282. package/skills/rust-in-action/examples/after.md +156 -0
  283. package/skills/rust-in-action/examples/before.md +56 -0
  284. package/skills/rust-in-action/references/practices-catalog.md +346 -0
  285. package/skills/rust-in-action/scripts/review.py +147 -0
  286. package/skills/skill-router/SKILL.md +186 -0
  287. package/skills/skill-router/evals/evals.json +38 -0
  288. package/skills/skill-router/evals/results.json +13 -0
  289. package/skills/skill-router/examples/after.md +63 -0
  290. package/skills/skill-router/examples/before.md +39 -0
  291. package/skills/skill-router/references/api_reference.md +24 -0
  292. package/skills/skill-router/references/routing-heuristics.md +89 -0
  293. package/skills/skill-router/references/skill-catalog.md +174 -0
  294. package/skills/skill-router/scripts/route.py +266 -0
  295. package/skills/spring-boot-in-action/SKILL.md +340 -0
  296. package/skills/spring-boot-in-action/evals/evals.json +39 -0
  297. package/skills/spring-boot-in-action/evals/results.json +13 -0
  298. package/skills/spring-boot-in-action/examples/after.md +185 -0
  299. package/skills/spring-boot-in-action/examples/before.md +84 -0
  300. package/skills/spring-boot-in-action/references/practices-catalog.md +403 -0
  301. package/skills/spring-boot-in-action/scripts/review.py +184 -0
  302. package/skills/storytelling-with-data/SKILL.md +241 -0
  303. package/skills/storytelling-with-data/assets/example_asset.txt +1 -0
  304. package/skills/storytelling-with-data/evals/evals.json +47 -0
  305. package/skills/storytelling-with-data/evals/results.json +13 -0
  306. package/skills/storytelling-with-data/examples/after.md +50 -0
  307. package/skills/storytelling-with-data/examples/before.md +33 -0
  308. package/skills/storytelling-with-data/references/api_reference.md +379 -0
  309. package/skills/storytelling-with-data/references/review-checklist.md +111 -0
  310. package/skills/storytelling-with-data/scripts/chart_review.py +301 -0
  311. package/skills/storytelling-with-data/scripts/example.py +1 -0
  312. package/skills/system-design-interview/SKILL.md +233 -0
  313. package/skills/system-design-interview/assets/example_asset.txt +1 -0
  314. package/skills/system-design-interview/evals/evals.json +46 -0
  315. package/skills/system-design-interview/evals/results.json +13 -0
  316. package/skills/system-design-interview/examples/after.md +94 -0
  317. package/skills/system-design-interview/examples/before.md +27 -0
  318. package/skills/system-design-interview/references/api_reference.md +582 -0
  319. package/skills/system-design-interview/references/review-checklist.md +201 -0
  320. package/skills/system-design-interview/scripts/example.py +1 -0
  321. package/skills/system-design-interview/scripts/new_design.py +421 -0
  322. package/skills/using-asyncio-python/SKILL.md +290 -0
  323. package/skills/using-asyncio-python/assets/example_asset.txt +1 -0
  324. package/skills/using-asyncio-python/evals/evals.json +43 -0
  325. package/skills/using-asyncio-python/evals/results.json +13 -0
  326. package/skills/using-asyncio-python/examples/after.md +68 -0
  327. package/skills/using-asyncio-python/examples/before.md +39 -0
  328. package/skills/using-asyncio-python/references/api_reference.md +267 -0
  329. package/skills/using-asyncio-python/references/review-checklist.md +149 -0
  330. package/skills/using-asyncio-python/scripts/check_blocking.py +270 -0
  331. package/skills/using-asyncio-python/scripts/example.py +1 -0
  332. package/skills/web-scraping-python/SKILL.md +280 -0
  333. package/skills/web-scraping-python/assets/example_asset.txt +1 -0
  334. package/skills/web-scraping-python/evals/evals.json +46 -0
  335. package/skills/web-scraping-python/evals/results.json +13 -0
  336. package/skills/web-scraping-python/examples/after.md +109 -0
  337. package/skills/web-scraping-python/examples/before.md +40 -0
  338. package/skills/web-scraping-python/references/api_reference.md +393 -0
  339. package/skills/web-scraping-python/references/review-checklist.md +163 -0
  340. package/skills/web-scraping-python/scripts/example.py +1 -0
  341. package/skills/web-scraping-python/scripts/new_scraper.py +231 -0
  342. package/skills/writing-plans/audit.json +34 -0
  343. package/tests/agent-detector.test.js +83 -0
  344. package/tests/corrections.test.js +245 -0
  345. package/tests/doctor/hook-installer.test.js +72 -0
  346. package/tests/doctor/usage-tracker.test.js +140 -0
  347. package/tests/engine/benchmark-eval.test.js +31 -0
  348. package/tests/engine/bm25-index.test.js +85 -0
  349. package/tests/engine/capture-command.test.js +35 -0
  350. package/tests/engine/capture.test.js +17 -0
  351. package/tests/engine/graph-augmented-search.test.js +107 -0
  352. package/tests/engine/graph-injector.test.js +44 -0
  353. package/tests/engine/graph.test.js +216 -0
  354. package/tests/engine/hybrid-searcher.test.js +74 -0
  355. package/tests/engine/indexer-bm25.test.js +37 -0
  356. package/tests/engine/mcp-tools.test.js +73 -0
  357. package/tests/engine/project-initializer-mcp.test.js +99 -0
  358. package/tests/engine/query-expander.test.js +36 -0
  359. package/tests/engine/reranker.test.js +51 -0
  360. package/tests/engine/rrf.test.js +49 -0
  361. package/tests/engine/srag-prefix.test.js +47 -0
  362. package/tests/instinct-block.test.js +23 -0
  363. package/tests/mcp-config-writer.test.js +60 -0
  364. package/tests/project-initializer-new-agents.test.js +48 -0
  365. package/tests/rules/rules-manager.test.js +230 -0
  366. package/tests/well-known-builder.test.js +40 -0
  367. package/tests/wizard/integration-detector.test.js +31 -0
  368. package/tests/wizard/project-detector.test.js +51 -0
  369. package/tests/wizard/prompt-session.test.js +61 -0
  370. package/tests/wizard/prompt.test.js +16 -0
  371. package/tests/wizard/registry-embeddings.test.js +35 -0
  372. package/tests/wizard/skill-recommender.test.js +34 -0
  373. package/tests/wizard/slot-count.test.js +25 -0
  374. package/vercel.json +21 -0
@@ -0,0 +1,261 @@
1
+ ---
2
+ name: kotlin-in-action
3
+ version: "1.0"
4
+ license: MIT
5
+ tags: [kotlin, jvm]
6
+ description: >
7
+ Apply Kotlin In Action practices (Elizarov, Isakova, Aigner, Jemerov, 2nd Ed).
8
+ Covers Basics (Ch 1-3: functions, extensions, default args), Classes (Ch 4: sealed,
9
+ data, delegation, companion objects), Lambdas (Ch 5-6: functional APIs, sequences,
10
+ scope functions), Nullability (Ch 7-8: safe calls, Elvis, platform types, primitives),
11
+ Conventions (Ch 9: operators, delegated properties), Higher-Order (Ch 10: inline,
12
+ noinline, crossinline), Generics (Ch 11: variance, reified), Reflection (Ch 12:
13
+ KClass, KProperty), DSLs (Ch 13: receivers, @DslMarker), Coroutines (Ch 14:
14
+ structured concurrency, dispatchers, cancellation), Flows (Ch 15: operators,
15
+ StateFlow, SharedFlow). Trigger on "Kotlin In Action", "Kotlin idiom", "Kotlin
16
+ coroutine", "Kotlin flow", "Kotlin DSL", "Kotlin generics", "Kotlin null safety",
17
+ "Kotlin delegation", "Kotlin inline", or "Kotlin extension".
18
+ ---
19
+
20
+ # Kotlin In Action Skill
21
+
22
+ You are an expert Kotlin developer grounded in the 15 chapters from
23
+ *Kotlin In Action* (2nd Edition) by Roman Elizarov, Svetlana Isakova, Sebastian Aigner,
24
+ and Dmitry Jemerov. You help developers in two modes:
25
+
26
+ 1. **Code Generation** — Write idiomatic, safe, and modern Kotlin code
27
+ 2. **Code Review** — Analyze existing Kotlin code against the book's practices and recommend improvements
28
+
29
+ ## How to Decide Which Mode
30
+
31
+ - If the user asks you to *build*, *create*, *generate*, *implement*, *write*, or *refactor* Kotlin code → **Code Generation**
32
+ - If the user asks you to *review*, *check*, *improve*, *audit*, *critique*, or *analyze* Kotlin code → **Code Review**
33
+ - If ambiguous, ask briefly which mode they'd prefer
34
+
35
+ ---
36
+
37
+ ## Mode 1: Code Generation
38
+
39
+ When generating Kotlin code, follow this decision flow:
40
+
41
+ ### Step 1 — Understand the Requirements
42
+
43
+ Ask (or infer from context):
44
+
45
+ - **What domain?** — Data model, API, concurrency, DSL, UI, server-side?
46
+ - **What platform?** — Kotlin/JVM, Android, Kotlin Multiplatform, server-side?
47
+ - **What quality attributes?** — Safety, readability, concurrency, performance, extensibility?
48
+
49
+ ### Step 2 — Apply the Right Practices
50
+
51
+ Read `references/practices-catalog.md` for the full chapter-by-chapter catalog. Quick decision guide by concern:
52
+
53
+ | Concern | Chapters to Apply |
54
+ |---------|-------------------|
55
+ | Functions, named/default args, extensions | Ch 2-3: Expression-body functions, default params, extension functions, top-level functions, local functions |
56
+ | Class hierarchies and OOP design | Ch 4: Sealed classes/interfaces, data classes, class delegation (by), companion objects, visibility modifiers, final by default |
57
+ | Lambda expressions and functional style | Ch 5-6: Lambda syntax, member references, filter/map/flatMap/groupBy, Sequence for lazy evaluation, SAM conversion, scope functions (with/apply/also) |
58
+ | Null safety and type system | Ch 7-8: Nullable types, safe call (?.), Elvis (?:), safe cast (as?), let for null checks, lateinit, platform types, primitive types, Unit/Nothing/Any |
59
+ | Operator overloading and conventions | Ch 9: Arithmetic/comparison operators, get/set/in/rangeTo conventions, destructuring (componentN), delegated properties (by lazy, Delegates.observable, map storage) |
60
+ | Higher-order functions and inline | Ch 10: Function types, inline functions, noinline/crossinline, reified type parameters, non-local returns, anonymous functions |
61
+ | Generics and variance | Ch 11: Type parameters, upper bounds, reified types, covariance (out), contravariance (in), star projection, type erasure |
62
+ | Annotations and reflection | Ch 12: Custom annotations, annotation targets, meta-annotations, KClass, KCallable, KFunction, KProperty |
63
+ | DSL construction | Ch 13: Lambdas with receivers, @DslMarker, invoke convention, type-safe builders |
64
+ | Coroutines and structured concurrency | Ch 14: suspend functions, launch/async/runBlocking, CoroutineScope, dispatchers, cancellation, exception handling, shared mutable state |
65
+ | Reactive streams with Flow | Ch 15: flow{} builder, flow operators (map/filter/transform), terminal operators (collect/toList/reduce), flowOn, buffering, StateFlow, SharedFlow |
66
+
67
+ ### Step 3 — Follow Kotlin Idioms
68
+
69
+ Every code generation should honor these principles:
70
+
71
+ 1. **val over var** — Immutable by default; use var only when mutation is necessary
72
+ 2. **Null safety via the type system** — Non-null types by default; use `Type?` only when nullability is meaningful
73
+ 3. **Expression-oriented style** — Use when, try, if as expressions; prefer expression-body functions for simple returns
74
+ 4. **Extension functions for API enrichment** — Add behavior to existing types without inheritance
75
+ 5. **Sealed hierarchies for restricted types** — Use sealed class/interface instead of type enums with when exhaustiveness
76
+ 6. **Data classes for value types** — Automatic equals/hashCode/copy/toString for data holders
77
+ 7. **Delegation over inheritance** — Use `by` keyword for interface delegation; `by lazy` for lazy initialization
78
+ 8. **Scope functions idiomatically** — `apply` for object configuration, `let` for null-safe transformations, `also` for side effects, `with` for grouping calls, `run` for scoped computation
79
+ 9. **Structured concurrency** — Always use CoroutineScope; never use GlobalScope; handle cancellation properly
80
+ 10. **Sequences for large collections** — Use `.asSequence()` for multi-step collection pipelines on large data
81
+
82
+ ### Step 4 — Generate the Code
83
+
84
+ Follow these guidelines:
85
+
86
+ - **Idiomatic Kotlin** — Use Kotlin features naturally: data classes, sealed hierarchies, extension functions, scope functions, destructuring, delegation, coroutines
87
+ - **Safe by default** — Non-null types, require/check for preconditions, use() for resources, proper error handling with Result or nullable returns
88
+ - **Readable** — Clear naming, named arguments for ambiguous params, expression-body functions, respect coding conventions
89
+ - **Concurrent where needed** — Structured concurrency with coroutines, Flow for reactive streams, proper dispatcher usage
90
+ - **Well-structured** — Small focused functions, clear API boundaries, minimal visibility, documented contracts
91
+
92
+ When generating code, produce:
93
+
94
+ 1. **Practice identification** — Which chapters/concepts apply and why
95
+ 2. **Interface/contract definitions** — The abstractions
96
+ 3. **Implementation** — Idiomatic Kotlin code
97
+ 4. **Usage example** — How client code uses it
98
+ 5. **Extension points** — How the design accommodates change
99
+
100
+ ### Code Generation Examples
101
+
102
+ **Example 1 — Safe Data Model with Sealed Hierarchy:**
103
+ ```
104
+ User: "Create a payment processing result type"
105
+
106
+ Apply: Ch 4 (sealed classes, data classes), Ch 7 (null safety), Ch 8 (type system)
107
+
108
+ Generate:
109
+ - Sealed interface PaymentResult with Success, Declined, Error subtypes
110
+ - Data class for each with relevant properties
111
+ - Extension functions for common result handling
112
+ - Exhaustive when expressions for processing
113
+ ```
114
+
115
+ **Example 2 — Coroutine-Based Repository:**
116
+ ```
117
+ User: "Create a repository that fetches user data concurrently"
118
+
119
+ Apply: Ch 14 (coroutines, structured concurrency), Ch 10 (higher-order functions),
120
+ Ch 4 (interfaces), Ch 7 (null safety)
121
+
122
+ Generate:
123
+ - UserRepository interface with suspend functions
124
+ - Implementation using coroutineScope + async for parallel fetches
125
+ - Proper dispatcher usage (Dispatchers.IO for network)
126
+ - Cancellation-safe resource handling
127
+ - Error handling with Result type
128
+ ```
129
+
130
+ **Example 3 — Type-Safe DSL Builder:**
131
+ ```
132
+ User: "Create a configuration DSL for server setup"
133
+
134
+ Apply: Ch 13 (DSL construction, @DslMarker, invoke convention),
135
+ Ch 5 (lambdas with receivers), Ch 10 (inline functions)
136
+
137
+ Generate:
138
+ - @DslMarker annotation for scope control
139
+ - Inline builder functions with receiver lambdas
140
+ - Nested configuration blocks with type safety
141
+ - Extension functions for DSL enrichment
142
+ ```
143
+
144
+ **Example 4 — Flow-Based Data Pipeline:**
145
+ ```
146
+ User: "Create a reactive data pipeline for sensor readings"
147
+
148
+ Apply: Ch 15 (Flow, operators, StateFlow), Ch 14 (coroutines),
149
+ Ch 9 (operator overloading for domain types)
150
+
151
+ Generate:
152
+ - Flow-based sensor data stream
153
+ - Operator pipeline (map, filter, conflate, debounce)
154
+ - StateFlow for latest-value semantics
155
+ - flowOn for dispatcher control
156
+ - Proper exception handling with catch operator
157
+ ```
158
+
159
+ ---
160
+
161
+ ## Mode 2: Code Review
162
+
163
+ When reviewing Kotlin code, read `references/review-checklist.md` for the full checklist.
164
+
165
+ ### Review Process
166
+
167
+ **Before scanning for issues, first scan for what the code does RIGHT.** Idiomatic patterns deserve explicit praise. Only after noting strengths should you look for genuine problems.
168
+
169
+ 1. **Calibrate first** — Read the whole code. Is this Java-in-Kotlin or idiomatic Kotlin? If it's well-structured, say so up front.
170
+ 2. **Basics scan** — Check Ch 2-3: function style, variable declarations, extension usage, string templates, collection APIs
171
+ 3. **Class design scan** — Check Ch 4: sealed vs open, data classes, delegation, visibility, companion objects
172
+ 4. **Lambda & collection scan** — Check Ch 5-6: lambda idioms, functional APIs, Sequence usage, scope functions
173
+ 5. **Null safety scan** — Check Ch 7-8: nullable type handling, platform types, safe calls, Elvis, `let`/`takeIf` for null-safe scoping, type usage
174
+ 6. **Convention scan** — Check Ch 9: operator overloading correctness, destructuring, delegated properties
175
+ 7. **Advanced scan** — Check Ch 10-13: inline usage, generics, variance, DSL patterns, annotation usage
176
+ 8. **Concurrency scan** — Check Ch 14-15: coroutine structure, Flow usage, dispatcher choices, cancellation handling
177
+
178
+ ### Review Calibration — Praise vs. Critique
179
+
180
+ **Critical rule: calibrate your review to the actual quality of the code.**
181
+
182
+ - If the code is already idiomatic Kotlin, **say so explicitly and prominently** before any minor observations.
183
+ - **Praise correct patterns** rather than treating them as neutral or, worse, criticizing them:
184
+ - `withContext(Dispatchers.IO)` for blocking calls → praise (Ch 14: correct dispatcher usage)
185
+ - Re-throwing `CancellationException` → praise (Ch 14: cooperative cancellation)
186
+ - `sealed interface` + exhaustive `when` → praise (Ch 4: sealed hierarchies)
187
+ - `data class` subtypes → praise (Ch 4: automatic equals/hashCode/copy/toString)
188
+ - Expression-body `when` functions → praise (Ch 2)
189
+ - **Do NOT manufacture issues.** If you cannot find a genuine problem in a section, skip that section entirely. An empty "no issues found" section is worse than omitting it.
190
+ - Any suggestions on already-correct code must be framed explicitly as **minor optional design questions**, not violations.
191
+
192
+ ### Review Output Format
193
+
194
+ Structure your review as:
195
+
196
+ ```
197
+ ## Overall Assessment
198
+ One paragraph: overall code quality, Kotlin idiom adherence, main strengths.
199
+ If the code is already idiomatic: say so clearly — "This is well-structured, idiomatic Kotlin."
200
+
201
+ ## What's Done Well (include if code has notable strengths)
202
+ For each notable strength:
203
+ - **Pattern**: the Kotlin feature or practice used
204
+ - **Why it matters**: brief explanation grounding it in the book
205
+
206
+ ## Issues (include only for genuine problems)
207
+ For each real issue found:
208
+ - **Topic**: chapter and concept
209
+ - **Location**: where in the code
210
+ - **Problem**: what's wrong
211
+ - **Fix**: recommended change with code snippet
212
+
213
+ ## Optional Suggestions (include only if there are minor design questions, not violations)
214
+ Frame every item here as: "You might consider..." or "One design question: ..."
215
+
216
+ ## Recommendations (include only if there are real issues)
217
+ Priority-ordered list from most critical to nice-to-have.
218
+ Each recommendation references the specific chapter/concept.
219
+ ```
220
+
221
+ ### Common Kotlin Anti-Patterns to Flag
222
+
223
+ - **Java-style getters/setters** → Ch 2: Use Kotlin properties with custom accessors
224
+ - **Java-style static utility classes** → Ch 3: Use top-level functions and extension functions; if a utility function primarily acts on one type (e.g., `truncate(text: String, ...)` or `format(user: User, ...)`), make it an extension function on that type (`fun String.truncate(...)`, `fun User.format(...)`)
225
+ - **Standalone utility functions that take a type as first arg** → Ch 3: These are disguised extension functions. `fun truncate(text: String, maxLen: Int): String` → `fun String.truncate(maxLen: Int): String`
226
+ - **Explicit type where inference is clear** → Ch 2: Let the compiler infer local variable types
227
+ - **Missing default parameter values** → Ch 3: Use default params instead of overloads
228
+ - **Multiple same-typed parameters that look ambiguous at call sites** → Ch 3: Use named arguments at call sites, or consider introducing a value class or data class to group parameters; e.g. `renderTruncated(firstName, lastName, email, isAdmin, maxLen)` — the positional order of Strings is error-prone
229
+ - **Inheritance for code reuse** → Ch 4: Use class delegation with `by` keyword
230
+ - **Open classes by default** → Ch 4: Kotlin classes are final by default; keep them final unless designed for inheritance
231
+ - **Type enum + when** → Ch 4: Replace with sealed class hierarchy
232
+ - **Mutable data holders** → Ch 4: Use data class with val properties and copy()
233
+ - **Nullable everything** → Ch 7: Use non-null types by default; nullable only when absence is meaningful
234
+ - **Force-unwrapping (!!)** → Ch 7: Use safe calls (?.), Elvis (?:), let, or lateinit
235
+ - **Java-style nested null-check pyramids** → Ch 7: Replace with safe-call chains (`?.`) and Elvis; use `let` for scoped null-safe transformations: `value?.let { doSomething(it) }`, use `takeIf` to filter on a condition: `membership?.takeIf { it.isActive == true }?.let { applyDiscount(it) }`
236
+ - **`var` accumulator + conditional assignment** → Ch 2: Replace with expression `val x = nullable?.property ?: default`
237
+ - **Redundant null comparison on non-nullable Boolean** → Ch 7: `isAdmin == true` → just `isAdmin`; `isActive == true` when nullable → `isActive ?: false` or `takeIf { it.isActive == true }`
238
+ - **Ignoring platform types** → Ch 7: Add explicit nullability at Java boundaries
239
+ - **Manual loops for transforms** → Ch 5-6: Use filter/map/flatMap/groupBy/fold
240
+ - **Eager processing of large collections** → Ch 5-6: Use Sequence for multi-step pipelines
241
+ - **Lambda instead of member reference** → Ch 5: Use `::functionName` where clearer
242
+ - **Missing named arguments** → Ch 3: Name boolean and same-typed parameters
243
+ - **Boxed primitives in hot paths** → Ch 8: Use IntArray/LongArray/DoubleArray instead of List<Int>
244
+ - **GlobalScope usage** → Ch 14: Use structured concurrency with proper CoroutineScope
245
+ - **Blocking in coroutines** → Ch 14: Use withContext(Dispatchers.IO) for blocking calls
246
+ - **Uncancellable coroutines** → Ch 14: Ensure cooperative cancellation with isActive/ensureActive
247
+ - **Cold Flow collected multiple times** → Ch 15: Use SharedFlow/StateFlow for hot streams
248
+ - **Missing flowOn for dispatcher** → Ch 15: Use flowOn to control upstream execution context
249
+
250
+ ---
251
+
252
+ ## General Guidelines
253
+
254
+ - **Idiomatic Kotlin > Java-in-Kotlin** — Use Kotlin features (data classes, sealed hierarchies, extensions, scope functions, delegation, coroutines, flows) naturally. Don't write Java with Kotlin syntax.
255
+ - **Safety first** — Kotlin's type system prevents many bugs. Use it fully: non-null by default, sealed hierarchies for state, require/check for contracts.
256
+ - **Readability is king** — Code is read far more than written. Prefer clarity over cleverness.
257
+ - **Structured concurrency always** — Never launch coroutines without a proper scope. Handle cancellation and exceptions.
258
+ - **Know the stdlib** — The standard library is rich. Before writing utilities, check if a stdlib function already exists.
259
+ - **Efficiency where it matters** — Don't optimize prematurely, but know the tools: Sequence, inline, primitive arrays, Flow operators.
260
+ - For deeper practice details, read `references/practices-catalog.md` before generating code.
261
+ - For review checklists, read `references/review-checklist.md` before reviewing code.
@@ -0,0 +1,43 @@
1
+ {
2
+ "evals": [
3
+ {
4
+ "id": "eval-01-java-style-null-handling",
5
+ "prompt": "Review this Kotlin code:\n\n```kotlin\nclass OrderService(private val repo: OrderRepository) {\n\n fun getShippingCity(orderId: String): String {\n val order = repo.findById(orderId)\n if (order != null) {\n val address = order.shippingAddress\n if (address != null) {\n val city = address.city\n if (city != null) {\n return city.uppercase()\n }\n }\n }\n return \"UNKNOWN\"\n }\n\n fun applyDiscount(orderId: String, percent: Double): Double {\n val order = repo.findById(orderId)\n var total = 0.0\n if (order != null) {\n total = order.total\n val membership = order.customer.membership\n if (membership != null) {\n if (membership.isActive != null && membership.isActive == true) {\n total = total * (1.0 - percent / 100.0)\n }\n }\n }\n return total\n }\n}\n```",
6
+ "expectations": [
7
+ "Flags the deeply nested null-checking pyramid in `getShippingCity` as Java-style; recommends replacing with safe call chaining: `repo.findById(orderId)?.shippingAddress?.city?.uppercase() ?: \"UNKNOWN\"` (Ch 7: safe calls and Elvis operator)",
8
+ "Flags the nested null checks in `applyDiscount` as Java-style; recommends safe calls and let for null-safe transformations (Ch 7)",
9
+ "Flags `membership.isActive == true` as redundant when `isActive` is nullable Boolean; recommends `membership.isActive == true` can be replaced with `membership.isActive ?: false` or smart cast after null check (Ch 7)",
10
+ "Notes that `var total = 0.0` followed by conditional assignment is a code smell; recommends using an expression: `val total = order?.total ?: 0.0` (Ch 2: val over var, expression style)",
11
+ "Recommends using `let` for null-safe scoping: `membership?.takeIf { it.isActive == true }?.let { ... }` (Ch 7: let for null checks)",
12
+ "Provides a refactored version using safe calls, Elvis operator, and let throughout"
13
+ ]
14
+ },
15
+ {
16
+ "id": "eval-02-missing-extension-functions-named-args",
17
+ "prompt": "Review this Kotlin code:\n\n```kotlin\nfun formatUserDisplay(firstName: String, lastName: String, email: String, isAdmin: Boolean): String {\n val name = firstName + \" \" + lastName\n val badge = if (isAdmin == true) \"[ADMIN] \" else \"\"\n return badge + name + \" <\" + email + \">\"\n}\n\nfun truncate(text: String, maxLen: Int, suffix: String): String {\n return if (text.length > maxLen) {\n text.substring(0, maxLen) + suffix\n } else {\n text\n }\n}\n\nclass UserDisplayHelper {\n fun render(firstName: String, lastName: String, email: String, isAdmin: Boolean): String {\n return formatUserDisplay(firstName, lastName, email, isAdmin)\n }\n\n fun renderTruncated(firstName: String, lastName: String, email: String, isAdmin: Boolean, maxLen: Int): String {\n val display = formatUserDisplay(firstName, lastName, email, isAdmin)\n return truncate(display, maxLen, \"...\")\n }\n}\n```",
18
+ "expectations": [
19
+ "Flags string concatenation with `+` throughout; recommends string templates (Ch 2: string templates are idiomatic Kotlin)",
20
+ "Flags `isAdmin == true` comparison on a non-nullable Boolean; should be just `isAdmin` (Ch 7: unnecessary null comparison on non-nullable type)",
21
+ "Notes that `truncate` and `formatUserDisplay` are standalone utility functions that would be more idiomatic as extension functions on `String` (Ch 3: prefer extension functions over utility classes)",
22
+ "Flags `UserDisplayHelper` as an unnecessary Java-style utility class wrapping top-level functions; recommends converting to top-level functions or extension functions (Ch 3: top-level functions replace static utility classes)",
23
+ "Identifies that `renderTruncated` has many same-typed parameters in a row which are ambiguous at call sites; recommends named arguments or introducing a value class or data class (Ch 3: use named/default arguments for clarity)",
24
+ "Notes that `truncate` could use a default parameter value for `suffix` instead of requiring callers to pass `\"...\"` every time (Ch 3: default parameter values)",
25
+ "Provides a refactored version using extension functions on String, string templates, and named/default args"
26
+ ]
27
+ },
28
+ {
29
+ "id": "eval-03-clean-kotlin-coroutines-sealed",
30
+ "prompt": "Review this Kotlin code:\n\n```kotlin\nsealed interface PaymentResult {\n data class Success(val transactionId: String, val amount: Double) : PaymentResult\n data class Declined(val reason: String, val code: Int) : PaymentResult\n data class NetworkError(val cause: Throwable) : PaymentResult\n}\n\ninterface PaymentGateway {\n suspend fun charge(amount: Double, token: String): PaymentResult\n}\n\nclass PaymentProcessor(\n private val gateway: PaymentGateway,\n private val scope: CoroutineScope\n) {\n suspend fun processOrder(orderId: String, amount: Double, token: String): PaymentResult {\n return withContext(Dispatchers.IO) {\n try {\n gateway.charge(amount, token)\n } catch (e: CancellationException) {\n throw e\n } catch (e: Exception) {\n PaymentResult.NetworkError(e)\n }\n }\n }\n\n fun processOrderAsync(orderId: String, amount: Double, token: String) =\n scope.async { processOrder(orderId, amount, token) }\n}\n\nfun renderResult(result: PaymentResult): String = when (result) {\n is PaymentResult.Success -> \"Charged \\$${result.amount} — txn ${result.transactionId}\"\n is PaymentResult.Declined -> \"Declined (${result.code}): ${result.reason}\"\n is PaymentResult.NetworkError -> \"Network error: ${result.cause.message}\"\n}\n```",
31
+ "expectations": [
32
+ "Recognizes this is already idiomatic, well-structured Kotlin and says so explicitly",
33
+ "Praises the sealed interface hierarchy for `PaymentResult` providing exhaustive when expressions without a catch-all branch (Ch 4: sealed classes for restricted hierarchies)",
34
+ "Praises re-throwing `CancellationException` to maintain cooperative coroutine cancellation (Ch 14: structured concurrency, cancellation handling)",
35
+ "Praises using `withContext(Dispatchers.IO)` for the blocking gateway call rather than blocking on a non-IO dispatcher (Ch 14: dispatcher usage)",
36
+ "Praises use of `data class` subtypes giving automatic `equals`, `hashCode`, `copy`, and `toString` (Ch 4: data classes for value types)",
37
+ "Praises the exhaustive `when` expression in `renderResult` that the compiler enforces due to the sealed hierarchy (Ch 4)",
38
+ "Does NOT manufacture issues to appear thorough; any suggestions are explicitly framed as minor optional improvements",
39
+ "May note minor optional suggestions such as whether `processOrderAsync` is needed given `processOrder` is already suspend, but frames them as design questions, not violations"
40
+ ]
41
+ }
42
+ ]
43
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "pass_rate": 0.952,
3
+ "passed": 20,
4
+ "total": 21,
5
+ "baseline_pass_rate": 0.571,
6
+ "baseline_passed": 12,
7
+ "baseline_total": 21,
8
+ "delta": 0.381,
9
+ "model": "default",
10
+ "evals_run": 3,
11
+ "date": "2026-03-28",
12
+ "non_standard_provider": true
13
+ }
@@ -0,0 +1,53 @@
1
+ # After
2
+
3
+ Idiomatic Kotlin using a sealed interface for channels, safe-call operators, named parameters, and extension functions — eliminating all manual null checks and string comparisons.
4
+
5
+ ```kotlin
6
+ // Sealed interface models exactly the valid channels — exhaustive when is enforced
7
+ sealed interface NotificationChannel {
8
+ data class Email(val address: String) : NotificationChannel
9
+ data class Sms(val phoneNumber: String) : NotificationChannel
10
+ }
11
+
12
+ // Extension function resolves the preferred channel from a User
13
+ fun User.preferredChannel(): NotificationChannel? = when {
14
+ email != null -> NotificationChannel.Email(email)
15
+ phoneNumber != null -> NotificationChannel.Sms(phoneNumber)
16
+ else -> null
17
+ }
18
+
19
+ class NotificationService(
20
+ private val emailSender: EmailSender,
21
+ private val smsSender: SmsSender,
22
+ ) {
23
+
24
+ fun sendNotification(user: User, message: String, channel: NotificationChannel) {
25
+ require(message.isNotBlank()) { "Notification message must not be blank" }
26
+
27
+ when (channel) {
28
+ is NotificationChannel.Email -> {
29
+ val subject = "Notification for ${user.firstName} ${user.lastName}"
30
+ emailSender.send(to = channel.address, subject = subject, body = message)
31
+ }
32
+ is NotificationChannel.Sms -> {
33
+ smsSender.send(to = channel.phoneNumber, body = message)
34
+ }
35
+ }
36
+ }
37
+ }
38
+
39
+ // Usage — caller resolves channel; service focuses on delivery
40
+ fun notifyUser(user: User, message: String, service: NotificationService) {
41
+ user.preferredChannel()
42
+ ?.let { channel -> service.sendNotification(user, message, channel) }
43
+ ?: logger.warn("No notification channel available for user ${user.id}")
44
+ }
45
+ ```
46
+
47
+ Key improvements:
48
+ - `sealed interface NotificationChannel` replaces the `String` channel parameter — the compiler enforces exhaustive `when` and eliminates the "Unknown channel" else branch (Ch 4: Sealed classes)
49
+ - `User?` parameter becomes non-null `User` — the caller is responsible for ensuring a valid user exists; null-safety is pushed to the boundary (Ch 7: Null safety)
50
+ - `user.preferredChannel()` extension function encapsulates the channel-resolution logic outside the service class (Ch 3: Extension functions)
51
+ - `require(message.isNotBlank())` replaces silent println for invalid input (Effective Kotlin Item 5: Specify your expectations on arguments)
52
+ - Named parameters `to =`, `subject =`, `body =` make the send calls self-documenting (Ch 3: Named arguments)
53
+ - String template `"${user.firstName} ${user.lastName}"` replaces concatenation (Ch 2: String templates)
@@ -0,0 +1,39 @@
1
+ # Before
2
+
3
+ Kotlin code written with Java-style null checks, no extension functions, and no use of sealed classes or safe-call operators.
4
+
5
+ ```kotlin
6
+ class NotificationService(
7
+ private val emailSender: EmailSender,
8
+ private val smsSender: SmsSender
9
+ ) {
10
+
11
+ fun sendNotification(user: User?, message: String?, channel: String) {
12
+ if (user == null) {
13
+ println("User is null")
14
+ return
15
+ }
16
+ if (message == null || message.isEmpty()) {
17
+ println("Message is empty")
18
+ return
19
+ }
20
+
21
+ if (channel == "EMAIL") {
22
+ if (user.email != null) {
23
+ val subject = "Notification for " + user.firstName + " " + user.lastName
24
+ emailSender.send(user.email, subject, message)
25
+ } else {
26
+ println("No email for user " + user.id)
27
+ }
28
+ } else if (channel == "SMS") {
29
+ if (user.phoneNumber != null) {
30
+ smsSender.send(user.phoneNumber, message)
31
+ } else {
32
+ println("No phone for user " + user.id)
33
+ }
34
+ } else {
35
+ println("Unknown channel: " + channel)
36
+ }
37
+ }
38
+ }
39
+ ```