r/badcode • • Apr 20 '19

c# bet you never thought of that

Post image
1.2k Upvotes

57 comments sorted by

201

u/EpikYummeh C# Apr 20 '19

This is especially bad because, in C#, converting a bool value to string will return either True or False and thus not match a lower-case F. This is glorious.

249

u/Duck_IRL Apr 20 '19

if(boolean.ToString()[0] == "f".ToUpper())
{
'
'
'
}

Fixed!

120

u/Dustorn Apr 20 '19

Thanks, I hate it.

9

u/[deleted] Apr 22 '19

Do I sense a member of the 9 year old army?

14

u/Dustorn Apr 22 '19

Nah, worse - a 25 year old with a 9 year old's sense of humor.

15

u/[deleted] Apr 20 '19

Cover every language base.

if (boolean.ToString()[0] == "f".ToUpper() || boolean.ToString()[0] == "f".ToLower()) {
  '
  '
  '
}

4

u/vasilescur Apr 21 '19
if (boolean.ToString().IndexOf("e") == 4)

4

u/SarahC Apr 20 '19

Hold up!

Don't some languages accept the strings "True" AND "true" when coercing values for true, so checking one single character, you'd want to .ToUpper() the left hand side too!

5

u/omenien Apr 20 '19

They may accept both, but I'm sure the result of calling toString() on one has the same casing every time.

1

u/EpikYummeh C# Apr 20 '19

In C#, invoking ToString() on a bool returns bool.TrueString if true and bool.FalseString when false. Thus, the value will be consistent for a given locale.

2

u/ekeyte Apr 21 '19

Code review passed. Let’s merge straight to master.

1

u/Boeldan Apr 29 '19

Or do the boolean to lowercase, that is also as good

1

u/francesco_dig Jun 16 '19

if(boolean.ToString()[0].ToUpper() == "F") {

}

0

u/[deleted] Apr 20 '19

[deleted]

21

u/Blackwater_7 Apr 20 '19
 if((len(boolean.ToString()) > 4) == True)
{
}

3

u/[deleted] Apr 23 '19

You mean

boolean.ToString(). Length

There's no len function in C#

1

u/Blackwater_7 Apr 23 '19

i thought it was python :(

3

u/[deleted] Apr 23 '19

But python doesn't have ToString, that would be str(...)...

4

u/Blackwater_7 Apr 23 '19

you are right man im fucked up

2

u/yoloswag4science Apr 29 '19

It’s okay we all are

3

u/StealthRabbi Apr 20 '19

It works for me this way too. Would it be that way in different locale?

1

u/EpikYummeh C# Apr 20 '19

Would it be that way in different locale?

This is a good point and unfortunately I don't have an answer :(

3

u/chusiksmirnov Apr 23 '19

I have a better idea. It would be more durable:

if (boolean.ToString().Length == 4){...}

1

u/EpikYummeh C# Apr 23 '19

Brilliance...

1

u/MazeOfEncryption Oct 03 '19

.Length?

Pfft, awful code. Use this.

61

u/Cloakknight Apr 20 '19 edited Apr 20 '19

Image Transcription: Code


if(boolean.ToString()[0] == 'f')
{
'
'
'
}

[the three ' are just visual studio placing marks for indentation, and not apostrophe characters]


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!

42

u/Abdul_Alhazred_ Apr 20 '19

Good human.

28

u/[deleted] Apr 20 '19

please note that the three ' are just visual studio placing marks for indentation, and not apostrophe characters.

13

u/Cloakknight Apr 20 '19

I added that in. Thanks, I didn't know what they were

6

u/Earhacker Apr 20 '19

It's really just a dashed line, not characters.

13

u/WhyYouLetRomneyWin Apr 20 '19

Dont worry, it catches mis-spellings by the compiler.

16

u/MalicousMonkey Apr 20 '19

Way faster since the program on only needs to check the first letter of the bool rather than the whole thing

Tapping head

7

u/jarfil Apr 20 '19 edited Dec 02 '23

CENSORED

9

u/Earhacker Apr 20 '19

Yeah so I've done exactly this before.

Postgres has a BOOLEAN data type. Unlike say MySQL which stores a true/false value as an INT 0 or 1, Postgres actually stores true and false values.

In Ruby, the PG gem (package) for accessing Postgres DBs returns these values as strings "true" and "false", and inexplicably not as instances of Ruby's TrueClass and FalseClass.

So I wrote a little wrapper function that abstracted out the conditional here, and wrapped every value in it that should have been boolean but was actually a string:

def string_to_bool str
  str == "true"
end

What's worse is, I was teaching at a coding school when I did this. It didn't matter much because we taught them about ORMs a few weeks later. But yeah, I taught newbies this awful code and I'm sorry.

12

u/SalvadorTheDog Apr 20 '19 edited Apr 20 '19

This doesn't compile. It's either Boolean with a capital B or bool; never boolean.

Edit: I'm dumb

58

u/brmifuse Apr 20 '19

That's a variable you dingus

10

u/c3534l Apr 20 '19

What a fucking moron he is! Who doesn't instantly recognize totally normal variable to have in your code like "boolean" that can't actually be a bool since they're capitalized in C#. What programmer isn't familiar with seeing classes named integer, and floating point numbers named string littered throughout their code. That's like stuff you learn on the first day.

4

u/Sharparam Apr 20 '19

they're capitalized in C#.

You're thinking of Java. In C# you use the keyword bool.

(Technically you can also use the System.Boolean struct but that's advised against and a waste of characters anyway.)

2

u/brmifuse Apr 21 '19

I don't even know C#, I just assumed it was a variable from the ToString() function. Also, dingus was a sarcastic comment so please chill.

1

u/aquarys Apr 27 '19

You forgot your “/s” tag lol

0

u/[deleted] Apr 20 '19

[deleted]

0

u/[deleted] Apr 20 '19

That sounded a lot more clever in your head, which apparently must be a common occurrence for you. Not that you'd notice it yourself, though

2

u/_IsNullOrEmpty Apr 20 '19

And that's what happen when you ran out of coffee

2

u/[deleted] Apr 20 '19

Honestly I don't believe anyone would do this

2

u/raekle Apr 20 '19

I thought one thing when reading this code...

Holy ‘f’...

1

u/[deleted] Apr 20 '19

I hope it's not upper case

1

u/Eim64 Apr 20 '19

next level obfuscation

1

u/[deleted] May 06 '19

This is why we can't have nice things

1

u/KingDarkBlaze Sep 24 '19

I did something similar to this in Scala once. But the reverse.

-3

u/gccpsben Apr 20 '19

Simply “if (!boolean) {....}”

22

u/runonandonandonanon Apr 20 '19

Not bad, but that only works if the value is false. The OP's algorithm will catch any value that starts with f.