r/badcode • u/SagglySloth • Apr 26 '20
js Found this absolute gold from when I first started programming. Obviously I didn't know about dividing ints.
58
u/TinyBreadBigMouth Apr 26 '20
Just a quick note to you or whoever tagged this as JavaScript: UnityScript is not JavaScript. It's based on JavaScript, and was even originally called JavaScript for some stupid reason, but it isn't. It's a thin veneer over the C# that Unity actually uses for its scripting system. Case in point: JavaScript doesn't have integer division. It only has a single number type, which is equivalent to C#'s double.
6
u/Elsolar Apr 26 '20
Thanks for this clarification, I was confused why OP would need this workaround in a language that doesn't support integer types.
1
u/SagglySloth Apr 27 '20
types
Javascript was the closest flair. UnityScript has a similar syntax to Javascript. I think UnityScript is actually a layer on top of the boo programming language.
1
u/TinyBreadBigMouth Apr 27 '20
Pretty sure Boo and UnityScript are both just layers on C#/.NET. They don't seem to have much in common other than that.
1
u/RFC793 May 06 '20
It is pretty nice that JavaScript is actually JavaScript now though. It had a bit of an identity crisis across browsers in its infancy.
29
u/ygoc95 Apr 26 '20
I like the empty lines that keep it in order
Edit:typo
8
u/jarfil Apr 26 '20 edited Dec 02 '23
CENSORED
2
u/SagglySloth Apr 27 '20
The commented out lines was my first attempt but I kept getting 0 or 1 because of int division.
7
162
u/LoneFoxKK Apr 26 '20
Writes two simple lines of code
You don't want to work huh?
Alright I know ways to make YOU and ME suffer
0
Apr 26 '20
[deleted]
13
0
15
u/pohpia Apr 26 '20
If you charge your customers by lines of code, you've hit a goldmine.
6
u/gaberocksall Apr 26 '20
Put braces on new lines in that case
Hell, put every word on a new line
8
Apr 26 '20 edited Apr 26 '20
// check if hp is equal to the integer value "0".
// if so, change the health bar value to "0.0".
if (hp == 0)
{
// Set health bar fill amount to "0.0"
healthb.fill = 0.0;
}
// check if hp is equal to the integer value "1".
// if so, change the health bar value to "0.1".
if (hp == 1)
{
// Set health bar fill amount to "0.1"
healthb.fill = 0.1;
}...
3
11
14
Apr 26 '20
Ahhhh, at least use a switch so it compiles to a jump table
12
u/lmureu Apr 26 '20 edited Apr 26 '20
I reckon any decent compiler would optimize this away.
EDIT: Looks like latest Clang optimizes this with even with -O; while latest gcc doesn't, even with -O3
EDIT2: Apparently clang is the only one that optimizes this, and does so for all platforms (predictably).
EDIT3: Using a switch statement produces optimized code on both clang and gcc, as you correctly suggested.
5
u/dbgprint Apr 26 '20
Compilers are not as smart as you think.
6
u/lmureu Apr 26 '20
if anything this teaches me that clang has really surpassed gcc, and is the best compiler we have. Apparently clang has had this optimization since forever.
Unfortunately the Linux kernel is strongly dependent on GCC specific behaviour and extensions, and until those become available in clang, the Linux kernel must be compiled using gcc. But that's another story.
4
u/dbgprint Apr 26 '20
Yes, clang is by far the best. I’ve found that ICC is also really good too, but it has a lot of issues. My point is that compilers are not smart. They suck most of the time. They don’t know what you want to happen from your code and therefore has no idea how to optimize it properly, they can mainly recognize certain patterns of instructions and optimize them or encode instructions better. Yes, they do more, but that’s generally what they do. Be careful with the «compilers are pretty smart» thought because in my experience, whenever I thought the compiler would optimize something it didn’t.
7
u/lmureu Apr 26 '20
Be careful with the «compilers are pretty smart» thought because in my experience, whenever I thought the compiler would optimize something it didn’t.
Fair enough. In my experience compilers are smart if you are smart in telling the compiled what you want. The OP is a perfect example: if you make that mess only the best compiler in the world is able to figure it out, but if you give it even the smallest of a hint (using the switch) it gets the thing done.
Bottom line: write decent code
4
u/dbgprint Apr 26 '20
Exactly. I’m just mentioning this because I constantly see «programmers» writing bad code and using the excuse «compilers are smart enough these days» when in reality they are just too lazy to understand the underlying concepts such as the stack, registers, instructions, etc.
1
2
u/0x564A00 Apr 26 '20
I don't think the mainline kernel is ready, but there are other versions that can be built with clang.
3
7
Apr 26 '20
In my frist programming course, we had a final exercise to do a four in a row (Connect Four without gravity). One pupil in the class did hard-code every check for four in a row.
12
u/codeOrCoffee Apr 26 '20
It took me 2 seconds to understand what you were trying to do, and how you failed, and another 1 to fix it. So all in all not terrible.
This is what I want from my junior devs, you had a problem but rather than give up you still brought the project to completion. You can always tighten up your code later, as long as it works.
2
1
2
u/activeXdiamond May 14 '20
I mean... it is more performant...
If you've got plenty of RAM but less CPU cycles..
Well, if only you use "else if"s.
1
u/Pangocciolo Apr 26 '20
It's fun thinking about that the goal of coding tools that make coding easier, should be better coding.
1
1
1
1
0
Apr 26 '20
[deleted]
2
u/bnl1 Apr 26 '20
That probably didn't work because of dividing ints
1
u/SagglySloth Apr 27 '20
Yeah, pretty much my first time trying to code without learning anything. I didn't even have internet back then to look up a better solution.
2
0
Apr 26 '20
[deleted]
15
u/Mr2-1782Man Apr 26 '20
Why would they need a div by 0 handler?
I'm more worried about the fact that your comment got upvotes.
8
262
u/octocode Apr 26 '20
My favourite part is where
9results in0.9instead of0.09