Doc #63204 [NEW]: Unreadable example code, replacement enclosed
| From: | henry at henrysmith dot org | Date: | Wed, 03 Oct 2012 07:45:29 +0000 |
| Subject: | Doc #63204 [NEW]: Unreadable example code, replacement enclosed | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-8926@lists.php.net to get a copy of this message | ||
From: henry at henrysmith dot org
Operating system: N/A
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:Unreadable example code, replacement enclosed
Description:
------------
---
From manual page: http://www.php.net/function.array-udiff#refsect1-
function.array-
udiff-examples
---
This is my case for replacing this example code:
It uses a class when there is no need to, and then to make matters worse
the
class is
named "cr" with no explanation as to what this means. All-lower-case class
names
go
against prevailing PHP code standards too.
The class only has one single attribute, and it's poorly-named. A name like
"priv_member" tells us nothing that the bare syntax doesn't already tell
us.
The class' constructor is in the old "repeat the class name" style, and
accepts
another
badly-named parameter: $val. Again, every variable is a "value" so this
tells us
nothing about what's going on.
The static comparator method is called "comp_func_cr". Presumably this
means
"Compute
function CR". Again, we know it's a function from the syntax, we know it'll
compute
something because that's what functions *do*, and we know it's in the "cr"
class
because we're looking at it right now in the "cr" class.
Inside the comparator, usage of whitespace is so randomly varied as to be
confusing to
the reader, and thanks to all the long "priv_member" attributes it's
difficult
to see
what's actually being compared and returned.
The instantiation code that creates arrays of "cr" objects is arranged into
over-long
lines, and although it defines keys for the array elements, it is not
arranged
into a
sensible visual order from 0 to 2.
A proposed replacement is enclosed as the "test script". Its output is
enclosed
as the "actual result". The code executes the same and produces the same
output
as the existing example, but without any of the issues mentioned above.
Test script:
---------------
<?php
$one = array(
'0' => 23,
'0.1' => 9,
'0.5' => 12,
'1' => 4,
'2' => -15,
);
$two = array(
'0' => 3,
'0.2' => 9,
'0.5' => 22,
'1' => 4,
'2' => -15,
);
$result = array_udiff($one, $two, function($a, $b) {
if ($a === $b) {
return 0;
} elseif ($a > $b) {
return 1;
} else {
return -1;
}
});
print_r($result);
Actual result:
--------------
Array
(
[0] => 23
[0.5] => 12
)
--
Edit bug report at https://bugs.php.net/bug.php?id=63204&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=63204&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=63204&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=63204&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=63204&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=63204&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=63204&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=63204&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=63204&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=63204&r=support
Expected behavior: https://bugs.php.net/fix.php?id=63204&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=63204&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=63204&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=63204&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63204&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=63204&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=63204&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=63204&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63204&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=63204&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=63204&r=mysqlcfg