Refactoring in Java

Book description

Master code refactoring techniques, improve code quality, design, and maintainability, and boost your development productivity with this comprehensive handbook

Key Features

  • Get a thorough understanding of code refinement for enhanced codebase efficiency
  • Work with real-world examples and case studies for hands-on learning and application
  • Focus on essential tools, emphasizing development productivity and robust coding habits
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

Refactoring in Java serves as an indispensable guide to enhancing your codebase’s quality and maintainability.

The book begins by helping you get to grips with refactoring fundamentals, including cultivating good coding habits and identifying red flags. You’ll explore testing methodologies, essential refactoring techniques, and metaprogramming, as well as designing a good architecture. The chapters clearly explain how to refactor and improve your code using real-world examples and proven techniques. Part two equips you with the ability to recognize code smells, prioritize tasks, and employ automated refactoring tools, testing frameworks, and code analysis tools. You’ll discover best practices to ensure efficient code improvement so that you can navigate complexities with ease. In part three, the book focuses on continuous learning, daily practices enhancing coding proficiency, and a holistic view of the architecture. You’ll get practical tips to mitigate risks during refactoring, along with guidance on measuring impact to ensure that you become an efficient software craftsperson.

By the end of this book, you’ll be able to avoid unproductive programming or architecturing, detect red flags, and propose changes to improve the maintainability of your codebase.

What you will learn

  • Recognize and address common issues in your code
  • Find out how to determine which improvements are most important
  • Implement techniques such as using polymorphism instead of conditions
  • Efficiently leverage tools for streamlining refactoring processes
  • Enhance code reliability through effective testing practices
  • Develop the skills needed for clean and readable code presentation
  • Get to grips with the tools you need for thorough code examination
  • Apply best practices for a more efficient coding workflow

Who this book is for

This book is for Java developers, software architects, and technical leads looking for a comprehensive guide to advancing their skills in software design and refactoring. The book is ideal for experienced Java enthusiasts, quality assurance engineers, and codebase maintainers as it provides practical insights, real-world examples, and essential patterns. Development managers who want to foster clean coding practices by using best practices for efficient workflows will also find this book useful.

