r/badcode • • Apr 09 '21

c# Yanderedev intensifies...

Post image
759 Upvotes

72 comments sorted by

150

u/[deleted] Apr 09 '21

We found it boys and girls and non binary pals. Yandare Devs' brain

17

u/Lost4468 Apr 09 '21 edited Apr 10 '21

How do we know this isn't just compiler optimised code? Or is this from the actual source code?

Edit: I think OP is right actually, I think the way Yanderedev wrote it prevented it being optimised out into anything else.

If we look it still checks every single month even after it finds a month... There's no else if branching, there's no break/continue/etc, there's no return. What I'm saying is I don't think it could have reasonably came from a switch statement, or an array or anything else.

Essentially by not returning or using else etc, the compiler is stuck, it thinks it has to do each one anyway, so I think this must be almost exactly how it was originally written? Also at OP /u/Antihihi

This doesn't mean the method is justified though, C# decompilation is still flawed for actual method bodies. But above the method body it actually should be very accurate.

10

u/Antihihi Apr 10 '21

This is a Unity game reversed, the code is almost 1:1 besides variable intialization

5

u/Lost4468 Apr 10 '21

Uhh I don't think so? I might be wrong though.

For .NET it wouldn't be the same. C# is compiled to the Common Intermediate Language (CIL), which is how .NET binaries such as executables and libraries (e.g. dll) are stored. Then this CIL is compiled by the Just In Time (JIT) compiler... well, just in time when it's ran.

So while it's not the aggressive type of compilation you might see with C++ or similar, there are still significant changes between the code and the CIL. Of course it is required to keep a lot more due to the way .NET works, and also to properly support reflection etc. But as I said it certainly is still different. And inside of a method? Yes it could very easily simplify a switch case to that, or maybe even an array to this depending on how it was written.

I don't know how Unity works in that regard, but according to this documentation, it uses the same compiler that .NET now uses, the open source Roslyn compiler. It also says it's equivalent to .NET 4.6.

Also when I modified KSP before, I believe I was able to just import binary files into Visual Studio natively and intellisense etc would import from them. So this must mean it also uses the CLI? Meaning it's almost certain it's just using the standard Roslyn compilation, which would mean this might be compiler optimisations?

1

u/Antihihi Apr 10 '21

I don't know much about it honestly from my own testing of making my own game in Unity and then trying to open it in dnSpy i didn't notice any differences

7

u/Lost4468 Apr 10 '21

Did you make sure to compile it as a release build? Because if you did it as a debug one then yeah everything will be 1:1 in order to speed up compilation and to enable breakpoints etc.

6

u/[deleted] Apr 10 '21

Nope, the compiler and decompiler makes lots of changes

3

u/Lost4468 Apr 10 '21

Yeah I didn't mention the decompiler either. That can make quite a bit of difference as well.

.NET decompilation is both accurate and inaccurate at the same time. All of the higher level structure of the program should be 100% there, that is anything above functions. I believe the only thing that is totally changed is the actual code on the sub-function level.

