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
}
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
  • Wouldn't the floor and the / 100 spend CPU cycles needlessly? – Adam Tegen Aug 08 '14 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... – Mathieu Dumoulin Aug 08 '14 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) http://stackoverflow.com/questions/3850665/how-can-i-use-bit-shifting-to-replace-integer-division – birdspider Aug 08 '14 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. – Tim Fountain Aug 08 '14 at 14:53

2 Answers2

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

birdspider
  • 3,034
  • 1
  • 16
  • 25
1

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

Lee McNeil
  • 46
  • 3