Tuesday, June 24, 2008

WTF: Would This Function

I don't learn by reading. I learn by doing; seeing. To those who have read an entire language specification in one sitting, the answers to the questions that make me ask myself, "WTF (Would This Function)?" might be obvious. For me, in order to understand something I need to try to break it. I need to see what does what when what happens.

I write small test programs that reveal the dark truths behind what an API, be it language inherent or not, does. Only then I accept what is in front of me, promptly before moving on and forgetting everything I learned; only to exclaim in the distant future, "OH! I knew how that worked at one point, but I forgot. That would really be useful here."

It's time to share my discoveries with my future self; and possibly a small portion of the rest of the world. Here's to all those times I squint at my monitor, trying to predict how two things will interact, and ask myself "WTF?"

Friday, May 30, 2008

In Your Face: Refactoring

I know we are all guilty of ignoring an obvious refactoring at one point or another. Even when we truly know better. But I'm dumb-founded by how some people just seem to lack any concept of even the simplest encapsulation techniques.

Should the following not be almost instinctive by now to even the most average of developers? The following refactorings should not take more than a few minutes, if not less.

When you write the same clumsy string concatenation more than once, it's time to refactor.

// bad
String userChangesTableName = "user_" + userName + "_workchanges";

//good
public static String getUserChangesTableName(String userName)
{
return new StringBuffer(64).append("user_").append(userName).append("_workchanges").toString();
}
// look, comes with optimization even!


When you have comments excessively explaining code, it's time to refactor.
// bad
// if records are present, user work is dirty.
boolean userWorkDirty = simulationDAO.isRecordsExist(getUserChangesTableName(userName), simulationId)

//good
public boolean isUserWorkDirty(String userName, int simulationId)
{
return this.isRecordsExist(UserWorkBusiness.getUserChangesTableName(userName), simulationId);
// possibly even consider inlining this, or just renaming the original method, or see below.
}


When your method name doesn't make grammatical sense, it is time to refactor.

// bad
public boolean isRecordsExist(String tableName, int simulationId){ ... }

// good
public boolean isSimulationDataPresentInTable(String tableName, int simulationId) { ... }


If you can't rename your method to something that is descriptive of what it is doing/telling you in a way that makes grammatical sense, THEN IT IS DEFINITELY TIME TO REFACTOR.

Only this time, it might take you a little longer than a few minutes.

You should have put those extra few minutes in here and there to start off with. Look at all that time you would have saved.

Monday, May 19, 2008

You won't see THIS post HERE

I starting writing a post one night after I came home from the bar not quite in my right mind. Drinking will do that to you, or so I'm told. I tend to disagree; especially when I'm drunk. I may not know up from down, but I still think I'm the smartest man alive.

(As an aside; in my notes, from the post that you won't ever see, I have written down "smartest man" being an oxymoron. I'm not sure what I meant by that at the time, but I'm sure I'll try to explore it in a later post.)

Which brings me to the reason I'm posting this to begin with. Even now, this post about the post where I initially wanted to introduce the concept of MY BRAIN; it's scattered all over the place. The original displayed this factor much better; but you won't see THAT post here. What exactly is it displaying?

MY BRAIN is cyclical and cascading and... feedback ... ish? Regardless, my thought process; it works like this: Thought 1 begets Thought 2; Thought 2 begets Thought 3. It doesn't take a genius to figure out where that was going. Problems start to occur when Thought 11 relates to Thought 42, and Thought 18 counters Thought 41. It starts the feedback loop; or as I like to liken it to... You know those bumpers in your average pinball game? It hits one bumper and it starts flying off all 3 bumpers at a rapid rate. Picture that happening IN YOUR BRAIN. That's what your average conundrum feels like to me.

Monday, February 25, 2008

And So It Begins

I originally intended this to be a technical blog. It still might be at times. But for the most part, those weird thoughts that pop up randomly in the back of my head? Yeah, they'll be here.

Read at your own risk :D