Archive for April, 2010

Refactoring For Dummies

Apr 25 2010 Published by Bryant under Programming

refactoring_dummies

I recently re-read Martin Fowler’s classic text on Refactoring. There is no doubt that it’s a foundational book with many key ideas, but the main problem is that it’s just not written in an approachable way.

When he starts talking about “Divergent Change” and “Parallel Inheritance Hierarchies”, he just loses a large part of his audience along with breaking a cardinal rule of Strunk & White – No $10 words.

As Steve Jobs has told us, people don’t read anymore. They scan. And in my opinion Fowler’s classic textbook on Refactoring is just not written for scanning. It’s written to be studied like a college text (which may explain why it’s only in hardback and has a built-in bookmark).

On the other end of the spectrum, we have the Dummies and Head First books. They are fun and strive to ensure that you can scan and remember what you read rather than just transmit a ton of dense information. They avoid the wall of text.

After reading Refactoring for roughly the third time and finally remembering a quarter of it, I’d like to present to you my simplified summary – “Refactoring for Dummies” if you will:

1.  Refactoring is Your Civic Duty

In order to write maintainable software, you must refactor. It’s easy to overlook quality in the the rush to add new features, but let me ask you — Do you write perfect code the first time? If you answered “yes” you’re lying so take the test again.

If you answered “no” then you need to refactor. It’s your civic duty and a hallmark of what it means to write software as a professional. Not only that but for any code base that has a long lifespan, the costs of refactoring to more maintainable code will pay for themselves in the long run.

2. Tests Allow You To Refactor

The goal of refactoring is to improve the code quality without changing the behavior. So the question becomes, how do you know all the expected behavior of your code and how do you know that you didn’t break it?

For small code bases, you can probably keep it all in your head, but for large code bases the best way is to have tests that you can run. This will easily show you whether you have inadvertently broken something with all your fancy refactoring.

3. Duplicate Code is Evil

If you have code that is the same (or nearly the same), turn it into a method that both locations can use. For obvious reasons, this is a huge win for maintenance. Also, you get the added benefit of making the code clearer as long as you choose a method name that is specific. Instead of having an unnamed block of code, you name a named re-usable chunk with a descriptive name.

4. Smaller is Better

In the world of maintainability, smaller is often better. It’s easier to maintain many small (well-named) units rather than one large one. We learned that the hard way back in the monolithic vs. modular code battles.

So, when refactoring your code, you should strive to achieve the following:

  • Smaller Methods: If you have to scroll through pages and pages of code to get to the end of a method, it’s doing too much and is too big.
  • Less Parameters: Have you ever tried to call a method with 14 parameters? Not fun. The less parameters you have, the better. If you really need to pass a lot of parameters, consider bundling them into some kind of object.
  • Smaller Classes: Does your class have 300 methods? Then It’s probably doing too much and does not accurately model one entity. Consider breaking it into smaller, more focused, classes.
  • Smaller IF Blocks: Does your code have gigantic, deeply nested IF statements?Sad to say I have fallen victim to this one. The better solution is to either employ helper methods or guarded blocks. In other words, test the exception rather than the normal case and immediately return if it’s true.

I’m just scratching the surface of what Fowler covers in his book, but I think this is the bare essentials.

And for those of you stout-hearted enough to read Fowler cover-to-cover, I’m impressed! May I recommend War and Peace next?

View Comments

Is The End of Flash Near?

Apr 19 2010 Published by Bryant under Programming

Adobe-Flash-Player-Icon

The powers that be are moving strongly against Adobe Flash. Will it make it remain a key player on the internet or soon be as antiquated as a Java Applet?

Flash is being attacked on multiple fronts: Apple, Usability, Silverlight and HTML 5. Can it survive? Let’s consider each of its threats.

Apple

With Apple’s most recent decision to ban all cross-compilers on the iPhone, they have royally screwed Abobe. The iPhone cross-compiler was the key feature of Flash CS 5 and and now it’s worthless.

What’s strange about this one is that users genuinely want Flash on their phone. So how long can Apple keep flash off the their devices? The answer seems to be indefinitely.

It’s good to be the king of the platform. As flash has learned the hard way, don’t piss off the king.

Usability

The usability gurus have long had it out for Flash. Jakob Nielsen (ie – Mr. Usability) had this to say about Flash in an article entitled 99% bad:

About 99% of the time, the presence of Flash on a website constitutes a usability disease. Although there are rare occurrences of good Flash design (it even adds value on occasion), the use of Flash typically lowers usability. In most cases, we would be better off if these multimedia objects were removed.

Flash tends to degrade websites for three reasons: it encourages design abuse, it breaks with the Web’s fundamental interaction principles, and it distracts attention from the site’s core value.

Ouch! At least flash still has that 1% going for it!

