O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java By Comparison

Book Description

Write code that's clean, concise, and to the point: code that others will read with pleasure and reuse. Comparing your code to that of expert programmers is a great way to improve your coding skills. Get hands-on advice to level up your coding style through small and understandable examples that compare flawed code to an improved solution. Discover handy tips and tricks, as well as common bugs an experienced Java programmer needs to know. Make your way from a Java novice to a master craftsman.

This book is a useful companion for anyone learning to write clean Java code. The authors introduce you to the fundamentals of becoming a software craftsman, by comparing pieces of problematic code with an improved version, to help you to develop a sense for clean code. This unique before-and-after approach teaches you to create clean Java code.

Learn to keep your booleans in check, dodge formatting bugs, get rid of magic numbers, and use the right style of iteration. Write informative comments when needed, but avoid them when they are not. Improve the understandability of your code for others by following conventions and naming your objects accurately. Make your programs more robust with intelligent exception handling and learn to assert that everything works as expected using JUnit5 as your testing framework. Impress your peers with an elegant functional programming style and clear-cut object-oriented class design.

Writing excellent code isn't just about implementing the functionality. It's about the small important details that make your code more readable, maintainable, flexible, robust, and faster. Java by Comparison teaches you to spot these details and trains you to become a better programmer.

What You Need:

You need a Java 8 compiler, a text editor, and a fresh mind.That's it.

Table of Contents

  1.  Foreword
  2.  Acknowledgments
  3.  Welcome!
    1. Who Should Read This Book
    2. Why Read This Book?
    3. How to Read This Book
    4. Online Resources
    5. Get Ready for Your Mission to Mars
  4. 1. Start Cleaning Up
    1. Avoid Unnecessary Comparisons
    2. Avoid Negations
    3. Return Boolean Expressions Directly
    4. Simplify Boolean Expressions
    5. Avoid NullPointerException in Conditionals
    6. Avoid Switch Fallthrough
    7. Always Use Braces
    8. Ensure Code Symmetry
    9. What Have You Learned?
  5. 2. Level Up Your Code Style
    1. Replace Magic Numbers with Constants
    2. Favor Enums Over Integer Constants
    3. Favor For-Each Over For Loops
    4. Avoid Collection Modification During Iteration
    5. Avoid Compute-Intense Operations During Iteration
    6. Group with New Lines
    7. Favor Format Over Concatenation
    8. Favor Java API Over DIY
    9. What Have You Learned?
  6. 3. Use Comments Wisely
    1. Remove Superfluous Comments
    2. Remove Commented-Out Code
    3. Replace Comments with Constants
    4. Replace Comments with Utility Methods
    5. Document Implementation Decisions
    6. Document Using Examples
    7. Structure JavaDoc of Packages
    8. Structure JavaDoc of Classes and Interfaces
    9. Structure JavaDoc of Methods
    10. Structure JavaDoc of Constructors
    11. What Have You Learned?
  7. 4. Name Things Right
    1. Use Java Naming Conventions
    2. Follow Getter/Setter Conventions for Frameworks
    3. Avoid Single-Letter Names
    4. Avoid Abbreviations
    5. Avoid Meaningless Terms
    6. Use Domain Terminology
    7. What Have You Learned?
  8. 5. Prepare for Things Going Wrong
    1. Fail Fast
    2. Always Catch Most Specific Exception
    3. Explain Cause in Message
    4. Avoid Breaking the Cause Chain
    5. Expose Cause in Variable
    6. Always Check Type Before Cast
    7. Always Close Resources
    8. Always Close Multiple Resources
    9. Explain Empty Catch
    10. What Have You Learned?
  9. 6. Assert Things Going Right
    1. Structure Tests Into Given-When-Then
    2. Use Meaningful Assertions
    3. Expected Before Actual Value
    4. Use Reasonable Tolerance Values
    5. Let JUnit Handle Exceptions
    6. Describe Your Tests
    7. Favor Standalone Tests
    8. Parametrize Your Tests
    9. Cover the Edge Cases
    10. What Have You Learned?
  10. 7. Design Your Objects
    1. Split Method with Boolean Parameters
    2. Split Method with Optional Parameters
    3. Favor Abstract Over Concrete Types
    4. Favor Immutable Over Mutable State
    5. Combine State and Behavior
    6. Avoid Leaking References
    7. Avoid Returning Null
    8. What Have You Learned?
  11. 8. Let Your Data Flow
    1. Favor Lambdas Over Anonymous Classes
    2. Favor Functional Over Imperative Style
    3. Favor Method References Over Lambdas
    4. Avoid Side Effects
    5. Use Collect for Terminating Complex Streams
    6. Avoid Exceptions in Streams
    7. Favor Optional Over Null
    8. Avoid Optional Fields or Parameters
    9. Use Optionals as Streams
    10. What Have You Learned?
  12. 9. Prepare for the Real World
    1. Use Static Code Analysis Tools
    2. Agree On the Java Format in Your Team
    3. Automate Your Build
    4. Use Continuous Integration
    5. Prepare for and Deliver Into Production
    6. Favor Logging Over Console Output
    7. Minimize and Isolate Multithreaded Code
    8. Use High-Level Concurrency Abstractions
    9. Speed Up Your Program
    10. Know Your Falsehoods
    11. What Have You Learned?
  13.  Bibliography