views:

170

answers:

4

Hi, i am doing computer science and in a project at the moment. I have had no code input for this as its specifically designed to mimic work i guess. I am the tester.

i have stumbled across a rather complex method written by my colleague who isn't here at the moment, and the method returns a boolean. The issue is however that the method only has true return conditions, there is no false branch anywhere. Does this mean we have a bug because it can never be false?

A: 

Is the method returning boolean the primitive or Boolean the class?

If it is primitive, then note that the default value is false, so the method can also return false if the variable is never written to.

If it is Boolean the class, then it may also return null.

aldrin
A: 

Depends on the requirement. At first glance, it looks useless, but the class may implement an interface where for this special class it is absolutely correct to always return true or false.

Example:

public interface Vehicle {
  public boolean hasTires();
}

public class Car implements Vehicle {
  public boolean hasTires() {
    return true;
  }
}

Edit

OK, this was not the case. I took the opportunity to refactor the code for readability..

public boolean ruleFour(Flight flight, Pilot pilot) {
    SimpleTimeZone offset = timeZones.get(flight.getDeparture());

    int rawOffset = offset.getRawOffset();
    if (rawOffset != 0) {
        rawOffset = rawOffset / 3600000; // converts into hours
    }

    GregorianCalendar start = flight.getDutyStart();

    int localTime = (start.get(Calendar.HOUR_OF_DAY)) + rawOffset;
    if (localTime <= 6) {
        if (pilot.getOnDayOff()) {
            //runs if they are/were off that day
        }
        return true; // can work it as weren't off that day.
    }
    return true; // as start time is not 06:00  local
}

It really looks like uncompleted work. The method name implies that if a rule had to be tested, something like if a pilot is available for a scheduled flight or so. Of course, a pilot may not be available (pilot not on duty) and this is currently not reflected in the method.

Andreas_D
I doubt it is for interfacing. Otherwise one would code it with only one return statement in this case.
trinithis
Me too, and that's why I edited my answer, which was created just before the code was added to the question.
Andreas_D
A: 

Maybe you need to return false in this conditional:

if (p.getOnDayOff()) {
    //runs if they are were on day off
    return false;
}

Just guessing... but the guess makes sense, as the conditional is empty.

Pablo Rodriguez
A: 

It would depend on what it does. However, just because it always returns true does not mean there is a bug. Maybe the operation is part of an interface and it happens that this in implementation true is the only logical value. Or maybe it is a work in progress and the operator saw some future scope for the operation to return false when some functionality is added in a later release, developers could then code against this now without having to change their logic later.

vickirk