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.

13,534 thoughts on “Stringly Typed Booleans

  1. I have realized some important things through your website post. One other thing I would like to convey is that there are various games in the marketplace designed mainly for toddler age kids. They incorporate pattern identification, colors, family pets, and forms. These usually focus on familiarization as opposed to memorization. This helps to keep children and kids engaged without feeling like they are studying. Thanks

  2. It’s appropriate time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you some interesting things or suggestions. Maybe you could write next articles referring to this article. I wish to read even more things about it!

  3. Hi, i feel that i saw you visited my web site so i got here to ?return the choose?.I’m attempting to in finding things to improve my web site!I assume its good enough to make use of a few of your concepts!!

  4. whoah this blog is wonderful i love reading your articles. Keep up the good work! You know, a lot of people are looking around for this info, you can help them greatly.

  5. звоните круглосуточно по телефону горячей линии клиники: наши специалисты готовы оказать необходимую помощь в решении проблемы алкогольной зависимости. Узнать больше – вывод из запоя на дому

  6. I?ll immediately seize your rss as I can’t in finding your email subscription link or newsletter service. Do you’ve any? Kindly let me recognise so that I could subscribe. Thanks.

  7. Thanks for your publication on this blog site. From my own experience, there are occassions when softening upwards a photograph might provide the photographer with a bit of an artsy flare. Oftentimes however, the soft cloud isn’t what exactly you had at heart and can in many cases spoil a normally good image, especially if you anticipate enlarging them.

  8. Nice post. I learn one thing more challenging on different blogs everyday. It’ll always be stimulating to read content material from other writers and follow a little bit something from their store. I?d choose to make use of some with the content material on my blog whether or not you don?t mind. Natually I?ll give you a hyperlink in your net blog. Thanks for sharing.

  9. Foi só aumentar a bet um pouquinho e o Bandito soltou um Multiplicador de 50x. lucro de 5 meses na conta!

  10. O multiplicador estourou no Candy Bonanza! Foi um Multiplicador de 50x absurdo, coisa de R$ 12.000.

  11. Thanks for the unique tips contributed on this blog. I have realized that many insurance carriers offer clients generous special discounts if they favor to insure many cars with them. A significant volume of households include several cars or trucks these days, particularly those with mature teenage kids still located at home, along with the savings upon policies can soon mount up. So it is good to look for a great deal.

  12. Thanks for the helpful article. It is also my belief that mesothelioma has an incredibly long latency period, which means that signs of the disease won’t emerge until 30 to 50 years after the 1st exposure to asbestos fiber. Pleural mesothelioma, that is certainly the most common style and influences the area about the lungs, might cause shortness of breath, upper body pains, along with a persistent cough, which may bring on coughing up blood vessels.

  13. One thing is that if you find yourself searching for a student loan you may find that you’ll want a co-signer. There are many circumstances where this is correct because you might find that you do not use a past history of credit so the loan provider will require that you have someone cosign the loan for you. Great post.

  14. Thank you for some other informative web site. The place else may I am getting that kind of info written in such a perfect way? I’ve a venture that I am just now operating on, and I’ve been on the look out for such info.

  15. With every little thing that seems to be developing within this specific area, all your opinions happen to be very exciting. However, I beg your pardon, because I can not subscribe to your entire idea, all be it refreshing none the less. It looks to us that your remarks are generally not totally validated and in reality you are your self not even totally convinced of the argument. In any case I did take pleasure in examining it.

  16. Looking for the Best Drive School of Motoring? Get expert driving lessons at best driving school for affordable and quality driving instruction.

  17. Looking for the Best Drive School of Motoring? Get expert driving lessons at best driving school for affordable and quality driving instruction.

  18. Dica: joguem Fortune Mouse depois do almoço. Acabei de pegar um Forra Máxima de lucro de 5 meses.

  19. Кстати, если вас интересует Открывая литературный мир Александра Ольшанского, загляните сюда.

    Смотрите сами:

    https://olshanski.ru

  20. Thanks for the auspicious writeup. It in truth used to be a leisure account it. Glance complex to far added agreeable from you! By the way, how can we keep in touch?

  21. Have you ever considered about adding a little bit more than just your articles? I mean, what you say is valuable and all. But imagine if you added some great images or videos to give your posts more, “pop”! Your content is excellent but with pics and videos, this site could undeniably be one of the most beneficial in its niche. Terrific blog!

  22. I have realized some important things through your blog post. One other subject I would like to express is that there are numerous games in the marketplace designed especially for preschool age young children. They include things like pattern recognition, colors, animals, and models. These usually focus on familiarization in lieu of memorization. This helps to keep children and kids engaged without feeling like they are studying. Thanks

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>