DEV Community

Cover image for Never write code you don't want to run, part 1
Ned Twigg for The Context Window

Posted on • Originally published at thecontextwindow.ai

Never write code you don't want to run, part 1

When I was a kid, I wanted to go FAST. And my mom, she didn’t care about fast, she only cared about SAFE. And it sucked, and anytime she’s not around I go FAST, and I have a modest basket of broken bones that wouldn’t be so crooked if she could have had any say, but eventually she didn’t have any say and it was worth it. Fast is worth it.

And all of the good programming languages are like my mom. They have at least one construct entirely devoted to “if the bad thing happens, then what?” In the best languages, you say “I will tie my shoelace”, and the language says “But what if you scrape your forehead?” And nothing else can happen until you have an answer.

“I won’t scrape my forehead while I -” UNNNNNHANDLED CASE ScrapeForeheadException WHAT IF YOU SCRAPE YOUR FOREHEAD ON LllllLLLine TWELVE THIRTY THIRTY TWO then what? …

One option is to run away to live with Python, who lets you drink Coke for breakfast and shit in the refrigerator.1 But it would be better to be a goodie two shoes and say “dear mommy I will get a bandaid from the cupboard and put on neosporin”. That might work, or maybe she’ll hit you with a nested DrankTheNeosporinException but then you can say “dear mommy I’ll call poison control” and eventually she’ll be satisfied and your shoes will be tied.2

As you age and better grasp the desperate scarcity of time, you will correctly say “I don’t have time for this” and incorrectly you’ll say “just do nothing”. The thing she’s worried about? It won’t happen. So if it does happen, do nothing. (because it won’t happen)

Fast is worth it. But you would be better off proudly eating your boogers at Python’s house than to “do nothing” if you scrape your forehead. Because while your mom may or may not respect you, your computer absolutely does. If you say “do nothing”, then nothing will be done. Shoelace by shoelace, your foreskull will be ground flat like the lucky red crayon that never got lost, and as you commanded, nothing will be done.

If you scrape your forehead while tying your shoes, you should scream. You should panic, and you should thank the wise mommy who gave you a chance to do so.

Top comments (3)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Interesting and fun read!

As a non-progammer (you may think what am I doing here... I'm a Community Manager 🙌), I thought this was a pretty cool analogy.

It sounds like Python doesn't give ya the same constraints as some other languages, but it also means you can go off the rails a bit.

In any case, I wondered if the same could be said from JS frameworks versus using vanilla JS. In my non-programmer brain, I think of frameworks as kinda limiting the things you can do and handling some things for you to kinda keep you in your lane. But, I don't know if that's a great example. I reckon I think of frameworks as a coloring book where you color inside the lines and vanilla JS is just drawing freehand. Please do tell me if I'm thinking about this the wrong way though. 😅

Collapse
 
nedtwigg profile image
Ned Twigg

Yeah, you're thinking of it exactly right. The piece is about that, but moreso it's about this thing where people write code, and they intend for it to never run. Which seems crazy! I think it happens more in the strict languages, because the compiler forces you to do something, but it definitely happens in Python and JS too. I wrote this particular piece because I was just working with a Python junior who wrote some code that he didn't want to run. "Why is this here?" "It doesn't matter, it's not going to run?" "Why would you spend time writing code you don't want to run?" Just a playful riff on that.

Collapse
 
michaeltharrington profile image
Michael Tharrington

Ooo right on, I follow ya now! Yeah, that is a goofy concept, haha — I'm analogy-less right now on this one, but that's interesting.