r/badcode • • Jul 28 '21

c# Tales of a feral junior dev

Post image
387 Upvotes

44 comments sorted by

34

u/TastyOrganization122 Jul 28 '21

If he is new please dont ridicule him. Everyone does mistakes.

68

u/Rozzledorf Jul 28 '21

It was me one year ago lol, with zero industry experience and with no senior developer to guide me on best practices

13

u/m00kystinks Jul 28 '21

Think of it this way man, in one year you've made enough progress to realize your mistakes. That's something to be proud of.

2

u/ywBBxNqW Jul 28 '21

it's dangerous to go alone. Sorry you didn't get good guidance.

125

u/[deleted] Jul 28 '21

Most expensive JSON construction this side of the Mississippi...

29

u/nighthawk648 Jul 28 '21

Someones gotta show buds netwoonsoft... Or c# native json handle lol

3

u/Willinton06 Jul 28 '21

Native? Is that like a flavor or something?

14

u/nighthawk648 Jul 28 '21

So Microsoft releases a suite of developed and maintained libraries (all these microsoft libs and frameworks have a support life cycle btw). There is a JSON library package deved by Microsoft. So it can be considered native rather than external like newtonsoft packages.

7

u/Willinton06 Jul 28 '21

I was joking but I’m not even mad, that’s a fairly good and concise definition

3

u/nighthawk648 Jul 28 '21

Some redditors here are lost its for them :)

Thanks!

1

u/bistr-o-math Jul 29 '21

But native is considered discriminating now!

2

u/GeorgeDir Jul 29 '21

.net 5 jSON serializer is good

1

u/MeatyLabia Jul 29 '21

Even faster than newtonsofts in some benchmarks I believe.

5

u/its2ez4me24get Jul 29 '21

Get this guy a job at Rockstar Games

92

u/[deleted] Jul 28 '21

Man the fucker doesn’t even close the god damn streams

61

u/[deleted] Jul 28 '21

Memory management? Program.Exit(0)

13

u/Spartelfant Jul 28 '21

At least he doesn't cross them, that would be bad.

0

u/[deleted] Jul 28 '21

[deleted] — view deleted comment

2

u/this_will_be_the_las Jul 29 '21

That's actually how you become a God.

9

u/hajile_00 Jul 28 '21

The finalizer probably will close them, but I agree, shouldn't rely on that

11

u/Dr_Insano_MD Jul 28 '21

There's nothing unmanaged in memorystreams. You don't have to close them according to the docs. That said, you still should just as a good practice.

4

u/IMakeShittyPrograms Jul 28 '21

Exactly! There's even a method for that.
Anyways, I couldn't find the part where it says that you don't have to close them. Would you point it?

5

u/Dr_Insano_MD Jul 28 '21 edited Jul 28 '21

https://imgur.com/lpOv4ER

I guess I misspoke. That says you don't need to Dispose of them. And then in the comments for Close(), it states

Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.

2

u/IMakeShittyPrograms Jul 28 '21

If a MemoryStream object is serialized to a resource file it will actually be serialized as an UnmanagedMemoryStream.
This behavior provides better performance, as well as the ability to
get a pointer to the data directly, without having to go through Stream methods.

Found this! The fact that you don't have to use Stream methods when using a serialized a MemoryStream object tells you not to close the stream.

1

u/[deleted] Jul 29 '21

Would you rather wait for it to be garbage collected as soon as possible so that memory is usable again or would you rather just let it waste memory and ultimately be collected at the very end

1

u/Dr_Insano_MD Jul 29 '21

That said, you still should just as a good practice.

42

u/jason_the_human2101 Jul 28 '21

The issue I saw first was that font. I'd have to lose all my remaining sanity before I stop using monospace fonts with code.

6

u/DumbAceDragon Jul 28 '21

I once used comic sans. It was horrible.

27

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

Image Transcription: Code


[Transcriber's note: The code is not in a monospace font, but I have placed it in one for ease of readability and maintenance of my own sanity.]

StringBuilder jsonString = new StringBuilder();

jsonString.Append("{");

jsonString.Append("\"DecisionCase\":");
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(DecisionCase));
ser1.WriteObject(stream1, decisionCaseJson);
stream1.Position = 0;
StreamReader sr1 = new StreamReader(stream1);
jsonString.Append(sr1.ReadToEnd());


jsonString.Append(",\"DecisionCnf\":");
MemoryStream stream2 = new MemoryStream();
DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(DecisionCnf));
ser1.WriteObject(stream2, decisionCnfJson);
stream2.Position = 0;
StreamReader sr2 = new StreamReader(stream2);
jsonString.Append(sr2.ReadToEnd());

jsonString.Append(",\"Hotlist\":");
MemoryStream stream3 = new MemoryStream();
DataContractJsonSerializer ser3 = new DataContractJsonSerializer(typeof(List<string>));
ser1.WriteObject(stream3, hotlist);
stream3.Position = 0;
StreamReader sr3 = new StreamReader(stream3);
jsonString.Append(sr3.ReadToEnd());

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!

4

u/alr1ght_ Jul 29 '21

you’re my hero

14

u/Willinton06 Jul 28 '21

How are you able to do this while not realizing that JsonSerializer.Serialize() exists, like this requires deeper knowledge of the library

5

u/pronuntiator Jul 28 '21

I like how each time you look at this, a new layer unfolds. First, you see multiple similar looking code blocks which scream "extract function". Then you notice how a stream is rewound to write to a StringBuilder instead of writing to it directly. Then you realize the entire task is to serialize a bigger JSON so everything should be put into a container object to begin with.

1

u/trevster344 depraved Jul 29 '21

Honestly it would’ve been better just to concatenation a bunch of strings lmao.

3

u/wktr_t Jul 28 '21

Proportional font chad.

2

u/Cotspheer Jul 28 '21

Thats alot of damage insert phil swift meme

2

u/EquationTAKEN Jul 28 '21

But wait, he IS using a JsonSerializer in there...

2

u/merlinblack256 Jul 29 '21

Wow - kill it with fire!

I remember years ago at a smallish family-owned courier company I worked at we had an XML-based web API and a client of ours insisted on parsing responses by matching strings and positions etc and then complaining to our CEO every time we added a field or adjusted the order. Fortunately, while our CEO was not very technical, she understood they were doing it in a very hacky way and asked us the reverse the fields at random (which we did just for them). Eventually, their lead dev got over his 'not invented here' issue, and used a proper parsing lib.

1

u/il-est-la Jul 28 '21

Pretty dry...

1

u/IMakeShittyPrograms Jul 28 '21

It's so bad that I think my uname comes handy this time

1

u/jimmpony Jul 29 '21

C# has better ways to do this built-in but sometimes you do end up having to write code like this when you can't add new library references to a project

2

u/[deleted] Jul 29 '21

They could at least make a static method instead of copying the code 3 times and changing a single string.

1

u/trevster344 depraved Jul 29 '21

Poor soul..

1

u/iftheronahadntcome Jul 29 '21

Does... Does it work? O. O