Table of contents

  1. Refactoring in Java
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. GitHub repository
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1: Introduction to Refactoring
  7. Chapter 1: What is Refactoring?
    1. What do we mean by refactoring?
      1. Refactoring and clean code
      2. Misconceptions about refactoring
    2. Why you should consider refactoring
      1. Improving the design of your software
      2. Maintainability and scalability
      3. Understanding, avoiding, and fixing bugs
      4. Faster development
    3. When you should refactor
      1. The “Rule of Three”
      2. Preparatory refactoring for smooth feature implementation
      3. Refactoring for bug fixing
      4. Comprehension refactoring
      5. The “Boy Scout Rule”
      6. Planned refactoring
      7. Long-term refactoring and “Branch by Abstraction”
      8. Refactoring in a code review
    4. What you should refactor
      1. Impact analysis
      2. Risk assessment
      3. Value estimation
      4. Prioritization matrix
      5. Further considerations
      6. Refactor or rewrite?
    5. Summary
    6. Further reading
  8. Chapter 2: Good Coding Habits
    1. Characteristics of good code
      1. Good code is readable
      2. Good code is reliable
      3. Good code is hard to misuse
      4. Good code is modular
      5. Good code is reusable
    2. Clean Code
      1. Why Clean Code?
      2. Some principles of Clean Code
    3. Write SOLID code
      1. Single responsibility principle
      2. Open-closed principle
      3. Liskov substitution principle
      4. Interface segregation principle
      5. Dependency inversion principle
    4. Side effects and mutability
      1. The builder pattern
    5. Causes of bad code
      1. Deadlines
      2. The Broken Window theory
      3. No code review process
      4. Insufficient domain or technical knowledge
    6. Summary
    7. Further reading
  9. Part 2: Essence of Refactoring and Good Code
  10. Chapter 3: Code Smells
    1. Duplicated code
    2. Long methods
      1. Replace temporary variables with query methods
      2. Parameter object
      3. Replace a function or method with a command
      4. Decompose conditionals
      5. Split loops
    3. Large classes
    4. Switches
    5. Primitive Obsession
    6. Middle man
    7. Message chains
    8. Feature envy methods
    9. Divergent change
    10. Shotgun Surgery
    11. God object
    12. Summary
    13. Further reading
  11. Chapter 4: Testing
    1. Why you should test (often)
      1. Identifying and fixing defects
      2. Ensuring quality and reliability
      3. Customer satisfaction
      4. Compliance and standards
      5. Security
      6. Integration and compatibility
      7. Confidence and peace of mind
    2. Unit testing
      1. JUnit framework
    3. Integration testing
      1. Mockito
    4. Contract testing
    5. Be safe – checking and improving your test coverage
      1. What is test coverage?
      2. A Java code coverage tool – JaCoCo
    6. Test-driven development
      1. Advantages of TDD
    7. Summary
    8. Further reading
  12. Chapter 5: Refactoring Techniques
    1. Writing better methods
      1. Extract Method
      2. Inline Method
      3. Extract and inline variables
      4. Combining a function into a class
    2. Moving features between objects
      1. Moving a method or field
      2. Moving statements into/from methods
      3. Hiding delegates
      4. Removing dead code
    3. Organizing data
      1. Field encapsulation
      2. Replacing primitives with objects
      3. Replacing type code with subclasses
    4. Simplifying conditional logic
      1. Returning a special case instead of null
      2. Using polymorphism instead of conditions
      3. Removing duplicated conditions
      4. Guard clauses
    5. Simplifying method calls
      1. Avoiding side effects
      2. Removing setter methods
    6. Using generalization
      1. Pull up field
      2. Push down field
      3. Pull up method
      4. Push down method
      5. Template method
    7. Using enums instead of constants
    8. Summary
    9. Further reading
  13. Chapter 6: Metaprogramming
    1. What is metaprogramming?
    2. Exploring compile-time metaprogramming tools
      1. Lombok
      2. MapStruct
    3. Exploring runtime metaprogramming tools
      1. Reflection
    4. Weighing the pros and cons of metaprogramming
    5. Summary
    6. Further reading
  14. Chapter 7: Static and Dynamic Analysis
    1. What is static analysis?
      1. Code errors or bad practices
      2. Security
      3. Cyclomatic complexity
      4. Exception (mis)handling
      5. Automated static analysis tools
    2. What is dynamic analysis?
      1. Debugging
      2. Profiling
      3. Fuzzing
      4. Symbolic execution
      5. Taint tracking
    3. Summary
    4. Further reading
  15. Part 3: Further Learning
  16. Chapter 8: Crafting Quality Every Day
    1. Code versioning
      1. Graph structure
      2. Local VCSs
      3. Centralized VCSs
      4. Distributed VCSs
      5. Branching strategies
      6. Best practices
    2. Code formatting and style
      1. Indentation
      2. Braces and whitespaces
      3. Line length and wrapping
      4. Comments and documentation
      5. Naming conventions
      6. Automated tools
    3. Code reviews
      1. Email pass-arounds
      2. Code review automation tools
      3. In-person (or remote) teamwork reviews
    4. Pair programming
      1. Advantages
      2. Disadvantages
      3. Styles of pair programming
      4. Time management
    5. Documentation
      1. Requirements
      2. Architecture documentation
      3. Technical documentation
      4. Good practices for documentation
    6. A hard dose of reality
    7. The importance of knowing what you’re doing
    8. Summary
    9. Further reading
  17. Chapter 9: Beyond Code – Mastering Software Architecture
    1. What is an architecture?
      1. Development
      2. Deployment
      3. System operation
      4. Maintenance
    2. Architectural patterns
      1. Layered architecture
      2. Microservices
      3. Event-driven architecture
      4. Reactive architecture
    3. Monolith to microservices
    4. Bad smells in the microservices architecture
      1. Shared persistence
      2. Shared libraries
      3. Direct communication
    5. Summary
    6. Further reading
  18. Index
    1. Why subscribe?
  19. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Refactoring in Java
  • Author(s): Stefano Violetta
  • Release date: December 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781805126638