2

I've been seeing developers pattern after some code in Zend and I'm trying to figure out why Zend might have implemented http response code checking this way:

/**
 * Check whether the response in successful
 *
 * @return boolean
 */
public function isSuccessful()
{
    $restype = floor($this->code / 100);
    if ($restype == 2 || $restype == 1) { // Shouldn't 3xx count as success as well ???
        return true;
    }

    return false;
}

Specifically, why would they do that instead of

public function isSuccessful()
{
    return $this->code >= 100 && $this->code < 300
}
4
  • Wouldn't the floor and the / 100 spend CPU cycles needlessly? Commented Aug 8, 2014 at 14:32
  • 1
    That statement is clearly preemptive optimization, try not to think like that... Unless you are doing a 1 billion loop with a floor thats useless, don't take it into account. Floor is a function that is definitely extremely fast compared to userland code... Commented Aug 8, 2014 at 14:34
  • i'm no expert but a compiler should optimize a /int into a shift which is fast - see here (not the best example though) stackoverflow.com/questions/3850665/… Commented Aug 8, 2014 at 14:34
  • FYI the equivalent function in ZF2 is basically the same as your second example. Like Lee McNeil said I think it was just down to personal choice by whoever wrote the ZF1 func. Commented Aug 8, 2014 at 14:53

2 Answers 2

2

maybe because they just care for

5 03

4 04

4 01

3 02

2 01

and so on, the basicly are interested in the class of errortype not the specifics

Sign up to request clarification or add additional context in comments.

Comments

1

Looking at the code I don't think there is any reasoning other than personal choice by the developer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.