dsmvwlng

Hands up if you know what I’m talking about.

You have a piece of code, perhaps a block about 15 lines that prepares for one statement, say, an if statement. Those 15 lines, all they do is take some data and reform it into a useful format so that when it comes time to execute the if statement you’re good to go.

If your hand isn’t up stop reading because you haven’t written anything more than a hello world program. The above description is something that all software does. Take this snippet:

user_input = input("Enter something: ")
digits = re.sub('[^0-9]', '', user_input)
if len(digits) > 10:
    print("That's a lot of digits")

The problem that “dsmvwlng” solves has to do with the code between between taking the user input and the print statement. I made a variable digits, whose value is derived in a way which doesn’t really matter in order to understand the main point of this block. That particular instruction is important to this code block functionally but not cognitively.

You see, that line uses the re module, and how the re module works doesn’t matter here. The fact that I told the re module to replace all occurrences of non-digit characters with null, which yields just digits back is really a distraction. There are other ways to get just the digits out of a string. But the main thing is that this variable has the digits. So, in order to sort of emphasize what is really happening in this code block, how do I make user_input stand out more?

I’m not going to consider capital letters or prefixes, though. One reason is because they are ugly and distracting, but the real reason I’m not going to consider them is because we have the question wrong. The real question should be, how do we make the minor variables more obscure. This is how I would write this today:

user_input = input("Enter something: ")
dgts = re.sub('[^0-9]', '', user_input)
more_than_ten_digits = len(dgts) > 10
if more_than_ten_digits:
    print("That's a lot of digits")

By removing the vowels, I obscure that variable. This is known as disemvowling, and hence the title of this post. It’s a way of keeping the code still readable, yet at the same time creating a major/minor distinction in a block. You’ll also notice, that to improve readability, I stored the result of the len() function into a variable that is written out in a way so that the if statement becomes a bit more like natural language.

This is the convention I like to use, although I don’t exactly remove all the vowels though. I keep any vowels that are initials or finals, if I wanted to call the digits variable only_digits, it would be only_dgits.

Previous Post
Comments are closed.