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

Goodbye Napster. Goodbye Rhapsody.

I loved Napster. For about 10 bucks a month I could listen to whole albums, make playlists of my favorite songs, download albums for listening when I was in a, ahem, low signal area (thanks, AT&T). A week or so ago, without much notice (I seem to recall an email about a week before the transition, and I’m being kind calling it a “transition”), I was forced to convert over to Rhapsody. Today I dumped Rhapsody. Here’s why.

For starters, the interface to the Rhapsody App (I have an Android, but can’t imagine the UI for the iPhone app is much better) is TERRIBLE. I’m a techie, so I know my way around gadgets, and how to do stuff. But this interface is squirrelly. How do I pull up my music? Hm. My Playlists? Nope, that looks like someone else’s playlists, I don’t recognize any of these songs.

None of my previous searches were remembered. I tend to listen to the same artists (tried “My Artists” but the list was flooded with a bunch of artists I’d never listened to) a lot, and each time I wanted to pull up a different, say, ELO album, I had to go into Search and type in Electric Light Orchestra (don’t even think of typing in ELO, that didn’t work).

The player was very skippy (ever heard of buffering, guys?). Soooo distracting when listening to a song, and *skip* play *skip* play *skip*. Ugh.

I can flag albums as favorites, but finding them was a bit of a pain. Not very intuitive (did I mention how crappy the UI was, okay, though so).

I don’t know exactly why Napster folded into Rhapsody, but it didn’t result in a superior user experience (I’m sure the folks whose stock went up as a result are crying over this).

Goodbye, Napster. I’ll miss you.

Goodbye Rhapsody. This was great. We should do it again sometime. I’ll call you.

Deep Thoughts

There is no difference between strong and weak nuclear force, electromagnetic force and gravitational force. Force is force – and force is mass – and depends on the definition of “is”. At least, all this is true in three spatial dimensions, in which the speed of a photon in a vacuum is constant. Very exciting. There are also conditions under which, when time is constant and the speed of light varies that universes can be created and studied in an instant. In scenarios with multiple time dimensions, even more spatial dimensions are possible.

Our mathematics is too primitive to deal with the universe as it really is.

All structures are unstable. We have even been able to calculate the half-life of a proton (though it is longer than the currently speculated age of the experiment (universe)).

Eventually this experiment (universe) will end, the data will be collected, analyzed and the conclusion will be drawn: physics in three spatial dimensions with constant time is not sustainable. Everything eventually breaks down. The universe itself even “leaks” this truth through to its doomed inhabitants on occasion (to those who are open and listening). The ultimate truth is this: all are one.

How can it be that such primitive life can arrive at this marvelous conclusion?