Today we have a true confession from Carl W. It has the benefit of being in R, which means that at a glance, I just assume "eh, looks fine to me." I guess I'm turning into Jimbo.
But let's dig into it. Carl's first snippet is this:
for(kk in 1:length(thematch)) {
if(length(thematch)==0) break
# actual code
}
"What WAS I thinking?" Carl asks.
You were thinking two things: you were thinking you wanted to iterate across a list, and you were thinking you didn't want to do anything if the list was empty. That these wires got crossed is natural- but sure, this isn't something that should end up in production code. The WTF is less the code itself, and more whatever allowed it to get released.
Carl's next snippet is described as "Yes, let's invert logicals as many times as possible."
slice <-rep(0,length(newxx))
slicelog<-as.logical(cuts[j]<=newyy & newyy<=cuts[j+1])
is.na(slice)<-!slicelog
Because R is weird, <- is one of the assignment operators (it also uses =, but for different purposes, and don't ask me to explain the difference), and because people like to have fun, it also has an -> operator (so the assignee is on the right side of the expression), and <<- and ->> versions which muck with scope in interesting ways.
In this example, the rep function replicates a value (0, in this case) length(newxx) times. So that gives us an array of zeros.
R supports broadcasting operations, so cuts[j] is an array and newyy is also an array (vector, if we're using R parlance), and by comparing two arrays we get an array of boolean values. Which we then pass to as.logical which converts an array into an array of boolean values.
Finally, in R, the NA value is their version of null. Thus is.na returns an array which is true if the original input held a null at that index, and false otherwise. Which is definitely going to be an array of falses, because slice holds an array of 0s. We just made it. Then we assign to the return value of that function. This is syntactically valid, but as you can imagine, it doesn't actually make sense- we just discard the result, at least based on me trying this in an R REPL.
Carl writes:
While the function containing these snippets was definitely a victim of "code it and design the requirements later," I can't believe that even Beginner Me wrote stuff that horrifying. But, it works perfectly so it ain't gonna get fixed.
Ah, that last sentence there, that is a dark truth. And it's important to note, it works perfectly: for now. But the world and runtime environment change. And someday, another developer will receive this code, and wonder, much like Carl, what the hell was going on when it was written.
In any case, Carl, consider yourself absolved. If it's stupid and it works, well, it's still stupid, but these WTFs are small and mostly self-contained.
Remember: code is a liability and starts accruing cruft and tech-debt the instant it's released. The functionality that code delivers is the asset, but that asset is inextricably tied to the code, which encodes assumptions about the world which, even if they were true to begin with, become increasingly false as the world changes.
Also, if you haven't figured out the headline, and because jokes are funnier when you explain them, this is clearly about a pirate's second favorite programming language, ARRRR. Why their second favorite? Because their first love will always be the C.