2
\$\begingroup\$

I have the following code to extract results of an airline web service. I am wondering if there is any better approach to this. The code is supposed to ignore any flight that has more than 1 transit.

All retrieved results from the airline web service are in the AvailabilityRes class.

for (int i = 0; i < availabilityRes.getResults().getResult().size(); i++) {
                if (availabilityRes.getResults().getResult().get(i)
                        .getOutbound().getFlightSegment().size() > 2) { //more than one transit
                    continue;
                } else {
                    flight = new Flight();
                    try {

                        date = convertAirlineStringDateTimeToDateFormat(availabilityRes
                                .getResults().getResult().get(i).getOutbound()
                                .getFlightSegment().get(0).getDeparture()
                                .getDateTime());
                    } catch (ParseException p) {
                        p.printStackTrace();
                    }

                    flight.setOutbound_date(extractAirlineDate(date));

                    flight.setOutbound_time1(extractAirlineTime(date));

                    flight.setRoute1(availabilityRes.getResults().getResult()
                            .get(i).getOutbound().getFlightSegment().get(0)
                            .getDeparture().getAirport());
                    try {

                        date = convertAirlineStringDateTimeToDateFormat(availabilityRes
                                .getResults().getResult().get(i).getInbound()
                                .getFlightSegment().get(0).getDeparture()
                                .getDateTime());
                    } catch (ParseException p) {
                        p.printStackTrace();
                    }

                    flight.setInbound_date(extractAirlineDate(date));

                    flight.setReturn_time1(extractAirlineTime(date));

                    flight.setRoute2(availabilityRes.getResults().getResult()
                            .get(i).getInbound().getFlightSegment().get(0)
                            .getDeparture().getAirport());
                    if (availabilityRes.getResults().getResult().get(i)
                            .getOutbound().getFlightSegment().size() == 2) { // has
                                                                                // 1
                                                                                // transit
                        flight.setTransit_outbound(true);
                        try {

                            date = convertAirlineStringDateTimeToDateFormat(availabilityRes
                                    .getResults().getResult().get(i)
                                    .getOutbound().getFlightSegment().get(1)
                                    .getArrival().getDateTime());
                        } catch (ParseException p) {
                            p.printStackTrace();
                        }
                        flight.setOutbound_time2(extractAirlineTime(date));
                        flight.setRoute1(flight.getRoute1()
                                + " To "
                                + availabilityRes.getResults().getResult()
                                        .get(i).getOutbound()
                                        .getFlightSegment().get(1).getArrival()
                                        .getAirport());
                        flight.setRoute2(flight.getRoute2()
                                + " To "
                                + availabilityRes.getResults().getResult()
                                        .get(i).getInbound().getFlightSegment()
                                        .get(1).getArrival().getAirport());
                        try {

                            date = convertAirlineStringDateTimeToDateFormat(availabilityRes
                                    .getResults().getResult().get(i)
                                    .getInbound().getFlightSegment().get(1)
                                    .getArrival().getDateTime());
                        } catch (ParseException p) {
                            p.printStackTrace();
                        }
                        flight.setReturn_time2(extractAirlineTime(date));

                    } else {
                        try {
                            date = convertAirlineStringDateTimeToDateFormat(availabilityRes
                                    .getResults().getResult().get(i)
                                    .getOutbound().getFlightSegment().get(0)
                                    .getArrival().getDateTime());
                        } catch (ParseException p) {
                            p.printStackTrace();
                        }
                        flight.setOutbund_time2(extractAirlineTime(date));
                        flight.setRoute1(" To "
                                + availabilityRes.getResults().getResult()
                                        .get(i).getOutbound()
                                        .getFlightSegment().get(0).getArrival()
                                        .getAirport());
                    }

                    source = new Source();
                    source.setSource_name("Airline Airline");
                    source.setUrl(availabilityRes.getResults().getResult()
                            .get(i).getUrl());
                    source.setPrice(availabilityRes.getResults().getResult()
                            .get(i).getFareInfos().getTotalFare());
                    flight.setSource(source); //add source to flight
                    results.getFlights().add(flight); //add to output array
                }
}
\$\endgroup\$
5
\$\begingroup\$
  1. Loop Conditional

    A really good practice to employ when designing loops, is to simplify the comparison value of the conditional expression as much as possible, to boost performance. In this case, if the value of availabilityRes.getResults().getResult().size() is not going to change, store it in an external variable.

    int n = availabilityRes.getResults().getResult().size()
    for (int i = 0; i < n; i++) {
    
  2. Eliminate Unnecessary Function Calls

    In most cases, when you are using the value of a function multiple times, and you are sure that the function's value is not going to change, you should store that value in a variable to optimize the speed of the code.

    For example, availabilityRes.getResults().getResult().get(i) is used 14 different times in your code. Not only does it obfuscate the code from being easily read, but it eats up processor power unnecessarily because that's three function calls where I assume the value doesn't change. So, every time you see something like that, my advice is to store the value in a variable at the outermost scope it's needed, to optimize performance.

    result = availabilityRes.getResults().getResult();
    int n = result.size();
    for (int i = 0; i < n; i++) {
    
        thisResult = result.get(i);
        outboundSegment = this_result.getOutbound().getFlightSegment();
        if (outboundSegment.size() > 2) { //more than one transit
            continue;
        } else {
            flight = new Flight();
            outboundSegmentDeparture = outboundSegment.get(0).getDeparture();
            try {
                date = convertAirlineStringDateTimeToDateFormat(outboundSegmentDeparture.getDateTime());
        ....
    

    Something like that - the trick here is to try to minimize the function calls required.

  3. Comments, comments, comments...

    I cannot stress this point enough. No matter how readable you think your code is, there is nothing like a good comment written in a language you understand.

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ The only thing I would add to it is, it might be a good idea to extract pieces of functionality to separate methods so that these are more easily readable. \$\endgroup\$ – CptBartender Jun 25 '15 at 11:14
  • 2
    \$\begingroup\$ This is entirely subjective but I'd rather write code that is as self documenting as possible, only commenting the function declarations and business logic. Just a tip for the OP. \$\endgroup\$ – Machinarius Jun 25 '15 at 12:04
  • \$\begingroup\$ @CptBartender does not it put more load on GC because the local variables of each extra method? \$\endgroup\$ – Daniel Newtown Jun 25 '15 at 22:13
  • \$\begingroup\$ @DanielNewton theoretically yes, it does, albeit only slightly, and if that is a limiting factor for your purposes, then you're either doing something else wrong or just plain not using the right tools for the job at hand. Don't go overboard with this, but in general you shouldn't ever worry about GC when refactoring your code and extracting methods. \$\endgroup\$ – CptBartender Jun 30 '15 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.