Like the war between Emacs and Vim, developers also tend to wage a war between "strongly typed" and "loosely typed" languages. There are tradeoffs either way, and I think that's why you see things like TypeScript and Python's type annotations starting to creep into loosely typed languages- types when you need them, but not required. But if you're not comfortable with types, and don't really understand type casting, you might start writing some code like, well, like these examples.

Sashi found this C# code:

public void FillUserinfoFromReaderWithDetails(SqlDataReader reader) { SqlDataReader dr = (SqlDataReader)reader; … }

We get a SqlDataReader as an input, decide that we want to have a different variable which references it (probably for no good reason), and to be extra safe, we make sure to cast the SqlDataReader into a SqlDataReader. All in all, this is just harmless, but it makes you wonder what the developer was thinking.

And if you can do that in C#, imagine the hijinks you can get up to in C. "SvOlli" found this:

*((unsigned short *) &number) = number | 0x2000;

number is defined as an unsigned short. So this code gets the address of number, converts it into a pointer to unsigned short, then dereferences the pointer to get back to the original value, which it sets equal to number | 0x2000. "SvOlli" writes: "We Germans have a saying 'von hinten durch die Burst ins Auge', which translates to 'from behind through the breast into the eye'. It means something that could be done easy is done really complicated."

This line could be replaced by number |= 0x2000.

And finally, we flip over to JavaScript. Oh, sure, JavaScript doesn't require any type conversions, but Corey's co-worker had an object with boolean values in it, and wants to make sure those boolean values get copied over into another object. They might not need to tern one type into another, but "this isn't needed" isn't going to stop them:

function checkAll(checkname, exby) { for (i = 0; i < checkname.length; i++) checkname[i].checked = exby.checked ? true : false }

Now, this may not actually be a WTF, or at least, the code itself may not be. While exby.checked is supposed to be a boolean value, JavaScript is happy to evaluate the truthiness of everything, so if a non-boolean got slipped in, you might get unexpected results. Then again, checkname is almost certainly an array of HTMLInputElements, and the checked property on a checkbox obeys the same truthiness rules as JavaScript variables, so even if these weren't booleans, it'd be fine.

Which brings us to TRWTF, which forever and always will be how JavaScript handles types and type-coercion.

[Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.