Possibly all this code is redundant. If it's options, that's much better handled data-driven, where all processing refers to a data structure with option name, option type, default value, current value, possible activator function etc that's looped over rather than inline.
That is assuming this is one to one map, if set has some members that are not to be used in this logical block, you are stuck with skipping hardcoded exceptions of the general rule. At least this is straight forward and explicit, loops with skips is going to be even worse.
The set variable should only contain the values you want to update. That might be how it already works, but a lot of people confuse null and undefined.
To me this looks like imported options from an external source, possibly a database. In that case, this is probably the import function, constructing exactly that central data structure.
They could probably simplify this with a loop, it's not great but I can see how this happened, they probably started with one, then another and as time went on they just kept appending them.
Honestly a loop might have a higher cognitive load, especially if there's any logic or exceptions that need to happen. It would just take up less space and remove redundancy. So, it depends on what you mean by "simplify" ;)
As this is a config, there shouldn't be much reason for someone to revisit this code once it's set up.
It also means the config is controlled by the config file, where as right now it's controlled by a config file and this file by the looks of it. You're one spelling mistake away from confusion.
Is this thing right here a config? It looks like it could be a coupling between two different segments of an application, and this is how they're mapping from one internal representation to another. If this right here is just configuration, why are they even copying the values? Of course that could actually be what's happening lol
In my old company we had an api.is file which was a 4000 line switch case nightmare. It all starts simple and then others keeping piling on until one guy gets really pissed off and refactors it.
And if that wouldn't produce ugly code somewhere else. CamelCase is the common way to write variables in (JavaScript) source code. But for databases, you usually use SCREAMING_SNAKE_CASE. So if you import settings from a db to a js-application, you either get have inconsistent variable naming in one of them or need to convert the names.
And that would result in fewer lines of code... how?
You can certainly use a map of some kind where the names of the properties are the keys, but that absolutely has a performance impact compared to the hard-coded values.
And if this is already widely used, good luck making that change now.
Furthermore, it requires a tight coupling between two different data structures, and I would argue if you're going this route, everything has to use the same setting map.
And then you'll have potential bugs where someone typo'd the name of the setting, and since that's now a run-time check instead of a compile-time one, who knows when it'll get caught, if at all, and what kind of damage will occur.
It's JS - there is only runtime, no "compile time". And since this is JS, you can treat everything as a map - set.OBSTACLE is the same as set['OBSTACLE'] - so a for loop can easily be written in a few lines of code to do the same thing as this code.
Mostly I'm confused by all-caps stuff, which I infer to be constants, being null-checked. Why are these constants maybe null? So yeah, just a naming thing that's misleading.
I would prefer not adding a library for such a menial thing, and this could be a specific set of keys, not every single key in the object, and if the need ever arises to do something special for one, the loop starts getting conditionals inside of it, so I’ll much prefer the original way, way simpler although much longer
Imagine you wanna make changes to these variables. You'd have to go through and change this list every time. How many other places would this happen. What if one person doesn't know this even happens and it causes settings to be missed. No thanks I'll take the dynamic approach unless there's a reason not to.
If a person doesn’t even know this happens how could making it dynamic change that? If anything this makes it easier to see cause it’s huge, the dynamic version could be much smaller
You are correct. some also go into other properties and some have other logic. I couldn't be bothered going through it all, my attention span isn't long enough. I think that's the point of this post.
Since this is well indented, I found it pretty easy to scan through the lines and find differences. But I have worked with similar code. I probably am just used to it. Stuff like this is pretty common in larger web-based projects with interfaces to different systems.
The way I'd do it is to have an array of properties, and each one has a settingsKey, a setKey (which is confusing since "set" in this case is a noun, not a verb), and whatever extra attributes are required, like the special behavior for BROADCAST_MESSAGE. Then it's trivial to just iterate over the array.
This is JS. With some refactoring (making the property names the same) and the assumption that the null checks are actually undefined checks, you can just this.settings = { ... this.settings, ...set } You can also correct set like you said.
Honestly, while this snippet is shitty and verbose, and apt to be a target for a refactor - because duplication like this invites copypaste bugs - it is at least readable, and I can grok the intent at a glance.
The code suggests this is for a game, and games in general require large amounts of optimization. Are there any speed or memory benefits for doing it this way vs a map and a loop, or is it just a micro-optimization at best?
119
u/trollsmurf Jan 14 '22
Possibly all this code is redundant. If it's options, that's much better handled data-driven, where all processing refers to a data structure with option name, option type, default value, current value, possible activator function etc that's looped over rather than inline.
75
u/thebritisharecome Jan 14 '22
They could probably simplify this with a loop, it's not great but I can see how this happened, they probably started with one, then another and as time went on they just kept appending them.
114
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 14 '22 edited Jan 14 '22
Image Transcription: Code
[The rest of the code is cut off in the image.]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
46
u/orangeoliviero Jan 14 '22
While ugly as sin, this actually makes a lot of sense.
All of the "better" solutions involve a lot of boilerplate and abstractions, and the invention of a DSL.
10
u/Haunting-Surprise-21 Jan 15 '22
Some variables (e.g. this.motionType) are not part of this.settings.
There is at least one variable (this.settings.broadcastMessage) that needs special care.
There might be elements of set that should not be read.
The set object may be read from an untrusted source. Running a loop that reads everything would be a security issue.
As long as this is run in a separate function, this may actually be best practice.
1
u/Da-Blue-Guy Mar 08 '22
I completely forgot about this post lmao. This is in the arras.io Private Server Template.
16
u/Willinton06 Jan 14 '22
I think this is the most readable way of doing this, all other ways require not so readable loops and are very inflexible
4
u/recycle4science Jan 14 '22
Mostly I'm confused by all-caps stuff, which I infer to be constants, being null-checked. Why are these constants maybe null? So yeah, just a naming thing that's misleading.
7
u/Fireball_Flareblitz Jan 15 '22
Who stole Yandere Dev's source code?
4
u/pyker404 Jan 15 '22
is this Yandere Simulator code?
2
u/Alicialouva Jan 16 '22
When Yandere Simulator gets ported to the web
2
u/Ikeoa Jan 14 '22 edited Jan 14 '22
for(let key of Object.keys(set)){ if(set[key] != null) this.setting[toCamelCase(key)] = set[key] }
Lol
1
u/TheBotFromReddit974 Jan 15 '22
...
1
u/xiipaoc Jan 15 '22
Well, that's a maintenance nightmare.
The way I'd do it is to have an array of properties, and each one has a
settingsKey, asetKey(which is confusing since "set" in this case is a noun, not a verb), and whatever extra attributes are required, like the special behavior forBROADCAST_MESSAGE. Then it's trivial to just iterate over the array.1
u/annoyed_freelancer Jan 15 '22
Honestly, while this snippet is shitty and verbose, and apt to be a target for a refactor - because duplication like this invites copypaste bugs - it is at least readable, and I can grok the intent at a glance.
1
u/interpolate1 Jan 15 '22
Unmarshal config file into a struct with pointers.
1
u/babalaban Jan 15 '22
Is this yandere dev code (before being transpiled into C# to be used with Unity)`?
1
u/Objective-Cat-9608 Nov 24 '22
I looks like spaghetti but to be fair it’s JS this might be better written in another language
1
u/RoastmasterBus Jan 15 '22
The code suggests this is for a game, and games in general require large amounts of optimization. Are there any speed or memory benefits for doing it this way vs a map and a loop, or is it just a micro-optimization at best?