Almost everyone else has posted a note about the meetup last night (Anita, Jack, Dennis, and Ram – but not Alex), so I’m a bit behind. We somehow manage to spend little of our time talking about blogging at these meetups but we still enjoy ourselves immensely. I’m disappointed that Anita (and Jack, I presume, though I didn’t get a chance to ask him) hasn’t been following Battlestar Galactica this season. The other folks I usually talk to about the show are still catching up from the first half of the season. I would love to know what they think about the Baltar-Six developments in the past few episodes.
Dennis — a.k.a. Orcmid — dropped by and left a comment this morning. He asked whether I had tried compiled JavaScript in .NET or not.
In the case of my strong typing epiphany, I wasn’t writing a script from scratch. Instead, I was modifying an existing script. Basically, I was changing it from doing a set of things once to doing that set of things multiple times, but with slight variations in the options. There was a bit of new logic in the program, but it was mostly refactoring. And it was the refactoring that got tough without strong typing as I converted repeating blocks of inline code into functions and corrected assumptions the original author had made (but that no longer were true). I had to change the scope of variables, create parameters, and document each new function. There were a lot of opportunities for typos.
One of the cardinal rules of modern programming is never to copy and paste. If you have two or more blocks of code that are identical, or even nearly identical, a subroutine should be made and then that subroutine called in place of the inline blocks of code. The reasoning is this: if the logic is duplicated in the code, if that logic is ever changed, it may not get picked up in all the places.
For example, pretend this pseudocode is a meaningful program:
if something equals "a" then
doFunctionOne(using "a")
doFunctionTwo(using "a")
doFunctionThree(using "a")
if something equals "b" then
doFunctionOne(using "b")
doFunctionTwo(using "b")
doFunctionThree(using "b")
There’s an example of copy-and-pasted code. It could easily be turned into a subroutine:
subroutine doFunctions(parameter)
doFunctionOne(parameter)
doFunctionTwo(parameter)
doFunctionThree(parameter)
And then the main part of the program would look like:
if something equals "a" then
doFunctions("a")
if something equals "b" then
doFunctions("b")
Or even simpler, in this trivial example:
doFunctions("a")
doFunctions("b")
Imagine this kind of change, but across several hundred lines of script code.
Getting back to Dennis’s question: I have done some things with JScript.NET, but it wouldn’t have helped here because I was modifying existing code rather than starting from scratch and I needed to work within the already-established parameters.
If I have to touch this bit of code again, for anything more than the most-trivial change, I’ll most likely invest the time to convert it to managed code. It will save me — or whoever comes along after me to maintain it — more time than I’ll spend doing the conversion.
Now, another question that the devil’s advocate in me has been asking: when is copy-and-paste better than factoring code into subroutines? Let’s forget about one-off, throw-away code. For stuff that’s meant to last and be maintained, are there any situations where copy-and-paste is preferable? Perhaps I will address that in the future or, even better, maybe some of you will provide answers in the comments.
Update: I’ve got to do something to remind myself to insert the tags on the posts before posting. I need to figure out how to hook Blogjet to remind me.
Tags: meetup, programming, refactoring
del.icio.us tags: meetup, programming, refactoring