PHP Developers Network
http://forums.devnetwork.net/

Audit my Code - Data converstions?
http://forums.devnetwork.net/viewtopic.php?f=50&t=119473
Page 1 of 1

Author:  Daz [ Tue Aug 03, 2010 11:59 am ]
Post subject:  Audit my Code - Data converstions?

I was curious if there are other/better ways to code the following (it's a function to convert data sizes):

Syntax: [ Download ] [ Hide ]
    function convert_data( $number, $unit0, $unit1 )
    {   // Base unit is megabytes
        $d = array();
        $d['bit']                   = 1/(2^23);
        $d['b'] = $d['byte']        = 1/(2^20);
        $d['kbit'] = $d['kilobit']  = 1/(2^13);
        $d['kb'] = $d['kilobyte']   = 1/(2^10);
        $d['mbit'] = $d['megabit']  = 1/(2^3);
        $d['mb'] = $d['megabyte']   = 2^0;
        $d['gbit'] = $d['gigabit']  = 2^3;
        $d['gb'] = $d['gigabyte']   = 2^10;
        $d['tbit'] = $d['terabit']  = 2^13;
        $d['tb'] = $d['terabyte']   = 2^20;
        $d['pbit'] = $d['petabit']  = 2^23;
        $d['pb'] = $d['petabyte']   = 2^30;
        $d['ebit'] = $d['exabit']   = 2^33;
        $d['eb'] = $d['exabyte']    = 2^40;

        $t = (array_key_exists($unit0, $d) AND array_key_exists($unit0, $d))? ($number * $d[$unit0]) / $d[$unit1]: FALSE;
        unset($d);
        return $t;
    }


thanks!

Author:  josh [ Sun Aug 08, 2010 7:51 pm ]
Post subject:  Re: Audit my Code - Data converstions?

It isn't clear from the function's signature what the parameters are supposed to be for.

Imagine seeing this in your code complete
function convert_data( $number, $unit0, $unit1 )

Maybe its from and to? do I pass in some sort of integer? A string? Which is which? Am I even correct in my assumption. The code itself looks fine I didn't read it in detail. I could probably figure out how to use it if I read it more, but that means the code can be improved - so I think I've looked at it enough to offer my opinion. Looks decent. Could have more descriptive & obvious signature.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/