Clean Code in JavaScript

James Padolsey



Building robust apps starts with creating clean code. In this book, you’ll explore techniques for doing this by learning everything from the basics of JavaScript through to the practices of clean code. You’ll write functional, intuitive, and maintainable code while also understanding how your code affects the end user and the wider community.

The book starts with popular clean-coding principles such as SOLID, and the Law of Demeter (LoD), along with highlighting the enemies of writing clean code such as cargo culting and over-management. You’ll then delve into JavaScript, understanding the more complex aspects of the language. Next, you’ll create meaningful abstractions using design patterns, such as the Class Pattern and the Revealing Module Pattern. You’ll explore real-world challenges such as DOM reconciliation, state management, dependency management, and security, both within browser and server environments. Later, you’ll cover tooling and testing methodologies and the importance of documenting code. Finally, the book will focus on advocacy and good communication for improving code cleanliness within teams or workplaces, along with covering a case study for clean coding.

By the end of this book, you’ll be well-versed with JavaScript and have learned how to create clean abstractions, test them, and communicate about them via documentation.

  • Understand the true purpose of code and the problems it solves for your end-users and colleagues
  • Discover the tenets and enemies of clean code considering the effects of cultural and syntactic conventions
  • Use modern JavaScript syntax and design patterns to craft intuitive abstractions
  • Maintain code quality within your team via wise adoption of tooling and advocating best practices
  • Learn the modern ecosystem of JavaScript and its challenges like DOM reconciliation and state management
  • Express the behavior of your code both within tests and via various forms of documentation
  • Write maintainable JS code using internal abstraction, well-written tests, and well-documented code
  • Understand the agents of clean coding like SOLID principles, OOP, and functional programming
  • Explore solutions to tackle common JavaScript challenges in building UIs, managing APIs, and writing states


建立強健的應用程式始於撰寫乾淨的程式碼。在這本書中,您將透過學習從 JavaScript 基礎到乾淨程式碼實踐的技巧,探索如何做到這一點。您將撰寫功能性、直觀且易於維護的程式碼,同時了解您的程式碼如何影響最終使用者和更廣泛的社群。

本書以流行的乾淨程式碼原則(如 SOLID 原則和 Demeter 法則)為起點,並強調乾淨程式碼的敵人,如盲目模仿和過度管理。接著,您將深入瞭解 JavaScript,了解語言的更複雜方面。接下來,您將使用設計模式(如類別模式和揭示模組模式)創建有意義的抽象。您將探索現實世界中的挑戰,如 DOM 協調、狀態管理、依賴管理和安全性,無論是在瀏覽器還是伺服器環境中。隨後,您將介紹工具和測試方法論,以及文件化程式碼的重要性。最後,本書將著重於倡導和良好溝通,以改善團隊或工作場所的程式碼整潔度,並涵蓋一個乾淨程式碼的案例研究。

通過閱讀本書,您將精通 JavaScript,並學會創建乾淨的抽象、測試它們,並通過文件化進行溝通。

本書的結尾,您將了解程式碼的真正目的以及它對最終使用者和同事解決的問題。考慮文化和語法慣例的影響,發現乾淨程式碼的原則和敵人。使用現代 JavaScript 語法和設計模式來打造直觀的抽象。通過明智地採用工具和倡導最佳實踐,維護團隊內的程式碼品質。瞭解 JavaScript 的現代生態系統及其挑戰,如 DOM 協調和狀態管理。通過測試和各種形式的文件化來表達程式碼的行為。使用內部抽象、良好撰寫的測試和充分文件化的程式碼撰寫易於維護的 JavaScript 程式碼。瞭解乾淨程式碼的原則,如 SOLID 原則、物件導向編程和函數式編程。探索解決常見 JavaScript 挑戰的解決方案,如構建使用者介面、管理 API 和撰寫狀態。


James Padolsey is a passionate JavaScript and UI engineer with over 12 years' experience. James began his journey into JavaScript as a teenager, teaching himself how to build websites for school and small freelance projects. In the early years, he was a prolific blogger, sharing his unique solutions to common problems in the domains of jQuery, JavaScript, and the DOM. He later contributed to the jQuery library itself and authored a chapter within the jQuery Cookbook published by O'Reilly Media. Over subsequent years, James has been exposed to many unique software projects in his employment at Stripe, Twitter, and Facebook, informing his philosophy on what clean coding truly means in the ever-changing ecosystem of JavaScript.


James Padolsey 是一位熱情洋溢的 JavaScript 和 UI 工程師,擁有超過 12 年的經驗。James 在青少年時期開始涉足 JavaScript,自學如何建立學校和小型自由職業項目的網站。在早期,他是一位多產的部落客,在 jQuery、JavaScript 和 DOM 領域分享他對常見問題的獨特解決方案。後來,他貢獻了自己的力量給 jQuery 函式庫本身,並在 O'Reilly Media 出版的《jQuery Cookbook》中撰寫了一章。在接下來的幾年裡,James 在 Stripe、Twitter 和 Facebook 的工作中接觸到許多獨特的軟體專案,這些經驗影響了他對於在不斷變化的 JavaScript 生態系統中什麼是乾淨程式碼的理念。


  1. Setting the Scene
  2. The Tenets of Clean Code
  3. The Enemies of Clean Code
  4. SOLID and Other Principles
  5. Naming Things is Hard
  6. Primitive and Built-In Types
  7. Dynamic Typing
  8. Operators
  9. Parts of Syntax and Scope
  10. Control Flow
  11. Design Patterns
  12. Real-World Challenges
  13. The Landscape of Testing
  14. Writing Clean Tests
  15. Tools for Cleaner Code
  16. Documenting Your Code
  17. Other Peoples' Code
  18. Communication & Advocacy
  19. Case Study