preload
Dez 01

Imagine you have java code like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   if (o.isShipped) //order is shipped --> archive
                {
                    //archived 21 days after delivery
                    Calendar cal = new GregorianCalendar();
                    cal.add(Calendar.DAY_OF_MONTH,DAYS_TO_ARCHIVE_AFTER_SHIPPING);
                    try {
                       ...
                        }
                    } catch (Exception ex) {
                        ...
                    }
                } else if (o.isCancelled == true || o.isClosed == true) {
                    //cancelled more than 7 days ago
                    ...
                                } else {
                                   ...
                                }
                            }
                        }
                    } catch (Exception ex) {
                        ...
                    }
                    try {
                        if (o.isClosedAt.getTimeInMillis() <= cal.getTimeInMillis()) {
 
                           ...
 
                                } else {
                                    ...
                                }
                            }
                        }
                    } catch (Exception ex) {
                        ...
                    }
                } else if (o.isReadyForShipping && o.isDispatched) {
                 ...
                }

Isn’t it extreme hard to read and to understand? Martin Fowlers Book “Refactoring” shows how to handle that. Replace all these nested if…else with if-guards. It could lool like this:

1
2
3
4
if (order.isShipped && !isCancelled && !isClosed) archiveOrder();
if (!order.isShipped && order.isCancelled && order.isCancelledAt.getTimeInMillis() <= cal.getTimeInMillis()) moveReservationOnCancel();
if (!order.isShipped && order.isClosed && order.isClosedAt.getTimeInMillis() <= cal.getTimeInMillis()) moveReservationOnClosing(); 
...

What did we do here? We renamed the order object o into order, and put the actions into separate methods. To improve readability a bit more we can create a method for the boolean checks. See the next example:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (isOrderShipped(order)) archiveOrder();
if (isOrderCanceled(order)) moveReservationOnCancel();
if (isOrderClosed(order)) moveReservationOnClosing(); 
...
private boolean isOrderShipped(OrderDetails order) {
        return order.isShipped && !order.isCancelled && !order.isClosed;
}
private boolean isOrderCancelled(OrderDetails order) {
        return !order.isShipped && order.isCancelled && order.isCancelledAt.getTimeInMillis() <= cal.getTimeInMillis();
}
private boolean isOrderClosed(OrderDetails order) {
        return !order.isShipped && order.isClosed && order.isClosedAt.getTimeInMillis() <= cal.getTimeInMillis();
}

Pretty readable isn’t it? You just need a few seconds to understand what is going on here. You have the main flow right in sight. When it is needed to go deeper into the code you can pick any method you like to inspect.

Leave a Reply

You must be logged in to post a comment.