r/badcode • u/NotSteve_ evil • Jan 14 '21
js Dynamically created functions are the future!
20
Jan 14 '21
Probably fun to write code like this. A nightmare if you inherit it to maintain.
17
u/NotSteve_ evil Jan 14 '21
It's so bad. The previous developers are all long gone too since they almost bankrupted the company and got themselves laid off in the process. There's no documentation either and the codebase is massive. Now I'm in charge of keeping it running. At least the pay is good...
12
7
u/Tomekske Jan 14 '21
Please, stay on full stack. It should be forbidden for you to program a microcontroller for humanity's sake
17
13
u/Niosus Jan 15 '21
Hahahhaahaha. I've seen and worked on code that ships in cars. Thousands upon thousands upon thousands of lines of C code. Not a unit test in sight. Functions being executed multiple times in a row as workarounds for race conditions. "Tactical" void pointers. And of course, it wouldn't be a proper C program without storing all the state in a global context.
Luckily that code was only used for the infotainment system. But honestly, I don't think the safety critical code is going to be that much better. The only reason we managed to ship relatively bug-free code is because the QA team was really good at their job and found most of the bad stuff before it reached the customers...
Humanity is screwed.
6
5
u/GuinnessDraught Jan 15 '21
Same, but airplanes.
2
u/pwnedgiraffe Jan 15 '21
Dude I am scared of flying as it is, I don’t want to know.
2
u/GuinnessDraught Jan 15 '21 edited Jan 15 '21
The saving grace is that aerospace still invests in massive QA operations and exhaustive testing. You can thank the FAA for that, though they have some serious egg on their face with the 737-MAX debacle. But it's a constant battle.
Unlike most other software shops that have just rolled QA into developer responsibilities and decimated QA departments.
Granted, they could probably save a boatload on QA if the developer quality was better instead of contracting and outsourcing so much of it. The result of massive QA of bad code is even worse code, but at least for known cases it is reliable to an outside observer.
4
u/b1ackcat Jan 15 '21
But honestly, I don't think the safety critical code is going to be that much better.
Also worked on code that shipped in cars. The safety critical stuff is definitely better than what you described. Mainly because every single bit and execution flow is accounted for. The systems are designed to work with a very specific set of constraints and they are absolutely purpose built for a given set of features and never anything more. And yes, QA qa's the everloving shit out of it too.
I know what you mean about infotainment systems, though. I worked with those. non software development engineers should not be writing that kind of code lol
189
95
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 14 '21
Image Transcription: Code
// this is the compile function that will become our dimension list;
let compileDimensionList = function (properties) {
let dimensionFunStr = '(function (d) {\n';
let dimString = '';
for (var i = 0, len = properties.length; i < len; i++) {
if (i === (len-1)) { // end of the list
dimString += 'd.'+properties[i];
} else { // every otther item
dimString += 'd.'+properties[i]+' + ';
}
}
dimensionFunStr += 'if (typeof timeLookup['+dimString+'] === "undefined") {\n';
dimensionFunStr += 'timeLookup['+dimString+'] = d.time;\n';
dimensionFunStr += '}\n';
dimensionFunStr += 'if (d.time > timeLookup['+dimString+']) {\n';
dimensionFunStr += 'timeLookup['+dimString+'] = d.time;\n';
dimensionFunStr += '} else {\n';
dimensionFunStr += 'd.time = timeLookup['+dimString+']\n';
dimensionFunStr += '}\n';
dimensionFunStr += 'return '+dimString+';\n';
dimensionFunStr += '})';
return eval(dimensionFunStr);
}
let dimensionList = compileDimensionList(groupby); // compile dimension functionsI'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
57
22
5
4
Jan 15 '21
[removed] — view removed comment
4
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 15 '21
Unfortunately, OCR isn't currently at a stage where it can be used for this purposes.
We actually have a bot that attempts to transcribe posts. It's pretty much the best OCR not behind an expensive paywall, and while it does mostly okay with simple posts that are basically just text, it's easily messed up by anything from different fonts to weird formatting.
One of the ones it has the biggest problem with is code like this. For some reason it just always messes up all of the formatting, and skips over many lines and characters. Some people use its attempts as a basis they correct, but I find to be quicker to just transcribe the post from scratch.
I agree; it would be great if an advancement could be made in the field, but currently, the technology's not at a stage where it can be putting out official transcriptions that haven't at least been vetted by a human, and in the case of code it is, as I've said, useless.
Hope that clears it up! If you have any further questions I can do my best to answer them!
4
4
u/Promant Jan 14 '21
C#'s source generators: let me please introduce ourselfs.
Jokes aside, code like this is actually pretty useful, but only if the compiler itself supports it (so it gets build once, not every time the method is called).
3
u/jarfil Jan 14 '21 edited Dec 02 '23
CENSORED
2
Jan 15 '21
Isn't this just template
Exactly. C++ devs around the world do that exact thing with templates since decades. With templates it looks a little bit nicer, but it actually is not 😅
2
u/dasbodmeister Jan 14 '21
Oof, when you haven't learned about upvalues yet
7
2
2
u/Martyn_X_86 Jan 15 '21
Ewww, it reminds me of dynamic SQL, which when done correctly can be powerful, but most examples I've seen in the wild are at best very difficult to maintain, and at worst, are a SQL injection attack waiting to happen.
2
1
1
1
1
1
Jan 15 '21
Could the function just be defined, and dimString passed as an argument? Well, I guess dimString is a local value, so maybe not. At the very least, you could define a separate function that takes a string parameter, and then use this same method to call the function in a string and use dimString as the parameter. Or maybe there's a really easy solution, and I'm just overthinking it
1
u/Unpredictabru Jan 17 '21
You could do something like this:
function compileDimensionList(properties) { let dimString = ...; return function () { // you can access dimString here ... } }1
1
1
u/MegaIng Jan 15 '21
While the exact implementation might need a bit of an improvement (e.g. templates, sanitization, etc.). I really support the core idea. Python as examples uses it in multiple places in its stdlib (namedtuple, dataclass, and (maybe) enum).
1
1
1
u/Yserbius Jan 15 '21
This reminds me of the last time I had to debug a JSP page with embedded JS. bbrrrrr
1
u/Bowuigi06 Jan 15 '21
Actually with smilebasic you can read and write programs via code so this is possible lol
1
0
Jan 14 '21
[deleted]
6
4
u/egrgssdfgsarg Jan 15 '21 edited Jan 15 '21
I just tested this, it's only faster if your getClassProperties function is very slow. (In my test I made it some arbitrary function that enumerated all the keys. I had to add a sleep of 100ms to get the string compiled function to be faster.)
And there's a simple optimization for that. You call getClassProperties only once for a schema otherwise you look it up. Essentially, there's a better way to write it than both of the options in the post.
My version of the function (ends up being faster than both versions, under slow and fast circumstances for getClassProperties):
const valuesCache = schema.getClassProperties().values(); function simpleCached(last, current) { const changed = {}; for (const property of valuesCache ) { if (last[property.name] !== current[property.name]) { changed[property.name] = true; } } return changed; }There might be some specific circumstances it's faster to write a function with a string, but 99.99% of the time this is the wrong way to do something.
2
74
u/[deleted] Jan 14 '21
In Ruby you can do this in a nice and useful way.