Refactoring in Java - Stefano Violetta - E-Book

Refactoring in Java E-Book

Stefano Violetta

0,0
33,59 €

oder
-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Refactoring in Java

Improving code design and maintainability for Java developers

Stefano Violetta

BIRMINGHAM—MUMBAI

Refactoring in Java

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Kunal Sawant

Publishing Product Manager: Teny Thomas

Senior Content Development Editor: Rosal Colaco

Technical Editor: Vidhisha Patidar

Copy Editor: Safis Editing

Project Manager: Prajakta Naik

Project Coordinator: Manisha Singh

Indexer: Pratik Shirodkar

Production Designer: Ponraj Dhandapani

DevRel Marketing Coordinator: Shrinidhi Manoharan

Business Development Executive: Kriti Sharma

First published: December 2023

Production reference:1081223

Published by Packt Publishing Ltd.

Grosvenor House

11 St Paul’s Square

Birmingham

B3 1RB, UK.

ISBN 978-1-80512-663-8

www.packtpub.com

I dedicate this book to Cristina, the love of my life, and my children, Alessandro and Andrea.

I believe, in life, the most important thing is to keep learning and getting better.

Thanks for showing me something new every day.

– Stefano

Contributors

About the author

Stefano Violetta is a creative backend engineer, bringing over 15 years of experience in software development and architecture. He has worked in a diverse range of companies, spanning from startups to industry giants like eBay. Stefano takes pride in crafting meticulously written code to build sophisticated applications that align with their intended purpose, ensuring functionality and meeting the precise needs of users. Or at least, he tries to! Beyond the world of software, Stefano enjoys immersing himself in reading and watching movies during his free time.

About the reviewers

Giuseppe Bonocore is a senior IT professional, with more than 15 years of experience in the software industry, with different roles. His competencies include cloud technologies, modern software architectures, data, and AI. He has a master’s degree in computer science. He is passionate about open source and the impact of digitalization on organizations and has been speaking at many important technical conferences. In 2021, he published a book about cloud-native applications development. His professional experiences include Salesforce, Red Hat, Accenture, and Docomo Digital, covering many technical leadership roles and deploying huge transformation projects across Europe.

Siddhesh Nikude is a software craftsperson who believes in improving the world’s software development by writing better software and teaching others how to do it. He carries over 10+ years of experience and prefers calling himself a generalist.

Siddhesh started his career as a smart card developer, where he was introduced to Clean Code and XP. It was then he started working in an iterative and incremental approach to notice effective results. Not only does it improve the design and safety of the software, but it also improves the lives of the people working on it. He has worked in a variety of roles from developer to transformation coach on a variety of projects. He has mentored teams in B/TDD, mutation and micro testing, CI/CD pipelines, XP/Lean practices, Clean Code and SOLID principles, DORA metrics, Monte-Carlo simulation, Scrum, Delivery Automation, Test Pyramid, User Stories/Slicing, and Chartering. As a developer, he has worked with Java, Kotlin, Go, TypeScript, React-Native, and so on.

Table of Contents

Preface

Part 1: Introduction to Refactoring

1

What is Refactoring?

What do we mean by refactoring?

Refactoring and clean code

Misconceptions about refactoring

Why you should consider refactoring

Improving the design of your software

Maintainability and scalability

Understanding, avoiding, and fixing bugs

Faster development

When you should refactor

The “Rule of Three”

Preparatory refactoring for smooth feature implementation

Refactoring for bug fixing

Comprehension refactoring

The “Boy Scout Rule”

Planned refactoring

Long-term refactoring and “Branch by Abstraction”

Refactoring in a code review

What you should refactor

Impact analysis

Risk assessment

Value estimation

Prioritization matrix

Further considerations

Refactor or rewrite?

Summary

Further reading

2

Good Coding Habits

Characteristics of good code

Good code is readable

Good code is reliable

Good code is hard to misuse

Good code is modular

Good code is reusable

Clean Code

Why Clean Code?

Some principles of Clean Code

Write SOLID code

Single responsibility principle

