r/badcode • • Jun 09 '20

c++ EA published their Command&Conquer Code on github. This is my favorite part so far.

Post image
1.6k Upvotes

36 comments sorted by

27

u/[deleted] Jun 09 '20

That looks quite normal, and yes, it's the constructor field. Ok, there are some ways to make it smaller - like element(element) - but it's far from bad code.

-30

u/Achereto Jun 09 '20

Yes, the code doesn't do anything horrible. It's also perfectly okay, that the constructor does nothing but initialization of fields. But that's not the point. ;-)

10

u/[deleted] Jun 09 '20

If it's perfectly okay, it doesn't belong here

-12

u/Achereto Jun 09 '20

There are different ways code can be bad. Once is horrible behaviour, another is bad maintainability. This is about the latter.

36

u/Achereto Jun 09 '20

You can find that piece of code here: https://github.com/electronicarts/CnC_Remastered_Collection/blob/master/REDALERT/CONNECT.CPP#L98

Why is it bad?

  1. The comment adds nothing to your code that isn't already explained by the code.

  2. The name "magic number" is a very bad choice since it's not explanatory.

  3. lots of visual noise around the code.

38

u/gu3st12 Jun 09 '20

It's not the worst tbh. It looks bad out of context, but given how UDP works, you need some value that represents a specific "connection" to the server.

11

u/Achereto Jun 09 '20

So what does "magic number" do? Does it cast some magic? Or does it identify a UDP Connection? If the latter, why isn't it named "UDPConnectionID" or something similar? Wouldn't that be much more readable?

40

u/evanratt Jun 09 '20

Magic Number is an actual term when using UDP connections: https://tools.ietf.org/html/draft-herbert-udp-magic-numbers-00

Someone who is familiar with UDP would look at this code and know what a "magic number" is, because it's the correct term. UDPConnectionID could actually introduce confusion by creating a new term, rather than using the accepted canonical term for this value.

10

u/Achereto Jun 09 '20

Ah, thanks! Didn't know that. ♥️

5

u/evanratt Jun 09 '20

Ya, not knowing that term, I would question the variable name, as well 😀

7

u/tuckmuck203 Jun 09 '20

Wild how domain knowledge can immediately change something from "what the fuck" to "why the fuck would you do otherwise". I wish more "bad code" was like this.

1

u/loopsdeer Jun 10 '20

To the top!

10

u/web8564j Jun 09 '20

I think this is a valid question, not sure why it's been downvoted. I can only think that magicnumber would be ok if it's litterally called magic number in whatever protocol is being processed.

Otherwise I completely agree, UDPConnectionID would be loads better.

2

u/[deleted] Sep 30 '20

I know this is an old comment, but that's actually exactly the case here. Apparently, UDP does use the term magic number as part of the protocol. At least that's what I found out from other comments here.

5

u/KernowRoger Jun 09 '20

But why call it magic number and not like connectionId?

9

u/locuester Jun 09 '20 edited Jun 09 '20

ConnectionId would imply usefulness to someone debugging. Magic number makes it clear that it’s used as a [magic number](https://en.m.wikipedia.org/wiki/Magic_number_(programming))

2

u/TheSpiffySpaceman Jun 09 '20

I think the last parenthesis of your URL escaped the URL formatting...

3

u/locuester Jun 09 '20

Thx. Fixed.

4

u/scandii Jun 09 '20

fakedRequiredUDPConnectionNumber, you're welcome.

16

u/microwavedHamster Jun 09 '20

Serious question. Have you ever worked as a software engineer in the video game industry?

4

u/Achereto Jun 09 '20

No. I am a Software Engineer for ~5 years now, but I haven't worked in the game industry yet.

2

u/sendvo Jun 09 '20

damn that code has so much shitty useless comments

214

u/barzamsr Jun 09 '20

Aren't magic numbers supposed to be defined as constants, not variables?

0

u/[deleted] Jun 09 '20

[deleted]

45

u/cj81499 Jun 09 '20

So it's a bad variable name

119

u/[deleted] Jun 09 '20

I don’t fully agree this is bad code. This depends a lot on the context like:

  • is there a company rule that requires every assignment to be documented (in which case the rule itself is the “problem”, not the code)

  • the name magic number might be very meaningful to everyone working on this project or even the whole industry (I’m not a game dev so I don’t know but I’ve seen the term magic number in other software where people were aware of its meaning)

9

u/gu3st12 Jun 09 '20

0

u/[deleted] Jun 09 '20

[deleted]

22

u/survivalking4 Jun 09 '20

No, don’t you see? It’s on the badcode subreddit, so it clearly MUST be bad code!

-26

u/scandii Jun 09 '20

the fact that this thread exists speaks for the fact that it is bad naming.

naming standard number one:

always without fail name your variables after what they are. the number is something, be it an ID, result of an operation, captured data etc.

5

u/GasolinePizza Jun 09 '20

The actual term for this value is "magic number". This is the correct name for this variable. Clear, concise naming does not include defining terms in the definition name for readers ignorant to the problem domain.

Edit: Just to clarify, I mean ignorant in the neutral sense, not the hostile form as an insult.

22

u/sergushka Jun 09 '20

It's actually not a bad code since this magic number is a part of the protocol and is used to check for example if there are no corruptions or the client is compatible. Protocol description could be elsewhere in the documentation.

3

u/asaf92 Jun 09 '20

If a magic number is not hard-coded than is it really a magic number?

1

u/skwrrkk Jun 09 '20

Who knew magic was this simple and easy? Fuck, I should have been a magician. Tech is for lames.

Thanks for enlightening me

-1

u/Yevgen4000 Jun 09 '20

They probably got lazy of remembering whether it was capital or not so they did this.