Pat Gunn (dachte) wrote,
Pat Gunn

Musings on Refactoring

Code refactoring..

I love it. It's not that I like digging through rotten code, but I like making it cleaner, folding mostly redundant code paths into one, unifying code to read and write something so they can't fall out of whack, etc. It's the perfect task for a programmer with too much of an æsthetic sense (too much in the sense that I am not particularly fast at writing new code).

Unfortunately, people who haven't been programming for awhile, particularly those that haven't had experience with LISP/Scheme and other highly fluid languages, don't easily recognise the flow of a carefully structured program - like the self-organising bundles of cables in a machine room, they find it frustrating to work with code where one bit of code or data structure is metaphysically powerful enough to do a lot of non-explicit things. Of course, good documentation is essential - even the best programmers will spend some time scratching their head until they're pointed at the heart of things, either to know how to work within the scaffolding or extend that scaffolding themselves (even if it's to add a little special case).

Working with uglier code, I think the hardest part of judgement as a programmer is, given a long flow of code passing through several modules that does something, how to change that something to allow for a divergent function when the function is kinda-but-not-entirely related to the original purpose. One could create a completely separate code path, but that fails to take advantage of the similarity of purpose. One could insert hooks everywhere at the last minute, but that can get bulky and mar the conceptual cleanliness of the original code. Two meta-judgements suggest themselves:

  1. Produce the same end-result as if you were looking back and refactoring it
  2. Produce the same end-result as if you were designing it from scratch and none of the code were there yet
Real-world time constraints come into play - we don't always have the luxury to do the above. Also, it's unclear to what extent having a more organic codebase is bad - some design is essential in programming (informal, ideally), but a lot of the time intuition and best practices play just as big of a role because neat ideas in design don't always present themselves until one gets to a close examination of that conceptual element.

Duplication of some level of code function is probably okay too - preventing a mess of conditionals can preserve clarity.

Some people would take it as a sign that there remains something interesting to be said when we reach this point where we can't go further without data - give us heuristics, or give us some tradition for everyone to follow regardless of the data so everyone will be on the same page and we'll have a convergent set of standards for people when refactoring or programming it the first time. Others would say we should consider the heart of wisdom to be awareness of all these concerns combined with experience trying to meet them, and to go further would be a commitment to inexcellence. The difference between these perspectives fascinates me.

Last night gave me my worst migraine in over a year. I think the cause was some powdered chocolate I had yesterday - I don't understand the chocolate hypothesis because I occasionally eat small-to-moderate amounts of chocolate at other times in other forms, but hot cocoa seems to be distinct in its ability to mess me up. I was very tempted to call 911. Fortunately, it lasted only about five hours rather than multiple days, and waking up has led me to feel very mentally fresh (even as physically exhausted - this was one of those thrashing-about level ones) - I haven't had an easy time programming recently because of depression, but this morning has been very productive at work (I was even in my office before 9!).

Celestial Seasonings tea reminds me of many years ago living in Brecksville. The remaining boxes of tea bags I have may in fact be that old - given how seldom I make tea from them and that I've never to my recollection bought them, they may have survived the move from Brecksville, through the various dorms at OSU in Columbus, then the various apartments there, then the various apartments and offices here. It would be interesting to have a survey of what things have the most move-survivability - probably not gadgets (my One True Keyboards being an exception), usually not consumables (my tea being an exception). Pillows? Lamps? Teacups?

The weather is now terribly cold here.

Tags: programming

  • CMU, the First Amendment, and Indecent Exposure

    Earlier on my G+ stream, I commented on the matter of a CMU student who protested the Catholic church's coverup of sexual abuse by dressing as the…

  • Dilution

    I've been thinking about an issue that's been raised in the secular community; I'm not sure it's a good issue, nor a bad one. Let me lead up to it…

  • Commentary on the Human Rights Campaign

    I recently was pointed at a blog post suggesting people reject the Human Rights Campaign, a large social justice organisation that focuses on…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 1 comment