Dart:
Refactoring
How to:
Example 1: Renaming and Extracting Methods
Before refactoring, you might have a piece of code that mixes different levels of abstraction or responsibilities, like calculating a discount and then applying it:
void main() {
var price = 100.0;
var discount = 0.2;
var finalPrice = price - (price * discount);
print("Final price: $finalPrice");
}
Output:
Final price: 80.0
After refactoring, you can extract the discount calculation into its own method and give it a meaningful name:
void main() {
var price = 100.0;
var discount = 0.2;
var finalPrice = calculateFinalPrice(price, discount);
print("Final price: $finalPrice");
}
double calculateFinalPrice(double price, double discount) {
return price - (price * discount);
}
Output:
Final price: 80.0
By extracting the calculation into a method, you now have a clearly defined operation that can be reused, tested independently, and easily modified.
Example 2: Simplifying Conditional Expressions
Before refactoring, conditional statements might be overly complex or hard to read:
void main() {
var customerType = "regular";
double discount;
if (customerType == "regular") {
discount = 0.05;
} else if (customerType == "member") {
discount = 0.1;
} else {
discount = 0.0;
}
print("Discount: $discount");
}
Output:
Discount: 0.05
After refactoring, consider using a map for clearer structure and easier updates or extensions to customer types and discounts:
void main() {
var customerType = "regular";
var discounts = {
"regular": 0.05,
"member": 0.1,
"none": 0.0,
};
var discount = discounts[customerType] ?? 0.0;
print("Discount: $discount");
}
Output:
Discount: 0.05
This refactor not only makes the code more concise but also encapsulates the logic for determining discounts in a way that’s easier to understand and maintain.
Third-party Libraries for Refactoring
When it comes to refactoring in Dart, especially within Flutter apps, the Dart DevTools suite is invaluable. It includes performance tools, a widget inspector, and a source-level debugger. While not a third-party library, Dart DevTools is often used alongside libraries like flutter_bloc
for cleanly managing state in a way that’s conducive to refactoring for improved modularity and readability. Unfortunately, due to the scope of this entry, specific code examples using third-party libraries won’t be provided here, but developers are encouraged to explore these tools to enhance the refactoring process in their Dart/Flutter applications.