@vaporeon_ software eggineering poisons your brain
@wallhackio Are you referring to yourself or to me or to us all?
@vaporeon_ I'm referring to anyone who writes code for money instead of writing code for fun
@vaporeon_ this is not a slight on Holly. i hope that is clear. she does code golf so you know she actually likes coding for the hell of it
@wallhackio @vaporeon_ oh yeah all this goes out the window when you're golfing. sometimes it's fun to let loose and write some horseshit
@monorail @vaporeon_ my worst code practices occur when i hack a solution to a challenging data structures/algorithms problem
@wallhackio How can you have bad code practices when there's no such thing as good Java code, it's all terrible
@vaporeon_ I've done DS&A problems in JavaScript and written some nightmarish oneliners doing that
@wallhackio Need to see
@vaporeon_ I know how this is gonna go. I will show it to you and then you'll be like, wait this is cool I like it :)
@vaporeon_ okay here's a bad one. i implemented a non-recursive quicksort using the variables gorp, norp, and sorp:
const partition = (arr, start, end, strategy) => {
const q = [[start, end]];
while (q.length > 0) {
const [left, right] = q.pop();
const p = strategy(arr, left, right);
const pVal = arr[p];
swap(arr, p, right);
let gorp = 0;
let norp = 0;
let sorp = 0;
const L = right - left + 1;
for (let i = 0; i < L - 1; ++i) {
const curr = left + gorp + sorp;
if (arr[curr] === pVal) {
swap(arr, left + sorp, curr);
++sorp;
} else if (arr[curr] > pVal) {
swap(arr, curr, right - norp - 1);
++norp;
} else {
++gorp;
}
}
for (let j = 0; j < gorp; ++j) {
swap(arr, left + j, left + sorp + j);
}
swap(arr, left + gorp + sorp, right);
if (gorp > 1) q.push([left, left + gorp - 1]);
if (norp > 1) q.push([left + gorp + sorp + 1, right]);
}
};
@aescling @vaporeon_ if you are wondering this is using the Dutch National Flag partition, which is the actual real name for it, and I simulated recursion using a stack (a JavaScript array has methods on it that allow it to easily resemble a stack)
I used the ninther strategy for the pivot selection but that was not present in this snippet
@wallhackio @aescling @vaporeon_ It's funny how "a JavaScript start has methods on it that allow it to easily resemble a stack" should immediately set off all kinds of red flags for anyone with basic algorithms knowledge, *and yet* because developers regularly use them like that, most JavaScript engines have developed to make those operations fast and otherwise efficient.
@aschmitz @aescling @vaporeon_ I once used a JavaScript array as a queue, using the shift
method to pluck elements from the start of the array. Of course, this means iterating over an entire "queue" should be slow, since every call to shift
causes every element in the array to be reassigned and reduces the size of the array by 1. I benchmarked my algorithm and it was slower than I hoped, so I immediately worked on the queue as I thought it was a throttle for the algorithm.
I made an object that effectively had a JavaScript array as a private variable and created a method for "removing" the first element from the array. In reality, there was pointer being tracked by the object that simply shifted to the right by 1, simulating the behavior of a queue with an ordinary array. Obviously iterating over this structure should be O(n).
To my surprise, when I benchmarked the algorithm again, it was slower. Evidently, with my first approach, the JIT engine realized I was using an array as a queue and secretly optimized it for me. Without realizing, I tried to outsmart the JIT engine, and lost.
@aescling @vaporeon_ oh this was an inefficient implementation of Dutch National Flag too, lol