Leanpub Header

Skip to main content

Elegant Java

Hands-on Guide to write 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 many examples which help you to write clean code. For more information like principlespatternstechniques, and tools have a look at my book Clean Code Fundamentals.

Minimum price

$14.99

$19.99

You pay

$19.99

Author earns

$15.99
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
93
Pages
16,876Words
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 best practices of writing better code. It also contains many code examples in Java of increasing complexity.

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.

Share this book

Author

About the Author

Martin Hock

Contents

Table of Contents

Preface

  1. What about the code examples and typographic conventions?
  2. Which literature is this book based on?
  3. Giving feedback?

1Java Best Practices

  1. 1.1Avoid instantiation for utility classes
  2. 1.2Use immutable objects
  3. 1.3Prefer records for immutability
  4. 1.4Provide immutable decorators for sensitive mutable classes
  5. 1.5Use pattern matching for instanceof
  6. 1.6Consider using sealed classes and interfaces
  7. 1.7Use record patterns for destructuring
  8. 1.8Avoid constant interfaces
  9. 1.9Avoid global constant classes
  10. 1.10Favour composition over inheritance
  11. 1.11Consider using lambdas
  12. 1.12Avoiding Primitive Obsession
  13. 1.13Use the @Override Annotation
  14. 1.14Use the @FunctionalInterface Annotation
  15. 1.15Prefer returning Null-Objects over null
  16. 1.16Avoid null as a method parameter
  17. 1.17Prefer enhanced loops over for loops
  18. 1.18Code against interfaces not implementations
  19. 1.19Use existing exceptions
  20. 1.20Validate method parameter
  21. 1.21Minimize method parameters for improved readability
  22. 1.22Prevent NullPointerException for String comparison
  23. 1.23Safely cast long to int
  24. 1.24Convert integers to floating-point for floating-point math operations
  25. 1.25Use generics in favor of raw types
  26. 1.26Prefer enums over int constants
  27. 1.27Prefer the == operator over equals for comparing enum values
  28. 1.28Be aware of the contract between equals and hashCode
  29. 1.29Use text blocks for multi-line strings
  30. 1.30Use always braces for the body of all statements
  31. 1.31Simplifying boolean expressions
  32. 1.32Pre-calculate the length in loops
  33. 1.33Avoid constructor instantiation of String
  34. 1.34Use StringBuilder for concatenation
  35. 1.35Reduce lookups in collection containers
  36. 1.36Instantiate wrapper objects with valueOf
  37. 1.37Use entrySet for iterating
  38. 1.38Use isEmpty() for String length
  39. 1.39Use switch expressions for concise control flow
  40. 1.40Avoid using the default case in switch statements for sealed classes
  41. 1.41Prefer method references over lambdas for conciseness
  42. 1.42Use the orElse and orElseGet methods for optional values
  43. 1.43Enhance code clarity with EnumMap
  44. 1.44Use local variable type inference with var judiciously
  45. 1.45Practice defensive programming
  46. 1.46Implement effective logging practices

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