dirtydozen.dev – Magic strings


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.

"My message sentence"

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.dev

Browse to https://dirtydozen.dev/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.dev Previous post dirtydozen.dev – Introduction
dirtydozen.dev Next post dirtydozen.dev – One-trick-pony variables

Leave a Reply

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