So it can still be useful for telling if something is well programmed, but it's useless on the level of OP's information. E.g. if we look at Terraria (early versions anyway) we can see that the whole game is forced into several static classes that seem to hardcode everything in the game, there's very little basic structure, methods are huge and do a million things each, no inheritance, not even basic object orientated programming. Oh and I think everything is stored as a global variable if I remember correctly. But we can't actually make out how any of that is programmed (although you can still see just how long the methods are... and also they very often end up on indent level 12+ which I would imagine even the compiler/decompiler aren't going to generate).

Even private functions/fields/properties etc I believe have to be kept due to reflection?

So it would be interesting to look at Yandere Simulator from this point of view. A class diagram would be the easiest thing to generate, then also a call graph between these, etc. And looking at it in a bit more detail, I think we could get a bad code version of it that is accurate.

Also I just realised that the content in OP's image still goes through and checks everything after it has found a match? I would say that's actually an indication it was programmed similarly to this, at least in an equally as terrible way. I don't see how it could have been a switch or array before? Because there should be some sort of return or break, or even just else if branching logic if this was the compiler optimising it?

76

u/haikusbot Apr 09 '21

We found it boys and

Girls and non binary pals.

Yandare Devs' brain

- Ok-Parsnip9933


I detect haikus. And sometimes, successfully. Learn more about me.

Opt out of replies: "haikusbot opt out" | Delete my comment: "haikusbot delete"

1

u/rik079 Apr 10 '21

I see that name here all the time, who are they and what's wrong with them?

1

u/[deleted] Apr 11 '21

You'll understand soon enough

156

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 09 '21

Image Transcription: Code


if (this.ParseDataDontCutLastChar("JAN", j))
{
    copyProtectScript.date_month = 1;
}
if (this.ParseDataDontCutLastChar("FEB", j))
{
    copyProtectScript.date_month = 2;
}
if (this.ParseDataDontCutLastChar("MAR", j))
{
    copyProtectScript.date_month = 3;
}
if (this.ParseDataDontCutLastChar("APR", j))
{
    copyProtectScript.date_month = 4;
}
if (this.ParseDataDontCutLastChar("MAY", j))
{
    copyProtectScript.date_month = 5;
}
if (this.ParseDataDontCutLastChar("JUN", j))
{
    copyProtectScript.date_month = 6;
}
if (this.ParseDataDontCutLastChar("JUL", j))
{
    copyProtectScript.date_month = 7;
}
if (this.ParseDataDontCutLastChar("AUG", j))
{
    copyProtectScript.date_month = 8;
}
if (this.ParseDataDontCutLastChar("SEP", j))
{
    copyProtectScript.date_month = 9;
}
if (this.ParseDataDontCutLastChar("OCT", j))
{
    copyProtectScript.date_month = 10;
}
if (this.ParseDataDontCutLastChar("NOV", j))
{
    copyProtectScript.date_month = 11;
}
if (this.ParseDataDontCutLastChar("DEC", j))
{
    copyProtectScript.date_month = 12;
}

I'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!

86

u/Darth_Noox Apr 09 '21

Good human

5

u/[deleted] Apr 10 '21

Good human

144

u/Lost4468 Apr 09 '21 edited Apr 10 '21

How do we know this isn't compiler optimisation? Or does this come from actual source code?

Also Yanderedev's problem isn't really the bad code. It's his attitude. Dude refuses/refused to take any criticism and admit he didn't know what he was doing. Had he just admitted he was out of his depth he could have easily fixed it before going too far with the crappy code base. Not to mention he seemingly has done nothing for multiple years.

Terraria was programmed terribly as well. In fact I would say it was significantly worse than Yanderedev's code. Thankfully the dev was never hit too hard by performance issues due to it being in 2D (and also spaghetti code can be fast sometimes) and not requiring a ton of computing power. That dev admitted the fault though and went back and fixed it I believe, not to mention all the other things he added. He didn't just sit there saying what an amazing dev he was.

I have interviewed people like that. I give them a problem and they're obviously stuck, but they would rather sit there for 20 minutes. And then when I finally interrupt them they always blame it on anything but themselves. If you seriously don't get it and know you've gone way past the allotted time, ask for help, of course employers would rather employ the person who can solve it, but they certainly wouldn't hire the person who just sat there refusing help, over someone who got stuck and asked.

Edit: I think OP is right actually, I think the way Yanderedev wrote it prevented it being optimised out into anything else.

If we look it still checks every single month even after it finds a month... There's no else if branching, there's no break/continue/etc, there's no return. What I'm saying is I don't think it could have reasonably came from a switch statement, or an array or anything else.

Essentially by not returning or using else etc, the compiler is stuck, it thinks it has to do each one anyway, so I think this must be almost exactly how it was originally written?

This doesn't mean the method is justified though, C# decompilation is still flawed for actual method bodies. But above the method body it actually should be very accurate, because the compiler has to preserve all of that. I believe it has to even preserve all private method/field/property names as well for reflection and similar purposes?

94

u/Shadow_Being Apr 09 '21

gonna give you a secret. all code is bad code. it's all shitpiles on top of shitpiles taped together with duct tape.

It's not a matter of if your code is bad. it's a matter of how bad is your code.

26

u/Lost4468 Apr 09 '21

Well I would say that means we still have good and bad code, it's just there's a lot of room to improve. Or maybe there isn't, maybe this is it, we've already reached peak code quality. But I would say that good code is 99% art and 1% engineering.

It's one of the criticisms I've seen of software engineering in general, and why some people argue it's not engineering. I don't agree with them at all on it not being engineering, but I can certainly see why they say parts are a mess.

Civil engineering has figured out objective ways to measure things, and has developed methods and stuck with them for a long time. We know a bridge won't (shouldn't) fall down because we know enough about the physics, we have objective ways to design it and test it and make sure it will not fall down, before we build it. These are generally all agreed upon and rarely change. And the best methods work amazingly well and have so few issues.

