Naming Things is Hard

Good names are hard to come up with.

Some reasons we suck at naming:

  • We get in a hurry
  • Good names take careful thought
  • We are in a hurry

I see a theme.

Here are some rules of thumb I’ve found handy.

Don’t encrypt a variable name. For example, if you are tempted to call a “request id” hashValue because every request is assigned a unique hash, you have encrypted the meaning of the variable (and obfuscated it on top of that). Yeah, don’t do that.

Don’t make method names overly generic. For example, if you have a method that calculates the balance in an account, don’t call it “processAccount()“. Instead call it calculateBalance(). It’s much clearer what it does, and you’re less tempted to pollute it with other functionality that should be included elsewhere as you maintain and enhance the code (think of it as technical debt insurance).

Don’t abbreviate variable or method names. Ever. EVER. The abbreviation might make sense to you, today, as of this moment. But chances are six months from now, it will take you a second to figure out what the heck maxBalCalcd means (maximum calculated balance value? maximum balance calculation date? No, sorry, it’s “has the maximum calculated balance been reached”, thanks for playing). As sucky as we are at naming, we are even suckier at abbreviating those bad names. Your colleagues will thank you (okay, probably not, but it’s still a good idea).

Don’t play code golf with variable or method names. I’ve seen really smart developers use variables like e to hold the return value from a function call. And the description of the data does not even start with the letter e. But even if it did, it’s still a bad idea. It’s not obvious what it does, and just makes the code harder to read.

Do give something a name that exactly matches what it is does. If it’s a function or method, name it according to what it does (and if it’s name would be too long if you called it everything it does, then you need to refactor your design). If it’s a variable, name it according to the type of data it holds.

Do give loop variables simple names. Yes, this seems to fly in the face of what I’ve been saying, but I’m talking about throw-away variables that are used to loop through the items in a collection, for example. I like aa, bb, and cc. Some like i, j and k. But whatever you do, for god sakes, though, don’t call it loopVariable. That’s overkill.

Do use long names because they’re descriptive. I would much rather see a method called computeChecksumIfPossible() than compChksum().

Do learn the language of the business domain you’re writing code for, and name things accordingly. My favorite awful method name of all time: getAreaCode(). Looks innocent enough, right? Turns out the developer wanted the first three digits of the U.S. zip code. In the USA the term “area code” has a whole other meaning. I’m not making this up.

Hope you found these tips helpful. Please feel free to comment and add a few of your own.

Happy naming!

–jsp

Make: Please Cancel My Subscription

I don’t know what has happened to Make Magazine over the years. When I received my first issue in 2004 I was very excited about the content, the projects (Mousebot!).

And this great editorial focus continued for years. Robots? Check. Circuit design? Check. Tesla coil? Van deGraff generator? Check. Check.

But something has happened in the past couple of years or so. The occasional article about “wearable blinking jewelry” (W.T.F?) has become the norm.

Back in the day, my Make subscription was a nice complement to my Nuts and Volts subscription (which has stayed true to its editorial focus since I’ve been a subscriber in 2005). Now, they are nothing alike.

O’Reilly, please cancel my subscription to Make. And good luck with wearable drone jewelry.

–jsp

Turn off Autocorrect on Mac

As a programmer I am always typing in weird words like Hyperledger, JUnit, and assertAll (don’t ask).

Yet, my Mac likes to correct these words to hyper ledger (fair enough), unit (hm, okay) and assertable (whoa).

TL;DR Go to System Preferences > Language & Region > Keyboard Preferences > Text, and uncheck “Correct spelling automatically” and “Use smart quotes and dashes”.

Plus, when I write doc, often times I’m showing you how to use program options, many of which begin with “–” (as in –version). And this gets corrected to a single, long dash.

(sigh)

So, I Googled a bit and found out how to turn this off. For good.

Go to System Preferences, then choose Language & Region. Click the Keyboard Preferences… button, then choose the Text tab. You will see a dialog like this (I’m running MacOS 10.12.6):

Screenshot 2017-10-19 22.04.41

Make sure “Correct spelling automatically” is unchecked. That will stop the constant autocorrecting. Then uncheck “Use smart quotes and dashes” and your — will stay a double dash.

Enjoy!

–jsp

When Polymorphism Fails

When Polymorphism Fails

I love Java. I love OOP. I love polymorphism. But polymorphism does not always solve every problem I have. Something about sweeping statements of the form “If you use instanceof you are a complete idiot” have always bothered me (on many levels, but I’m referring here only to its technical aspect).

This nifty blog post by Steve Yegge made me feel better. It points out when polymorphism just will not do the trick (and has a nice recap/primer of polymorphism if you are a little rusty on it).

Just thought I’d share that.

–jsp

God and Guns: Vote GOP!

Hypocrisy irritates me. Speaking of hypocrisy, this is an election year, and the GOP primaries are in full swing. This week’s topic: morality!

While I believe Mr. Santorum is sincere when he spouts his brand of noise, claiming America’s moral decline is due to the number of unwed mothers, in the end, the GOP utopia is the World of “Bladerunner” where everything is privatized and corporations run the world (we are closer to this than you might think).

The GOP only cozies up to the religious right and the second amendment with their scare tactics to get the votes to bring that utopia to pass (after all they represent only 1%, which isn’t enough votes). The don’t give one tiny toot about morality.

Truth be told, the U.S. is a plutocracy; a system set up to (a) protect the wealthy and (b) make sure the system perpetuates itself. All this talk of morality is scare-noise meant to frighten the religious right into voting GOP.

And it works.

Separation of Church and State

I was reading this article about House Speaker John Boehner making a huge fuss about the White House’s stand on providing equal access to birth control to all women.

Now, I’m no contitutional scholar, but it seems like there are two interpretations of “Separation of Church and State”. The Democrats (for the most part) view it as “Keep matters of faith and religion separate from policy.” While the GOP (for the most part) view it as “Support Christianity – and *only* Christianity – using the full power of policy to do so.”

I may be generalizing, but whenever government supports an idea that benefits society as a whole (such as preventing unwanted pregnancy in this case), the other side (the GOP in this case) claim that government is stepping on religious freedom/rights/whatever. Isn’t the idea that policy be separate from religion? Isn’t America a great melting pot?

Facebook Addicting? That’s Ridiculous (I’m Totally Making That My Status)

I saw this article at ZDNet (they’ve been ON FIRE lately!) about Facebook and Twitter and how they’re more addictive than alcohol or nicotine. Can that be? It…

Sorry, had to check my twitter. What was I saying? Oh, yeah, Facebook and Twitter are addictive. I know for me that they hardly have any effect on me at all. For example,…

Oh, how funny. Somebody just posted the funniest link on my Facebook Wall. Where was I? Oh, yeah, Facebook. I don’t really check it all that often. I mean, I do have alerts sent directly to my Android phone (same for Twitter). But, really, it just doesn’t mean that much to ….

Wow, it looks like Lance Armstrong will be racing a 70.3 Ironman soon. Just saw a Tweet about it. What was I saying?

Oh, nevermind. I forget. Now if you’ll excuse me, I think I’ll check Twitter. It’s been, like, forever.

–jsp