HiveBrain v1.2.0
Get Started
← Back to all entries
principlephpMinor

Is this good approach to use debug functions for implementing features?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thisdebugfeaturesforgoodfunctionsuseimplementingapproach

Problem

Just thinking if using code like this

<?php

abstract class helper
{
    private static $_cycles = array();

    public static function isOdd($v)
    {
        return (0 == ($v % 2)) ? false: true;
    }

    public static function isEven($v)
    {
        return !self::isOdd($v);
    }

    public static function cycle($odd, $even)
    {
        $trace = debug_backtrace();
        $trace = $trace[0];
        $cycle = crc32(serialize($trace));

        if (!isset(self::$_cycles[$cycle]))
        {
            self::$_cycles[$cycle] = 1;
        }

        return (self::isOdd(self::$_cycles[$cycle]++)) ? $odd : $even;
    }
}


for featues like this


    ">


is not overcoded

PS

Easy of usage (by html/css coders) is more important then performance in this particular case

UPDATE:

As @Geoffrey mentioned i didn't tell before that same 'oddCss', 'evenCss' pair may be used several times on one page (so basically within one request)

Solution

Since this is a code review site, let me point out that the following is an abomination:

return (0 == ($v % 2)) ? false: true;


The constants are redundant. Much simpler:

return (0 != ($v % 2));


(The parentheses are also redundant but that’s unrelated.)

Code Snippets

return (0 == ($v % 2)) ? false: true;
return (0 != ($v % 2));

Context

StackExchange Code Review Q#1452, answer score: 5

Revisions (0)

No revisions yet.