But in software engineering there's really nothing like that. There's very few objective measures of code, it's nearly all subjective. And there's very little widespread agreement, and the trends seem to change every few years. And there's really no method that works well outside of both very specific circumstances and in a vacuum. We have no methods which can guarantee anything will work and act like we want it to, it always inevitably ends up interacting in some really complex way we couldn't predict beforehand.

There are some obvious exceptions, like validate your fucking input so we don't have another Therac-25. But as far as the large scale design and behaviour of code, it's much more of an art than engineering. And it will be for the foreseeable future, because there are just way too many degrees of freedom. When engineering a bridge you're designing it to withstand forces which always behave in the same way, materials which always behave and interact in similar ways, etc. Whereas when developing software you literally create all of those rules yourself, and you can't even properly sort it out for your own code, nevermind when you start interacting with other peoples code.

6

u/TigreDeLosLlanos Apr 10 '21

It even runs in a much deeper level. When you want to use some system call or even a wrapper you are found with the "the standard doesn't specify what this thing should return or how it should do it" so you are left with non portable and potentially unpredictable code. The cause is often that the code came before than the standarization so they don't want to crap on some edge cases and forcing some people to redo their code because they used some things in a non-common sense way to save 0.1 ms execution time and 14kB of memory.

0

u/MrHelloBye Apr 10 '21

Therac-25?

4

u/Lost4468 Apr 10 '21

The Therac-25 was a medical machine used for X-ray based radiation therapy.

Looks like I have had it mixed up in my mind for a while though. The issue I was thinking of was a similar machine, and there were machines in both the France and the UK. France uses a , in the place of a . and a . in the place of a , e.g. so 1,000,000.5005 in the UK but 1.000.000,5005 in France. Some patients were moving back and forth between France and the UK, so this would cause them to frequently have doctors from both countries communicate in what doses etc they have given the patient.

So the problem was the doctors were sending each other numbers with , and . in them back and forth, e.g. a UK doctor might prescribe a dose of 1.115, but then in France this would look like 1,115 to someone there. These doses were obviously lethal/very dangerous, but the machine did no input validation at all. So someone in France would get the number, just quickly type in the number without thinking and deliver a dose 1,000 times as large to the patient.

Now it looks like the Therac-25 issue was different though, and more complicated. It essentially boiled down to a ton of mistakes, there's a good video here. It's rather complicated, but it essentially boiled down to hardware cut offs replaced with software cut offs which would sometimes not work, and even when they did could be easily overridden by the user with no explanation.

I can't find anything on the one I heard about. It was introduced to me in my software engineering class, and I'm beginning to wonder if it was one of those stories that had been passed around that mutated from the original, and this was actually the Therac-25 story, just incorrect?

2

u/[deleted] Apr 10 '21

there was a picture of yandere's code quite like this. It had "this." at the start of stuff

it wasn't decompiled, there was a comment in there

6

u/SandeonMNG Apr 09 '21

Has yanderedev never heard of a switch

3

u/Nilstrieb Apr 10 '21

