dirtydozen – Magic strings

dirtydozen

I. Magic strings

Don’t lose it, re-use it

There’ll be a point in your programming life where you’ll come across string data types. These are normally defined as double quoted "..." pieces of text, like the examples below.

"foo"
"My message sentence"
"123"
"true"

As you can see, strings can be quite powerful, right? You can wrap the actual text in these, numbers and even boolean types.

The problem called Magic strings arises when you have code written where you check if one value, perhaps assigned in a variable, equals another value, perhaps written in the string.

For example, this small code snippet in isolation doesn’t look too offensive, right?

var coffeeName = "americano";
if (coffeeName == "latte")
{
    return "Good choice with the Latte!";
}

Read more on dirtydozen

Browse to https://qbituniverse.github.io/dirtydozen/magic-strings to carry on reading about Magic strings

Marcin Narloch

Marcin Narloch

Creative and out-of-the-box thinker with strong interests and knowledge in technology and innovation.
dirtydozen Previous post dirtydozen – Introduction
dirtydozen Next post dirtydozen – One-trick-pony variables

Leave a Reply

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