r/shittyprogramming • • Sep 06 '16

Convert a bool to bool

Post image
241 Upvotes

50 comments sorted by

13

u/AlGoreBestGore Sep 06 '16

Can't trust those double equals.

7

u/ab7a71 Sep 06 '16

Ofcourse not! (P.S in JS, colleague uses == and not ===. I've told them difference!)

6

u/Spo8 Sep 06 '16

Tell him to get his shit together. If his code only works because he's using ==, he's writing bad code.

1

u/ab7a71 Sep 06 '16

Haha, I wish I could! :D

1

u/CheshireSwift Sep 06 '16 edited Sep 06 '16

There's one exception I know of where != compared to one of the falsy values (null, I think) is a very succinct way of checking the return value from a bunch of DB libraries. That said, the fact I can't remember which comparison it is counts against it pretty heavily...

2

u/[deleted] Sep 06 '16

You might be thinking of x != null which will match both null and undefined

1

u/CheshireSwift Sep 06 '16

That's the one. (I got carried away with my equals signs, now edited.)

1

u/green_meklar Sep 07 '16

Not in Javascript, anyway...

7

u/hillman_avenger Sep 06 '16

Why is there even a Convert.ToBoolean(boolean) function? To help spot shittyprogrammers?

12

u/ab7a71 Sep 06 '16

Hello, That isnt the case, its actually Convert.ToBoolean(object) and all .Net objects are based off of this. :)

4

u/frisch85 Sep 06 '16 edited Sep 06 '16
if(String.IsNullOrEmpty(txtCostPerHour.Text) || String.IsNullOrEmpty(tstHoursPerDay.Text)) {
}

1

u/ab7a71 Sep 06 '16

I actually like this. But prefer IsNullOrWhiteSpace(). Can I ask why you didnt use this instead?

1

u/frisch85 Sep 06 '16

I edited my post sorry for misleading to .IsEmpty. IsNullOrWhiteSpace makes more sense i guess when used on form properties. We used IsNullOrEmpty in our software to also avoid nullreference-Exceptions but i guess that's covered by IsNullOrWhiteSpace, too.

I am not up-to-date regarding the framework progress but we started on framework version 2.0 which didn't have the IsNullOrWhiteSpace method.

1

u/ab7a71 Sep 06 '16

Oh right, Thanks for explaining!

1

u/iinlane Sep 06 '16

There is no string.IsEmpty() function.

2

u/frisch85 Sep 06 '16

Dang it, sorry it's been couple of years since i've written my last C# program... i'll correct my post immediately.

6

u/iinlane Sep 06 '16

:)

It's weird to write code without autocomplete. I once had to write code on paper and failed miserably - no autocomplete, no backspace, no line insertions, no google or help. Wouldn't recommend.

2

u/frisch85 Sep 06 '16

Yeah lol, when working on my userscripts in tampermonkey i often have to lookup the exact names of some methods... It can get really confusing because in javascript public mehtods are written in camel case while in C# they are written in pascal case...

1

u/Jabbersii Sep 06 '16

Just to nitpick, string.IsNullOrWhiteSpace() will also do an empty check: https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(v=vs.110).aspx

1

u/iinlane Sep 06 '16

It was introduced in .NET 4. Chances are the old program is in .net 2 or 3.5. In this application I would prefer decimal.TryPrase(txthours.Text, out hours).

2

u/Prod_Is_For_Testing Sep 07 '16

Using out variables just for an if condition is annoying if you can find another option (since the variable has to be declared in a parent scope). This should be remedied in C# 7 because they're adding out variable declarations within the function call itself

2

u/willeb96 Sep 24 '16

Convert.ToBoolean(something == true)

1

u/[deleted] Sep 06 '16

😘😘

1

u/[deleted] Sep 06 '16

I was really expecting it was going to be ~(~(value))

1

u/calsosta Sep 07 '16

And the shit posts keep on rolling.

1

u/ekolis Sep 10 '16

Check the timestamp on the source file; he wants a boxed Boolean rather than an unboxed bool. Must be December 26.

0

u/tophatstuff Sep 06 '16

Eh, I can see why you might get into the habit of using a fancy ToBoolean function if you used a heathen language where strings and integers and pointers, empty/0/null or otherwise, aren't truthy/falsey without a cast.

-3

u/dadibom Sep 06 '16

Meh. if (!txtThingy1.Text || !txtThingy2.Text)

3

u/aerno Sep 06 '16

also shitty

2

u/ab7a71 Sep 06 '16

.Text is type string, So I dont thing this will work :)

1

u/dadibom Sep 06 '16

Oh, i didn't know you were using types(cript?)

1

u/dadibom Sep 06 '16

Care to elaborate? I've never used typescript, if it's because of that. I find it to be very useful, given that you actually know what's happening, because I usually don't care if it's null/undefined/0/false/whatevs, since these ifs are usually for "if the string is empty".

2

u/ab7a71 Sep 06 '16

NO its not typescript. This is C# :)

2

u/dadibom Sep 06 '16

Oh, i misunderstood since i read the comment about js

1

u/go_so_loud Sep 06 '16

Worse, actually

5

u/nictytan Sep 06 '16

Use de Morgan's law and have one less operation!

!(string1 && string2)

But yeah in a strongly typed language like C# (OP's example) this won't work of course.

0

u/[deleted] Sep 06 '16

[deleted]

9

u/ab7a71 Sep 06 '16

Hey, what would a compiler do in this case?

1

u/stone_henge Oct 13 '16

Loathe its job

0

u/[deleted] Sep 06 '16

[deleted]

19

u/Cathercy Sep 06 '16

Not familiar with this Convert object or even what language this is, but this seems a bit more complex. Not sure that a compiler would catch it.

2

u/ab7a71 Sep 06 '16

Heya :) Yes it is C#

12

u/VileVial Sep 06 '16

Looks like C#. OP is probably taking a .NET class.

2

u/ILikeLenexa Sep 07 '16

Function in-line expansion should be able to handle it in a single pass.

1

u/bonoboner Sep 06 '16

Agreed. Convert could change the Boolean value for all we know.

0

u/[deleted] Sep 06 '16

[deleted]

2

u/nictytan Sep 06 '16

There's nonetheless the overhead of a static method lookup, and a function call (extremely expensive compared to simply reusing the bool.)

1

u/ab7a71 Sep 06 '16

Oh right! Thanks for explaining! :)

25

u/PUSH_AX Sep 06 '16

You know there's no optimisation for stupid right?

31

u/interiot Sep 06 '16

If you build a better optimizer, someone will build a better idiot.

5

u/dylanthepiguy2 Sep 06 '16

And javascript

17

u/nemec Sep 06 '16 edited Sep 06 '16

Maybe after JIT, but it doesn't seem to be compiled away:

if (Convert.ToBoolean(true))
{
	Console.WriteLine("hello");
}

IL_0000:  nop         
IL_0001:  ldc.i4.1    
IL_0002:  call        System.Convert.ToBoolean
IL_0007:  stloc.0     
IL_0008:  ldloc.0     
IL_0009:  brfalse.s   IL_0018
IL_000B:  nop         
IL_000C:  ldstr       "hello"
IL_0011:  call        System.Console.WriteLine
IL_0016:  nop         
IL_0017:  nop         
IL_0018:  ret        

Edit: I should note that this is the output from LINQPad 5, which uses Roslyn to compile.