And did I mention that he wrote the article in October of 2000? Yes, he has a long, long hatred of flash.

Silverlight

With the advent of Silverlight, Microsoft is now gunning for them, by creating a Flash clone that’s better architected and surely much more likely to attract the hearts and minds of existing Microsoft developers – of which there are many.

Silverlight’s main hurdle is getting installed on all the browsers, but Microsoft still has such a huge slice of the browser market. And they can go a long way towards solving this by pushing Silverlight with a windows update.

It’s unclear what role Silverlight will play as it’s still so young, but Microsoft has a long history of slowly overtaking competing products after starting from way behind (Anyone still using Word Perfect?).

If nothing else, they will carve away at the audience of developers and users who want interactive content on the web. Who knows how many of the current crop of C# developers would be Java developers if C# did not exist?

HTML5

HTML5 may be the largest threat of all to flash. Not only does HTML5 have some of the same features as flash, but it has the trump card of being a web standard rather than locked into one vendor.

It just seems to fit the open nature of the web better. And in maybe a sign of the times, YouTube is transitioning to HTML5 video (away from Flash).

Conclusion

So what does this all mean for Flash? The future does not look good, with all of the barbarians at the gate, but they could still innovate their way out of this dilemma by providing something beyond what it available in Silverlight or HTML5.

And if all else fails, at least they’ll always have Club Penguin.

abigu-club_penguin

View Comments

First Look: Top 3 Features of Visual Studio 2010

Apr 13 2010 Published by Bryant under Programming

VS2010

Visual Studio 2010, Microsoft’s flagship development environment, shipped yesterday so (of course) I *had* to download it from MSDN and check it out.

The Install Process

The download was quick, but the install took a while. Not sure if this is because I did it on an XP machine or what, but it seemed like it had to install a lot of prerequisites. Also, it rebooted twice (ugh!). All that said, everything appeared to install properly and it did nothing to harm my VS2008 install.

The First Day

I spent the first day working in VS2010 while editing an existing asp.net 3.5 web app. That’s likely what I’ll be working on for the foreseeable future as I don’t see anyone jumping immediately to asp.net 4. Most large companies will at least wait for the first service pack. So I wanted to see what the experience was like opening existing 3.5 apps.

After a day of use, three new features stood out to me: Multi-monitor Support, Quick File Navigation, and View Call Hierarchy.

1. Multi-monitor Support

multimonitor Photo Credits: ScottGu Blog

It’s a fact that most developers have multiple monitors so wouldn’t it be great if our tools actually supported them? Now VS.NET does! In the past, if you wanted to look at two pieces of code side-by-side across monitors you had to launch two instances of VS.NET.

With VS2010, it’s as simple as dragging the tab file to the other monitor (much like the experience in Google Chrome). You can also drag toolbars to different monitors. It’s very freeing and a wonderful way to work.

2. Quick Navigation

navigateTo Photo Credits: ScottGu Blog

As has been stated many times by the vi and emacs crowd, navigating through text files is what programmers spend the majority of their time doing, and therefore it should be efficient.

Geoffrey Grosenbach (aka TopFunky) from peepcode says it best:

For most programming tasks, editing involves opening several files at once and switching between them (e.g. HTML/JavaScript/CSS).

And yet most text editors offload these important tasks to the operating system or implement half-hearted custom solutions. Even worse, third-party plugins frequently implement file-jumping functionality for different frameworks with unique, cryptic key mappings (e.g. between implementation and test or controller and view template).

This is a core task! Editors should approach it with the same innovation and care they spend on basic text entry.

In the past, this could not be done without a nice plug-in like Resharper. Now VS2010 natively supports it. All you do is press ctrl+, and a nice dialog pops up which allows you to type any part of a file name and jump directly to that file.

This is a feature I’ll use every day. I love to work in full screen mode with all the windows unpinned and this just makes it easier – bye, bye solution explorer!

3. View Call Hierarchy

callhiearchy Photo Credits: ScottGu Blog

When you are looking at a piece of code, it’s often handy to walk through it without attaching a debugger. It’s nice to see the call stack and understand who will call your function and what path they will take to calling it.

Perhaps you’d like to trace the flow of a variable from the entry point of the code to a deep class nested under many methods. Now, you can visually see that without even needing to run your code. You just select your method and choose “View Call Hiearchy”. VS2010 will display a list of everyone that calls your function and everyone that it calls so you can easily navigate up and down the call stack.

Conclusion

While I don’t think there is any “must-have” feature, VS2010 adds lot of “nice-to-haves” that will make your coding more productive. All of the little niceties add up to more pleasurable development experience, and given the amount of time that we spend in our development environments, shouldn’t it be as pleasurable as possible?

computer_problems
Photo Credits: xkcd

View Comments

Next »