Leanpub Header

Skip to main content

Clean Code Fundamentals

Hands-on Guide to Understand the Fundamentals of Software Craftsmanship and Clean Code in Java

Clean Code Fundamentals
This book is 100% completeLast updated on 2026-08-02
+42,590 words in the last 30 days

To become a better software developer needs deep knowledge and practical skills in the field of software development and quality.

This book gives an overview and discusses in-depth knowledge for the analysis and improvement of your software code. You will be able to apply principlespatternstechniques, and tools needed to write clean code.

Minimum price

$19.99

$24.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
460
Pages
102,510Words
About

About

About the Book

Nothing affects the work of a team as much as bad, illegible, sloppy, and quickly written code that has not been designed carefully. Team dynamics can be improved, requirements can be redefined, and the schedule can be modified. However, if bad code takes over, it becomes more and more a burden for the team.

Programmers should develop all the time. Even if they think that they have sufficient skills and knowledge to move around in current projects, they should not stop there, and it is worthwhile for them to learn new concepts, approaches, language, and frameworks from time to time. Learning should be a journey and not a destination.

This book discusses the basics of software qualityprinciplespatterns, and best practices of writing better code. It also contains many code examples in Java of increasing complexity. Among other things topics like software metrics, static software testing, and tools which can help to measure software quality will be covered.

This is a Forever Edition. That means that the book will see periodic updates, which are free for you. The book will use the latest Java version for the examples provided. New content and improvements will be released every 2-3 weeks.

Share this book

Bundle

Bundles that include this book

Author

About the Author

Martin Hock

Martin is a principal software engineer with over 25 years of experience in application design and development. His primary focus is on lightweight Java and open source technologies with expertise in software architecture, web technologies, and the Spring ecosystem.

He has found that teaching is the best way to learn software development. He is a part-time lecturer at two universities and teaches courses on Java, Software Craftsmanship and Clean Code.

Contents

Table of Contents

Preface

  1. What is the goal of this book?
  2. Which topics can you expect?
  3. Who should read this book?
  4. What you must learn about Software Development yourself?
  5. What about the code examples and typographic conventions?
  6. Which literature is this book based on?
  7. Giving Feedback?

1Introduction to Software Craftsmanship and Clean Code

  1. 1.1A Passion for Software Development
  2. 1.2Manifesto for Software Craftsmanship
  3. 1.3Clean Code Developer
  4. 1.4Boy Scout Rule
  5. 1.5Broken Windows Theory
  6. 1.6Cargo Cult Programming
  7. 1.7Knowledge - Expertise

2Basics of Software Design

  1. 2.1Software Design Pyramid
  2. 2.2Basic concepts of OOD
  3. 2.3Goals of Software Design
  4. 2.4Symptoms of bad design
  5. 2.5Criteria for good design
  6. 2.6Information Hiding
  7. 2.7Cohesion
  8. 2.8Coupling
  9. 2.9Cohesion - Coupling
  10. 2.10Big Ball of Mud
  11. 2.11Architecture Principles
  12. 2.12Cognitive Psychology and Architectural Principles
  13. 2.13Layered Architecture
  14. 2.13.1Use of Layered Architecture
  15. 2.13.2Violated Layered Architecture
  16. 2.13.3Horizontal Layering
  17. 2.13.4Feature-Based Layering - Single package
  18. 2.13.5Feature-Based Layering - Slices before layers
  19. 2.13.6Feature-Based Layering – Hexagonal Architecture
  20. 2.13.7The Java Module System
  21. 2.14Architecture Documentation
  22. 2.15Testing the Architecture and Design
  23. 2.16Software Engineering Values
  24. 2.17Team Charter

