Junior code is insidious
Published by marco on
The article Enumerated Science by Remy Porter (Daily WTF) 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.
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
The article points out all of the mistakes but I’ll summarize them here.
- Why does the code ignore the iteration item declared in
fname
? Instead, the code re-indexes into the array being iterated withimg_list[index]
. Like, why bro? You already had it! You know whatimg_list[index]
is? It’sfname
, 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
img_title
? It’s not a title; it’s a filename. - Why does the code increment the
index
? 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 beconst
/immutable exactly so you can avoid doing something distracting like this.