This page shows the source for this entry, with WebCore formatting language tags and attributes highlighted.

Title

Junior code is insidious

Description

<img attachment="highlightanythingthatseemsstupid.webp" align="right">The article <a href="https://thedailywtf.com/articles/enumerated-science" author="Remy Porter" source="Daily WTF">Enumerated Science</a> describes a train wreck of a code example. It suitably illustrates why we really have to question whether scientists/juniors/etc. should really be writing code with so little training. If they wrote text this poorly, they'd be laughed out of their profession. Somehow, it's perfectly fine to write code like this. <bq><code>index = 0 for index, fname in enumerate(img_list): data = np.load(img_list[index]) img = data[0][:,:] img_title 'img'+str(index).zfill(4)+'.jpg' cv2. imwrite(img_title, img) index = index + 1</code></bq> The article points out all of the mistakes but I'll summarize them here. <ul> Why does the code ignore the iteration item declared in <c>fname</c>? Instead, the code re-indexes into the array being iterated with <c>img_list[index]</c>. Like, why bro? You already had it! You know what <c>img_list[index]</c> is? It's <c>fname</c>, bro. Why does the code bother calculating a complicated new filename that has nothing to do with the original filename? Why is the filename called <c>img_title</c>? It's not a title; it's a filename. Why does the code increment the <c>index</c>? It has no effect, does it? Or is it possible that this algorithm skips every other item? Honestly, why does Python even allow modification of the iterator variables? They should be <c>const</c>/immutable exactly so you can avoid doing something distracting like this. </ul>