@vaporeon_ software eggineering poisons your brain
@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 She is cool and I admire her Python code golfing skills
@vaporeon_ it said she is cool in many ways
@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]);
}
};
@wallhackio I love how my Mastodon posting has made you use gorp
, norp
, and sorp
in actual code
@wallhackio @vaporeon_ is that single member array of arrays not a typo lol
@aescling @vaporeon_ nope :)
@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
@aescling @vaporeon_ oh this was an inefficient implementation of Dutch National Flag too, lol
@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.
@vaporeon_ here is another hack with a "beautiful" one liner:
Problem Statement
You are given a 0-indexed array of integers nums
of length n
. You are initially positioned at nums[0]
.
Each element nums[i]
represents the maximum length of a forward jump from index i
. In other words, if you are at nums[i]
, you can jump to any nums[i + j]
where:
0 <= j <= nums[i]
andi + j < n
Return the minimum number of jumps to reach nums[n - 1]
. The test cases are generated such that you can reach nums[n - 1]
.
Example
[2,3,1,1,4]
2
The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
Code
const jump = (nums, i = 0, gorp = {}) => {
if (i === nums.length - 1) return 0;
if (i > nums.length - 1 || nums[i] === 0) return Infinity;
if (gorp[i] !== undefined) return gorp[i];
let min = Infinity;
for (let j = 1, res; j <= nums[i]; ++j) res = 1 + jump(nums, i + j, gorp), min = res < min ? res : min;
return gorp[i] = min;
};
@wallhackio @vaporeon_ i think you omitted a closing parenthesis?
@aescling @vaporeon_ I just ran it and what is here is correct
@wallhackio @vaporeon_ oh i can't read, nvm
@vaporeon_ oooooh I could have done for (var min = Infinity, j = 1, res; j <= nums[i]; ++j) res = 1 + jump(nums, i + j, gorp), min = res < min ? res : min;
javascript variables declared with var and not block scoped, so min
is available outside of the for loop teehee
@wallhackio @vaporeon_ that's awful i love it lol
@aescling @vaporeon_ ....it runs faster with this change
@wallhackio @vaporeon_ if you look into the semantic diffurences between for
with let
and var
scoped variables, this is not surpurrising; the furmer is surpurrisingly complicated. const
allows fur some optimizations over let
though iirc
@aescling @vaporeon_ with let
the algo ran the test cases ~700ms and with var
it shaved ~100ms
@wallhackio Your work has you write both Java and JavaScript?
@vaporeon_ full-stack software eggineer
@wallhackio Your company's server backend is written in Java?
@vaporeon_ I am currently not working there, I was laid off in June, but yes it was
This is actually extremely normal since Java was extremely popular during the dot.com boom of the late 90's/early 2000's and the most appealing alternative was C++, so obviously people used Java instead
An enormous amount of critical codebases run on Java
@wallhackio 🎵 3 billion critical codebases run on Java!! 🎵
@vaporeon_ and because OOP was all the rage in that time period (it wasn't around ~2010 that the software engineering world was like, maybe we shouldn't solve everything with OOP)
@wallhackio I thought PHP was the typical language for writing web server backends?
@wallhackio <- has never written a web server backend
@vaporeon_ I find it extremely Uninteresting
@wallhackio Does this imply that you find Java to some extent Interesting?
@vaporeon_ once I use Java enough and get in the Java Zone I don't actually hate the language itself, but more the standard library and the excessive OOP people who use Java keep falling into (although I suppose it's fair to criticize a language if it incentivizes people to write bad code)
@vaporeon_ the Java community also has a bad habit of what I'll call "Premature Generalization" where people where make a generic framework for something that is only used for a specific use case, making the API for it much more complicated than you want
You don't really see people using Spring for anything other than RESTful backends, and yet Spring is a generic application tool for using dependency injection in Java applications. It's so tedious to learn
@vaporeon_ this plagued our backend, where everything was some subclass of an abstract class which implemented an interface and there was only one subclass which actually implemented the interface. Just make it one fucking class!!!!!!
@vaporeon_ Maybe that was true at one time, but it would be unusual today
@wallhackio I once talked to a computer toucher and they told me that their backend was also written in JavaScript and I thought that's incredibly cursed
@vaporeon_ it's actually not abnormal lol
@vaporeon_ I love JavaScript so I have mixed feelings about it
@vaporeon_ you'd be stunned by the performance that JIT engines can achieve these days, they can get stuff that is close-ish to Java sometimes
Most people who do it write the codebase in TypeScript, which is a language that looks a hell of a lot like JS but it strictly typed, and is compiled to JavaScript (I have extremely mixed feelings about TypeScript)
There is just a huge cost to running a server with JS though (either you use a JIT engine which has a significant memory cost or you don't and get a huge performance cost), which makes me hard-pressed to ever consider it for a web server
@wallhackio @vaporeon_ idk about “unusual”; there is a lot of legacy code based on php. notably, wordpress
we have php code at the library somewhere (i had to live-analyze a bit of php code fur my interview), but i haven’t done any php work yet somehow
@aescling @vaporeon_ ah fuck I hate spreading misinformation on the internet
@wallhackio Are you referring to yourself or to me or to us all?