| Conditions | 10 |
| Paths | 26 |
| Total Lines | 45 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 27 | function kmeans($data, $nclusters) |
||
|
|
|||
| 28 | { |
||
| 29 | $resp = []; |
||
| 30 | $finished = false; |
||
| 31 | $npoints = count($data); |
||
| 32 | if (count($npoints) <= 0) throw new Exception("Not enough data. "); |
||
| 33 | $ndimensions = count($npoints[0]); |
||
| 34 | $centroids = initCentroids($nclusters, $ndimensions, function(){return rand(0,100)/100;}); |
||
| 35 | while (!$finished) { |
||
| 36 | |||
| 37 | // Assign each one of the points to one centroid |
||
| 38 | $nresp = []; |
||
| 39 | for ($j = 0; $j < $npoints; $j++) { |
||
| 40 | $best = -1; |
||
| 41 | $bdist = INF; |
||
| 42 | for ($i = 0; $i < $nclusters; $i++) { |
||
| 43 | $ndist = eclideanDistance($npoints[$j], $nclusters[$i]); |
||
| 44 | if($bdist > $ndist) { |
||
| 45 | $bdist = $ndist; |
||
| 46 | $best = $i; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | $nresp[] = $best; |
||
| 50 | } |
||
| 51 | |||
| 52 | // Check change |
||
| 53 | if(count($resp)!=0) { |
||
| 54 | $finished = true; |
||
| 55 | for ($j = 0; $j < $npoints; $j++) { |
||
| 56 | if($resp[$j]!==$nresp[$j]){ |
||
| 57 | $finished = false; |
||
| 58 | break; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | $resp = $nresp; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** Recalculate the centroids*/ |
||
| 65 | $centroids = initCentroids($nclusters, $ndimensions, function(){return 0;}); |
||
| 66 | $counts = array_fill(0, $nclusters, 0); |
||
| 67 | for ($j = 0; $j < $npoints; $j++) { |
||
| 68 | sumCentroid($centroids[$resp[$j]], $data[$j]); |
||
| 69 | $counts[$resp[$j]]++; |
||
| 70 | } |
||
| 71 | normalizeCentroids($centroids, $counts); |
||
| 72 | } |
||
| 104 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.