Dishonest Programming

by Jay Jonas

David Brady, from Javalobby News, their weekly newsletter, wrote a interesting article about Dishonest Programming. I took this excerpt, wrote it down and put it on my desk. If you are a developer, you should read it too.

Honesty Tip #1:
Name functions for what they really do. You’ve been taught to write foo munging code by drawing a box on the paper and labeling it fooMunger() and then writing the code. Okay, but when you’re done, go back and read your code. Is it really fooMunging code, or does it also do something else? If you’re spending 80% of your method checking error conditions, your method is clearly less concerned with munging foos than determining whether it should munge foos. Congratulations! — you’ve written an event handler. Rename it to onMungeFoo() or handleFooMungeRequest() or whatever, and extract the code that ONLY munges foos to mungeFoo().

Honesty Tip #2:
Let your yea mean yea and your nil mean nil. Don’t set up code to cleverly create side effects. EVERY effect is a side effect! Name your code for the intended side effect and document any other side effects. Don’t name your code for the unintended effect, especially if you think you’re cleverly creating an abstraction. (If the abstraction is really good AND other code will use it unchanged, you may create an abstracted method but you should still not use the abstraction in your other code. Write a wrapper method that explains your intended effect, like:

bool isLegalOverride(Foo a, Bar b) { return !areSimilar(a, b); }

Honesty Tip #3:
Don’t pee on my leg and tell me it’s raining, and don’t write a public accessor to a private member and tell me that member is still private. If an object is completely and utterly accessible from anywhere in the application, it’s a global variable.

Honesty Tip #4:
Don’t try to trick the compiler into giving you what you want. You’ll only trick your coworkers (and probably yourself). Tell the compiler what you want and write code that makes it give it to you.

Advertisements
Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: