r/badcode • • Apr 26 '20

js Found this absolute gold from when I first started programming. Obviously I didn't know about dividing ints.

Post image
757 Upvotes

57 comments sorted by

262

u/octocode Apr 26 '20

My favourite part is where 9 results in 0.9 instead of 0.09

5

u/SagglySloth Apr 27 '20

It's funny because I didn't even know that was there until you pointed it out

42

u/pranksta02 Apr 26 '20

Glad I'm not the only one annoyed by that

51

u/fakehalo Apr 26 '20

Assuming this is a game, you're gonna get a real false sense of confidence for a very short amount of time.

42

u/h2g2_researcher Apr 26 '20

There's a really sneaky game-design cheat where the health bar isn't linear with health. I think it was one of the early Assassin's Creed games which was the first game to openly do it, but with about 10% health bar you're at 30% health. The idea is to give you a lot more experiences where you feel you rescued yourself from the brink of defeat where the fight would otherwise have felt hard, but always under control.

11

u/[deleted] Apr 26 '20

I think Diablo did it even earlier

8

u/Urio_Badapple Apr 26 '20

Oh yeah I’m pretty sure the new Doom games do that too

5

u/meldsher Apr 26 '20

Now my butt-clenching clutch survival on high difficulty won't feel so good anymore :(

4

u/lolitscarter Apr 26 '20

Destiny does that as well

2

u/sunyudai May 04 '20

One of the Half Life games, HL2 I think, had a bit where if your health was greater than 1, there was a chance that damage which should kill you would instead leave you with 1 HP exactly (50%-ish, IIRC).

8

u/miketwo345 Apr 26 '20

Exactly the kind of bug that "less lines of code" can avoid.

0

u/[deleted] Apr 26 '20

[deleted]

0

u/theInfiniteHammer Apr 26 '20

You could just check for the exception and then divide if it's not the exception. That way when your character is at 10% health and they get hit you instantly gain some health to help make it easier. It's a brilliant idea.

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

u/[deleted] Apr 26 '20

Hell, those two comments up top could almost do the job themselves.

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

u/[deleted] Apr 26 '20

[deleted]

13

u/Davis019 Apr 26 '20

Who came up with such a terrible metric

0

u/[deleted] Apr 26 '20

[deleted]

12

u/MoldyWolf Apr 26 '20

They probably wonder why their software is clunky

1

u/currentlyatwork1234 Apr 29 '20

Don't work for a tech illiterate manager is the best solution.

0

u/[deleted] Apr 26 '20

[deleted]

1

u/sunyudai May 04 '20

Rarely, but some places, yes.

It was more prevalent in the 80s.

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

u/[deleted] 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

u/pohpia Apr 26 '20

And do massive block comments, if they count

11

u/Chillgamer3 Apr 26 '20

Looks like yandere simulator code

14

u/[deleted] 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

Tested both ARM and x86-64

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

u/djdanlib Apr 26 '20

Clang is best? Pshaw, I still use Borland.

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

u/nikvasya Apr 26 '20

Or atleast else-ifs...

7

u/[deleted] 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

u/-Dueck- Apr 26 '20

This could have been avoided with a single 5 second google search though

1

u/[deleted] Apr 27 '20

Is that unity javascript?

1

u/SagglySloth Apr 28 '20

UnityScript. Its a similar syntax to javascript but javascript.

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

u/Gortix Apr 26 '20

But if its js, then it would normy divide into float number

1

u/Beall619 Apr 26 '20

HealthBar.fillAmount = (Health.Health/100);

1

u/NergoN123 Apr 26 '20

In unity can scale a health bar 0-100

1

u/hddnblde Apr 27 '20

someone's first unity coding. 🤣

0

u/[deleted] 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

u/_-Shimo-_ Apr 26 '20

You can devide ints if you write it like this afaik:

float = int / float;

0

u/[deleted] 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

u/SleepyHarry Apr 26 '20

Just in case 100 was actually 0. Better to be safe than sorry.