r/badcode • u/Achereto • Jun 09 '20
c++ EA published their Command&Conquer Code on github. This is my favorite part so far.
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?
The comment adds nothing to your code that isn't already explained by the code.
The name "magic number" is a very bad choice since it's not explanatory.
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
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
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
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
4
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
214
119
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
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
2
u/schwester Jun 10 '20
This is the first thing that comes to my mind when thinking of magic number:
John Carmack's Unusual Fast Inverse Square Root (Quake III)
https://medium.com/hard-mode/the-legendary-fast-inverse-square-root-e51fee3b49d9
https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overview_of_the_code
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.
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.