this can actually not be a switch like this.
also in C# string switches don't exist behind the scenes, so it gets converted to else if chains.
also a switch would only make this code slightly less terrible

1

u/Antihihi Apr 10 '21

Actually it's kinda my fault for this but this is not Yandere Simulator, this is a different game made in Unity, I don't want to tell the games name as i actually enjoy it.

1

u/digitbh Apr 24 '21

I love how every reply to yanderedev is "hurr durr, try switch!!" As if that's gonna solve any of the gaping problems

0

u/[deleted] Apr 09 '21

[deleted]

2

u/Sevigor Apr 10 '21

You could always use an enum too

23

u/NetherFX Apr 09 '21

I'm curious, let's say there's no way to use Date, what'd be a better way than using a Dictionary, or HashMap?

38

u/PolishedCheese Apr 09 '21

Definitely dictionary or hashmap comprehension. Return mapDict[a]

5

u/[deleted] Apr 09 '21

Yup! Map them!

-3

u/Shadow_Being Apr 09 '21

based on this code alone. that is literally no different in approach.

Somewhere you have to type out the month string and the numbers in a long list of code.

24

u/dpelego Apr 09 '21

Big difference in readability between 12 if statements and initializing a hashmap with 12 values.

If you really want, you could have an array with all of the values and then a for loop to populate the hashmap where each key is the array element and the value is the array index + 1.

Still 3 lines, better than the code in the image.

-10

u/Shadow_Being Apr 09 '21

yeah sure i mean i guess so you are right.

If I was to give the original code a readability score I'd say it's a 6/10

If I was to give your code a readability score I'd say its a 6.5/10

not exactly worth even spending the time to do it just for that.

6

u/dpelego Apr 10 '21

How would you do it then?

-2

u/Shadow_Being Apr 10 '21

i would leave it as is because its not worth spending time on it.

Ideally woudn't have architected it such that manually searching a string for certain substrings and converting it to integers by doing all these checks.

E.g. saving data in a json format would probably have been way smarter.

copyProtectScript = JsonSerializer.Deserialize(j);

4

u/TinyBreadBigMouth Apr 10 '21
  • Dictionary is shorter.

  • Dictionary is easier to read and check for typos.

  • Doing a single dictionary lookup is more efficient than doing 12 string comparisons, without even using else to stop comparing when you find a match.

-1

u/Shadow_Being Apr 10 '21
  1. It would definitely also not be shorter. likely the same length

  2. Being easier to read, it is possible that is would be very slightly easier to read which i coverered in another response.

  3. no its not, you still have to do 12 string comparisons because you would have to loop over the dictionary and run ParseDataDontCutLastChar on each item in the dictionary. The performance would actually be very slightly worse because you have the overhead of setting up the dictionary.

2

u/TinyBreadBigMouth Apr 10 '21

Presumably, whatever is in ParseDataDontCutLastChar could be reworked to return the parsed value instead of doing the comparison internally.

Dictionary<string, int> dateNameTable = new Dictionary<string, int>()
{
    { "JAN", 1 },
    { "FEB", 2 },
    { "MAR", 3 },
    { "APR", 4 },
    { "MAY", 5 },
    { "JUN", 6 },
    { "JUL", 7 },
    { "AUG", 8 },
    { "SEP", 9 },
    { "OCT", 10 },
    { "NOV", 11 },
    { "DEC", 12 },
};

...

string monthName = this.ParseDataDontCutLastChar(j);
if (this.dateNameTable.TryGetValue(monthName, out int monthIndex)) {
    copyProtectScript.date_month = monthIndex;
}

1

u/Shadow_Being Apr 10 '21

> Presumably, whatever is in ParseDataDontCutLastChar could be reworked to return the parsed value instead of doing the comparison internally.

Yes that is precisely what the code in OP is doing....

the only correct thing to do here is to ditch trying to create your own serialization system and just use a well established one like JSON.

2

u/TinyBreadBigMouth Apr 10 '21

Yes that is precisely what the code in OP is doing