Open-closed principle

Liskov substitution principle

Interface segregation principle

Dependency inversion principle

Side effects and mutability

The builder pattern

Causes of bad code

Deadlines

The Broken Window theory

No code review process

Insufficient domain or technical knowledge

Summary

Further reading

Part 2: Essence of Refactoring and Good Code

3

Code Smells

Duplicated code

Long methods

Replace temporary variables with query methods

Parameter object

Replace a function or method with a command

Decompose conditionals

Split loops

Large classes

Switches

Primitive Obsession

Middle man

Message chains

Feature envy methods

Divergent change

Shotgun Surgery

God object

Summary

Further reading

4

Testing

Why you should test (often)

Identifying and fixing defects

Ensuring quality and reliability

Customer satisfaction

Compliance and standards

Security

Integration and compatibility

Confidence and peace of mind

Unit testing

JUnit framework

Integration testing

Mockito

Contract testing

Be safe – checking and improving your test coverage

What is test coverage?

A Java code coverage tool – JaCoCo

Test-driven development

Advantages of TDD

Summary

Further reading

5

Refactoring Techniques

Writing better methods

Extract Method

Inline Method

Extract and inline variables

Combining a function into a class

Moving features between objects

Moving a method or field

Moving statements into/from methods

Hiding delegates

Removing dead code

Organizing data

Field encapsulation

Replacing primitives with objects

Replacing type code with subclasses

Simplifying conditional logic

Returning a special case instead of null

Using polymorphism instead of conditions

Removing duplicated conditions

Guard clauses

Simplifying method calls

Avoiding side effects

Removing setter methods

Using generalization

Pull up field

Push down field

Pull up method

Push down method

Template method

Using enums instead of constants

Summary

Further reading

6

Metaprogramming

What is metaprogramming?

Exploring compile-time metaprogramming tools

Lombok

MapStruct

Exploring runtime metaprogramming tools

Reflection

Weighing the pros and cons of metaprogramming

Summary

Further reading

7

Static and Dynamic Analysis

What is static analysis?

Code errors or bad practices

Security

Cyclomatic complexity

Exception (mis)handling

Automated static analysis tools

What is dynamic analysis?

Debugging

Profiling

Fuzzing

Symbolic execution

Taint tracking

Summary

Further reading

Part 3: Further Learning

8

Crafting Quality Every Day

Code versioning

Graph structure

Local VCSs

Centralized VCSs

Distributed VCSs

Branching strategies

Best practices

Code formatting and style

Indentation

Braces and whitespaces

Line length and wrapping

Comments and documentation

Naming conventions

Automated tools

Code reviews

Email pass-arounds

Code review automation tools

In-person (or remote) teamwork reviews

Pair programming

Advantages

Disadvantages

Styles of pair programming

Time management

Documentation

Requirements

Architecture documentation

Technical documentation

Good practices for documentation

A hard dose of reality

The importance of knowing what you’re doing

Summary

Further reading

9

Beyond Code – Mastering Software Architecture

What is an architecture?

Development

Deployment

System operation

Maintenance

Architectural patterns

Layered architecture

Microservices

Event-driven architecture

Reactive architecture

Monolith to microservices

Bad smells in the microservices architecture

Shared persistence

Shared libraries

Direct communication

Summary

Further reading

Index

Other Books You May Enjoy

Part 1: Introduction to Refactoring

We will start by defining what is meant by “refactoring” and why it is an important practice in software development. We will also cover the benefits of refactoring and the situations in which it is appropriate to use it. We will delve into the specific areas of code that developers should consider refactoring, providing guidance on how to identify and prioritize areas for improvement.

Then we’ll cover the topic of writing high-quality code by day-to-day habits, which each professional should interiorize. We’re going to start by discussing what distinguishes good code from bad code and briefly explore the concept of clean code. Then we’ll delve into the SOLID principles of software design, the importance of (not using) side effects, mutability, and common causes of bad code.

This part has the following chapters:

Chapter 1, What is Refactoring?Chapter 2, Good Coding Habits