Leanpub Header

Skip to main content

Clean Code Fundamentals

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

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

$24.99

Author earns

$19.99
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
328
Pages
54,751Words
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

Author

About the Author

Martin Hock

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.2Bad comments
  18. 3.2.1Redundant comments
  19. 3.2.2Misleading comments
  20. 3.2.3Mandatory comments
  21. 3.2.4Diary comments
  22. 3.2.5Gossip
  23. 3.2.6Position identifier
  24. 3.2.7Write-ups and incidental remarks
  25. 3.2.8Don’t leave commented out code in your codebase
  26. 3.2.9Rules for commenting
  27. Primary Rule
  28. Redundancy Rule
  29. Single Truth Rule
  30. 3.3Classes and objects
  31. 3.3.1Classes
  32. 3.3.2Functions
  33. 3.3.3Variables
  34. 3.4Shapes of code
  35. 3.4.1Spikes
  36. 3.4.2Paragraphs
  37. 3.4.3Paragraphs with headers
  38. 3.4.4Suspicious comments
  39. 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.1Heuristics
  36. 4.5.2Naming of test methods
  37. 4.5.3Object Mother
  38. 4.5.4Test Data Builder
  39. 4.5.5F.I.R.S.T
  40. 4.6Mocking with Mockito
  41. 4.6.1Types of Test Double
  42. 4.6.2Activation
  43. 4.6.3Annotations
  44. 4.6.3.1@Mock
  45. 4.6.3.2@Spy
  46. 4.6.3.3@Captor
  47. 4.7Code Coverage
  48. 4.8Static Code Analysis
  49. 4.9Continuous Integration
  50. 4.9.1Differences between CI, CD, and CD
  51. 4.9.2CI Workflow
  52. 4.9.3Preconditions
  53. 4.9.4Advantages and Disadvantages
  54. 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

6Design Patterns of the Gang of Four

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

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

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 $14 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