...How do you figure? It doesn't returns a parsed value, it takes a string and returns the result of comparing that string with the parsed value.

6

u/mplaczek99 Apr 10 '21

Always remember folks, you want to keep your code efficient and tidy. You don't want to be another Yanderedev.

4

u/TheRealSlimCoder Apr 10 '21

do NOT cut that last char!!!!!!

3

u/QuadraticLove Apr 10 '21

Yanderes are pleasing. This is not..

2

u/[deleted] Apr 09 '21

Not yanderedev because he doesn't understand what this. does it what parse means

1

u/c3534l Apr 09 '21

I'm not sure what it says about me that I used to defend Yandere Simulator despite it not being my bag up until I started to see the kind of code the dev was writing.

1

u/Kyn21kx Apr 10 '21

I am almost convinced Yanderedev is not aware of the existance of enums

1

u/Sevigor Apr 10 '21 edited Apr 10 '21

What’s an Enum with a switch?

1

u/Spynder shameless stealing content to r/YandereTechnique Apr 10 '21

r/YandereTechnique in its glory

1

u/RootHouston Apr 10 '21

I am the only one in here disturbed by the complete lack of antialiasing?

1

u/[deleted] Apr 10 '21

This is so stressful to look at please tell me it's not real

1

u/[deleted] Apr 10 '21

any love for enums?

1

u/Taksin77 Apr 10 '21

It's not that bad... Would be better with pattern matching.

1

u/[deleted] Apr 10 '21

Doesn't start from 0...it hurts...

1

u/Gabbianoni Apr 10 '21

What is the name of the font ? It looks cool

1

u/asaf92 Apr 11 '21

Why is not-cutting the last char not the default?!

1

u/LightTranquility3 May 15 '21

the naming conventions hurt my eyes more

0

u/ResTamReditDeDeus Apr 09 '21

An enum could work here too. Also, why do you call him/her Yanderedev?

5

u/[deleted] Apr 10 '21

This is code from the game Yandere simulator. A game about a girl who kills other girls who are dating her senpai or whatever. Games pretty pervy and the dev used patreon funds for a couple grand sex doll.

1

u/Antihihi Apr 10 '21

Actually it's kinda my fault for this but this is not Yandere Simulator, this is a different game made in Unity, I don't want to tell the games name as i actually enjoy it.

-3

u/_-Phage-_ Apr 09 '21

I mean It's better if you use switch in terms of readibility.
If It's performance though It depends on how many time this is called, switch Isn't incredibly faster than if-else(except sometimes.)

29

u/[deleted] Apr 09 '21

um how about an array

7

u/_-Phage-_ Apr 09 '21

yeah

I'm still learning, I'm more familiar with unity so I'd thought it could be something similar.

6

u/[deleted] Apr 10 '21

Well you are already ahead of yanderedev you admitted you were wrong.

14

u/[deleted] Apr 09 '21

The Switch statement is always faster than if-else, except in most cases, the performance gain is insignificant.

3

u/[deleted] Apr 09 '21

Is it on strings though? I don't know if strings are hashed for the switch (so a hash + go-to) or if it's just a chain of strcmp.

4

u/[deleted] Apr 09 '21

As far as I know, C# strings are checked with hashes in the switch statement. I might be wrong, I should check the documentation.

0

u/_-Phage-_ Apr 10 '21

That's what i said..

2

u/[deleted] Apr 10 '21

You said " except sometimes ". There's always a performance gain, but it won't matter unless you write performance-critical code probably.

-1

u/BananyaDev Apr 10 '21

It's depressing how many programmers who probably don't have the slightest idea about or have ever worked on performance critical code decide to make a meme out of the decompiled gameplay code of a random indie dev.

0

u/[deleted] Apr 09 '21

[deleted]

35

u/Antihihi Apr 09 '21

This is dnSpy, reason of that font that it's ran through Wine so yeah

0

u/[deleted] Apr 09 '21

[deleted]

2

u/Antihihi Apr 09 '21

No, this is ugly takes up a lot of lines and could easily be done through an array for example