January 9, 2008

...Learn TDD with Codemanship

Crap4J Contains Long Methods

My ertswhile brother sent me a link to a place where you can browse the code for a tool called Crap4j. He flagged up some examples of - well - crap in the crap4j code.

Take this method from a class called - with absolutely no sense of irony - SystemCrapStats:

-------------------------------------------
203 public String toXml() {
204 MyStringBuilder s = new MyStringBuilder();
205 s.start("");
206 crapProject.toXml(s);
207 s.start("");
208 XmlUtil.itemToXml(s, "name", name);
209 NumberFormat nf = FormatUtil.getNumberFormatter();
210 XmlUtil.itemToXml(s, "totalCrap", nf.format(total));
211 XmlUtil.itemToXml(s, "crap", nf.format(crapNumber));
212 XmlUtil.itemToXml(s, "median", nf.format(median));
213 XmlUtil.itemToXml(s, "average", nf.format(average));
214 XmlUtil.itemToXml(s, "stdDev", nf.format(stdDev));
215 XmlUtil.itemToXml(s, "methodCount", Integer.toString(methodCount));
216 XmlUtil.itemToXml(s, "crapMethodCount", Integer.toString(crapMethodCount));
217 XmlUtil.itemToXml(s, "crapMethodPercent", nf.format(crapMethodPercent()));
218 XmlUtil.itemToXml(s, "crapLoad", Integer.toString(crapWorkLoad));
219 XmlUtil.itemToXml(s, "crapThreshold", Integer.toString((int) crapThreshold));
220 XmlUtil.itemToXml(s, "globalAverage", nf.format(globalStats.getCrapAverage()));
221 XmlUtil.itemToXml(s, "globalCraploadAverage", nf.format(globalStats.getCrapLoadAverage()));
222 XmlUtil.itemToXml(s, "globalCrapMethodAverage", nf.format(globalStats.getCrapMethodAverage()));
223 XmlUtil.itemToXml(s, "globalTotalMethodAverage", nf.format(globalStats.getTotalMethodAverage()));
224 XmlUtil.itemToXml(s, "globalAverageDiff", nf.format(globalStats.getCrapAverageDiff(crapNumber)));
225 XmlUtil.itemToXml(s, "globalCraploadAverageDiff", nf.format(globalStats.getCrapLoadAverageDiff(crapWorkLoad)));
226 XmlUtil.itemToXml(s, "globalCrapMethodAverageDiff", nf.format(globalStats.getCrapMethodAverageDiff(crapMethodCount)));
227 XmlUtil.itemToXml(s, "globalTotalMethodAverageDiff", nf.format(globalStats.getTotalMethodAverageDiff(methodCount)));
228 String projectName = URLEncoder.encode(crapProject.getProjectName());
229 int ones = crapLessThan(2.0f);
230 int twos = (crapBetween(2.0f, 4.0f));
231 int fours = (crapBetween(4.0f, 8.0f));
232 int eights = (crapBetween(8.0f, 16.0f));
233 int sixteens = (crapBetween(16.0f, 32.0f));
234 int thirtytwos = (crapBetween(32.0f, 64.0f));
235 int sixtyfours = (crapBetween(64.0f, 128.0f));
236 int one28s = (crapBetween(128.0f, 256.0f));
237 int two56s = (crapGE(256.0f));
238
239 String url = server+"stats/new?stat[project_hash]="+crapProject.getProjectId().toString()+
240 "&stat[project_url]="+projectName +
241 "&stat[crap]="+URLEncoder.encode(FormatUtil.getUSNumberFormatter().format(crapMethodPercent()))+
242 "&stat[crap_load]="+Integer.toString(crapWorkLoad)+
243 "&stat[crap_methods]="+Integer.toString(crapMethodCount)+
244 "&stat[total_methods]="+Integer.toString(methodCount)+
245 "&stat[ones]="+Integer.toString(ones)+
246 "&stat[twos]="+Integer.toString(twos)+
247 "&stat[fours]="+Integer.toString(fours)+
248 "&stat[eights]="+Integer.toString(eights)+
249 "&stat[sixteens]="+Integer.toString(sixteens)+
250 "&stat[thirtytwos]="+Integer.toString(thirtytwos)+
251 "&stat[sixtyfours]="+Integer.toString(sixtyfours)+
252 "&stat[one28s]="+Integer.toString(one28s)+
253 "&stat[two56s]="+Integer.toString(two56s)
254 ;
255 XmlUtil.itemToXml(s, "shareStatsUrl", url);
256 s.start("");
257
258 int[] places = {ones, twos, fours, eights, sixteens, thirtytwos, sixtyfours, one28s, two56s};
259 Arrays.sort(places);
260 int largest = places[places.length-1];
261 float scale = 170.0f / (float)largest;
262 int minHeight = 20;
263 makeHist(s, "one", Integer.toString(ones), nf.format(adjustedHeight(ones, scale)));
264 makeHist(s, "two", Integer.toString(twos), nf.format(adjustedHeight(twos, scale)));
265 makeHist(s, "four", Integer.toString(fours), nf.format(adjustedHeight(fours, scale)));
266 makeHist(s, "eight", Integer.toString(eights), nf.format(adjustedHeight(eights, scale)));
267 makeHist(s, "sixteen", Integer.toString(sixteens), nf.format(adjustedHeight(sixteens, scale)));
268 makeHist(s, "thirtytwo", Integer.toString(thirtytwos), nf.format(adjustedHeight(thirtytwos, scale)));
269 makeHist(s, "sixtyfour", Integer.toString(sixtyfours), nf.format(adjustedHeight(sixtyfours, scale)));
270 makeHist(s, "one28", Integer.toString(one28s), nf.format(adjustedHeight(one28s, scale)));
271 makeHist(s, "two56", Integer.toString(two56s), nf.format(adjustedHeight(two56s, scale)));
272 s.end("
");
273 s.end("
");
274 s.start("");
275 List crapValues = getSubjects();
276 Collections.sort(crapValues, Crap.comparator);
277 MethodCrap.setCrapLoadThreshold(crapThreshold);
278 for (Crap crap : crapValues) {
279 crap.toXml(s);
280 }
281 s.end("
");
282 s.end("
");
283 return s.toString();
284 }
--------------------------------------------------------


Can I take it that Crap4J doesn't test for long methods, then? Is this further evidence to support the theory that you get what you measure (or, in this case, you don't get what you don't measure)?


Posted 12 years, 10 months ago on January 9, 2008