Stringly Typed Booleans

I’m getting rather hacked off seeing boolean properties typed as strings. Stringly typed is a phrase I learnt off a post from the excellent Coding Horror blog and it describes a phenomena whereby properties that are of a specific type, e.g. Boolean, Date, int, are stored needlessly as strings.

I’m working on a website that communicates with a money laundering service to check whether an individual is “bad” or not. Individuals can pass the test, but importantly it is possible to pass the test with some warning flags raised. If any of the warning flags are raised then an email should be sent off to compliance for them to do…whatever it is they do. Fair enough right?

The warning flags are obviously booleans. There was either a warning raised or there wasn’t – there are no two ways about it (no pun intended). In the serialized response, the warning flags are encoded as “Yes” or “No”…which is understandable. Now, upon receiving the response, it is parsed and turned into an internal representation. This is where things get really weird. The programmer that coded the object that stores the response from the service has decided to encode the warnings as strings, initialised to “”. Which is totally fucking bonkers.

Why?

Well, now our boolean warnings aren’t really booleans – they have WAY more than two possible values and the meaning of these values is subjective. One may consider “”, null, “No” as false, but could conceivably also consider “false” or “0″. We get the same sort of problem with true – “Yes”, “1″, “true” and then we get a whole load of unknown values which is every other possible string in the world. Which might be considered to be true.

So how the hell can any number of programmers work on this piece of code without introducing errors because of differing definitions of truthy and falsey values? Well, they can’t. To illustrate the problem further, even loosely typed languages differ in their boolean coercion, for example JavaScript and PHP:

<script>
if("0") alert('Opposite day!');
</script>

<?php
if("0") echo 'Opposite day!';
?>

JavaScript considers “0″ true but PHP considers it false. Personally I think JavaScript is “right” here, but as I said before, it is totally subjective.

The icing on the cake is of course the extra code you have to write to check the truthy or falsey string values. Something along the lines of “if x is not null and not empty and not the word No then it is probably true…probably”, which would otherwise have been coded as “if x then true” if x was a boolean – which is orders of magnitude shorter.

Of course, there are some bat shit crazy strongly typed languages that allow you to assign null to a Boolean, but that is a different story altogether.

8,962 thoughts on “Stringly Typed Booleans

  1. кухни на заказ производство спб кухни на заказ производство спб .

  2. кухни на заказ от производителя кухни на заказ от производителя .

  3. Good morning Decode play mysterious rules containing interesting and valuable game mechanics news and master every level you touch see on the website for rulebooks Full information on the link – https://202fliks.site All the best and development in business

  4. Gates of Olympus Slot Additionally, online poker rooms often host tournaments and special events that offer players the chance to win big prizes and compete against other skilled players Whether you’re a casual player looking to have some fun or a serious poker enthusiast aiming to hone your skills, online casino poker is a great choice for those seeking entertainment and potential winnings in the virtual world of poker Players can access a diverse selection of games with different themes, features, and betting limits to suit all preferences Gates of Olympus Slot This diversity ensures that players never get bored and can always find a game that suits their preferences and skill level Casino online is a thriving industry that presents players with exciting opportunities to engage in captivating gameplay and potentially win big rewards To ensure a fair and secure gaming environment, reputable online poker sites implement strict security measures and use certified random number generators to guarantee the integrity of the games Online Slot Machine
    https://snglift.com/neospin-casino-game-review-for-australian-players/
    Zoom in by using the cropping tool.  Ken Burns perfected the pan-and-zoom technique, and while tempting to add into a video, this technique works best with still photographs.  To create a video zoom in, click on the clip where you want to zoom in and then click the crop button.  From there you can select ‘crop to fill’ and click and drag the crop square.  Keep in mind that the higher your video’s resolution the better the zoom with look. Trusted by Top-Tier Media Sites Worldwide With the iPad Pro M2 being the most powerful of all Apple tablets, it’s a no-brainer that this model is a great choice for use as a video editor. A couple of days after discovering VN, I took a second look at another app, one that I tested about a year ago when it was still in beta but somehow lost track of it over the months. It’s called ‘Feelmatic’ and is available for both Android and iOS and similar to VN (at least when looking at the Android version), you have to create an account for their video sharing platform community.

  5. Thank you for another excellent post. Where else could anyone get that type of info in such an ideal way of writing? I have a presentation next week, and I am on the look for such information.

  6. Yesterday, while I was at work, my sister stole my iPad and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is entirely off topic but I had to share it with someone!

  7. Thanks a lot for sharing this with all people you actually understand what you’re talking approximately! Bookmarked. Kindly additionally seek advice from my website =). We may have a hyperlink exchange arrangement among us!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>