Method names and business meaning

When you are maintaining legacy applications it come to a point where you need break down the code into smaller pieces so that you can understand better. For example the following code may be in the middle of a customer rating section based on previous orders placed by the customer.


List previousOrders = order.getPreviousOrdersByCustomerId(long customerId);
if ( prevousOrders != null && !previousOrders.isEmpty())
{
// Customer rating logic
.........
}

In this case, if you take above lines and covert them into a smaller methods then it serve two purposes:

1) Single Responsibility for each method and
2) Better readability

private List getPreviousOrdersByCustmerId(long customerId) {
// return previous customer orders for a given customer
}


private boolean hasPreviousOrders( List previousOrders) {
return previousOrders != null && !previousOrders.isEmty();
}

Now customer ratings just need to worry about how to rate the customer by just looking at previous orders size rather than how it got previous orders. By separating the code into smaller methods you can understand the code better and maintenance becomes much easier.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s