3Clean Code Best Practices

  1. 3.1Communicate through code
  2. 3.1.1Use Java code conventions and avoid misinformation
  3. 3.1.2Choose an expressive name and avoid mental mapping
  4. 3.1.3Make differences clear with meaningful variable names
  5. 3.1.4Use pronounceable names
  6. 3.1.5Do not hurt the readers
  7. 3.1.6Don`t add redundant context
  8. 3.1.7Don’t add words without additional meaning
  9. 3.1.8Don’t use and or or in method names
  10. 3.1.9Use positive names for boolean variables and functions
  11. 3.1.10Respect the order within classes
  12. 3.1.11Group by line break
  13. 3.1.12Prefer self-explanatory code instead of comments
  14. 3.1.13Use Domain Wording instead of Technical Names
  15. 3.1.14Use Exceptions as a Form of Communication
  16. 3.1.15Refactor step by step
  17. 3.1.16Clean Code Review Checklist
  18. 3.2Bad comments
  19. 3.2.1Redundant comments
  20. 3.2.2Misleading comments
  21. 3.2.3Mandatory comments
  22. 3.2.4Diary comments
  23. 3.2.5Gossip
  24. 3.2.6Position identifier
  25. 3.2.7Write-ups and incidental remarks
  26. 3.2.8Don’t leave commented out code in your codebase
  27. 3.2.9Rules for commenting
  28. Primary Rule
  29. Redundancy Rule
  30. Single Truth Rule
  31. 3.3Classes and objects
  32. 3.3.1Classes
  33. 3.3.2Functions
  34. 3.3.3Variables
  35. 3.4Shapes of code
  36. 3.4.1Spikes
  37. 3.4.2Paragraphs
  38. 3.4.3Paragraphs with headers
  39. 3.4.4Suspicious comments
  40. 3.4.5Intensive use of an object

4Software Quality Assurance

  1. 4.1Test Pyramid
  2. 4.2Test Classification
  3. 4.3Test-driven Development (TDD)
  4. 4.4Unit testing with JUnit 6
  5. 4.4.1Unit Tests
  6. 4.4.2JUnit 6
  7. 4.4.3First unit test
  8. 4.4.4Assertions
  9. 4.4.4.1assertEquals() / assertArrayEquals()
  10. 4.4.4.2assertSame() / assertNotSame()
  11. 4.4.4.3assertTrue() / assertFalse() / assertAll()
  12. 4.4.4.4assertNull() / assertNotNull()
  13. 4.4.4.5assertThrows()
  14. 4.4.4.6assertTimeout()
  15. 4.4.4.7fail()
  16. 4.4.5Annotations
  17. 4.4.5.1@Test
  18. 4.4.5.2@BeforeEach / @AfterEach
  19. 4.4.5.3@BeforeAll / @AfterAll
  20. 4.4.5.4@Disabled
  21. 4.4.5.5@DisplayName
  22. 4.4.5.6@Tag
  23. 4.4.5.7@Timeout
  24. 4.4.6Assumptions
  25. 4.4.6.1assumeFalse()
  26. 4.4.6.2assumeTrue()
  27. 4.4.6.3assumingThat()
  28. 4.4.7Parameterized Tests
  29. 4.4.7.1@ValueSource
  30. 4.4.7.2@MethodSource
  31. 4.4.7.3@CsvSource
  32. 4.4.7.4@CsvFileSource
  33. 4.4.7.5@ArgumentsSource
  34. 4.5More on Unit Tests
  35. 4.5.1Structure tests with Arrange, Act, Assert
  36. 4.5.2Heuristics
  37. 4.5.3Naming of test methods
  38. 4.5.4Object Mother
  39. 4.5.5Test Data Builder
  40. 4.5.6F.I.R.S.T
  41. 4.6Mocking with Mockito
  42. 4.6.1Types of Test Double
  43. 4.6.2Activation
  44. 4.6.3Annotations
  45. 4.6.3.1@Mock
  46. 4.6.3.2@Spy
  47. 4.6.3.3@Captor
  48. 4.7Code Coverage
  49. 4.8Static Code Analysis
  50. 4.9Continuous Integration
  51. 4.9.1Differences between CI, CD, and CD
  52. 4.9.2CI Workflow
  53. 4.9.3Preconditions
  54. 4.9.4Advantages and Disadvantages
  55. 4.9.5Best Practices

5Design Principles

  1. 5.1Goal of Design Principles
  2. 5.2Overview of Design Principles
  3. 5.3SOLID Principles
  4. 5.3.1Single Responsibility Principle
  5. 5.3.1.1Example: Modem
  6. 5.3.1.2Example: Book
  7. 5.3.1.3Example: Product
  8. 5.3.2Open Closed Principle
  9. 5.3.2.1Example: LoanRequestHandler
  10. 5.3.2.2Example: Shape
  11. 5.3.2.3Example: HumanResourceDepartment
  12. 5.3.2.4Example: Calculator
  13. 5.3.2.5Example: FileParser
  14. 5.3.3Liskov Substitution Principle
  15. 5.3.3.1Example: Rectangle
  16. 5.3.3.2Example: Coupon
  17. 5.3.3.3Example: Bird
  18. 5.3.4Interface Segregation Principle
  19. 5.3.4.1Example: MultiFunctionDevice
  20. 5.3.4.2Example: TechEmployee
  21. 5.3.4.3Example: StockOrder
  22. 5.3.5Dependency Inversion Principle
  23. 5.3.5.1Example: UserService
  24. 5.3.5.2Example: Logger
  25. 5.4Packaging Principles - Cohesion
  26. 5.4.1Release Reuse Equivalency Principle
  27. 5.4.2Common Closure Principle
  28. 5.4.3Common Reuse Principle
  29. 5.5Packaging Principles - Coupling
  30. 5.5.1Acyclic Dependencies Principle
  31. 5.5.1.1Example: Cyclic dependency
  32. 5.5.2Stable Dependencies Principle
  33. 5.5.3Stable Abstractions Principles
  34. 5.6Further Design Principles
  35. 5.6.1Speaking Code Principle
  36. 5.6.2Keep It Simple (and) Stupid!
  37. 5.6.3Don’t Repeat Yourself / Once and Only Once
  38. 5.6.4You Ain’t Gonna Need It!
  39. 5.6.5Separation Of Concerns
  40. 5.6.6Law of Demeter (Principle of Least Knowledge)
  41. 5.6.7Tell, Don’t Ask

6Design Patterns of the Gang of Four

  1. Patternitis
  2. 6.1Creational
  3. 6.1.1Singleton
  4. 6.1.1.1Example: Lazy loading
  5. 6.1.1.2Example: Eager loading
  6. 6.1.1.3Example: Static Holder loading
  7. 6.1.1.4Example: Enum singleton
  8. 6.1.2Builder
  9. 6.1.2.1Example: MealBuilder
  10. 6.1.2.2Example: PizzaBuilder
  11. 6.1.2.3Example: Email
  12. 6.1.2.4Example: ImmutablePerson
  13. 6.1.3Factory Method
  14. 6.1.3.1Example: Logger
  15. 6.1.3.2Example: Department
  16. 6.1.4Abstract Factory
  17. 6.1.4.1Example: Car
  18. 6.1.5Prototype
  19. 6.1.5.1Example: Person - Shallow copy
  20. 6.1.5.2Example: Person - Deep copy
  21. 6.1.5.3Example: Person - Copy constructor / factory
  22. 6.1.5.4Prototype registry
  23. 6.2Structural
  24. 6.2.1Facade
  25. 6.2.1.1Example: Travel
  26. 6.2.1.2Example: SmartHome
  27. 6.2.2Decorator
  28. 6.2.2.1Example: Message
  29. 6.2.2.2Example: Window
  30. 6.2.3Adapter
  31. 6.2.3.1Example: Sorter
  32. 6.2.3.2Example: TextFormatter
  33. 6.2.4Composite
  34. 6.2.4.1Example: Graphic
  35. 6.2.4.2Example: Organization Chart
  36. 6.2.5Bridge
  37. 6.2.5.1Example: Message
  38. 6.2.5.2Example: Television
  39. 6.2.6Flyweight
  40. 6.2.6.1Example: Font
  41. 6.2.6.2Example: City
  42. 6.2.7Proxy
  43. 6.2.7.1Example: Spaceship
  44. 6.2.7.2Example: ImageViewer
  45. 6.3Behavioural
  46. 6.3.1State
  47. 6.3.1.1Example: MP3Player
  48. 6.3.1.2Example: Door
  49. 6.3.2Template Method
  50. 6.3.2.1Example: Compiler
  51. 6.3.2.2Example: Callbackable
  52. 6.3.3Strategy
  53. 6.3.3.1Example: Compression
  54. 6.3.3.2Example: LogFormatter
  55. 6.3.4Observer
  56. 6.3.4.1Example: DataStore
  57. 6.3.4.2Example: Influencer
  58. 6.3.5Chain of Responsibility
  59. 6.3.5.1Example: Purchase
  60. 6.3.5.2Example: Authentication
  61. 6.3.6Command
  62. 6.3.6.1Example: FileSystem
  63. 6.3.6.2Example: Television
  64. 6.3.7Interpreter
  65. 6.3.7.1Example: HexBinary
  66. 6.3.7.2Example: Calculator
  67. 6.3.8Iterator
  68. 6.3.8.1Example: Cars - intern
  69. 6.3.8.2Example: Cars - extern
  70. 6.3.9Mediator
  71. 6.3.9.1Example: Chat
  72. 6.3.9.2Example: Aircraft
  73. 6.3.10Memento
  74. 6.3.10.1Example: Editor
  75. 6.3.10.2Example: Balance
  76. 6.3.11Visitor
  77. 6.3.11.1Example: Fridge
  78. 6.3.11.2Example: Figures

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub