Showing posts with label Style. Show all posts
Showing posts with label Style. Show all posts

Sunday, October 5, 2008

Refactoring Code

Last time I wrote a bit about huge projects. I glossed over it a bit.

Truth is, many big projects aren't fully or adequately spec'd out at the start, and come crunch time...let's just say that compromises are made.

So often you can look back at code and wonder who made that terrible design decision or atrocious wire layout. Why did this guy document his code so poorly? What the heck does that VI name mean? This page-long diagram could have been done with three nodes! And the list of criticisms goes on. Until you realize you're the one that wrote that VI.

Every now and then you get a great chance to rework an old peice of less-than-optimal code. I am fortunate enough right now to have that times two. I have the big project that's just about almost got the complete confidence of the customer. In the mean time, there's the immediate follow-on from the big project, which is a clone of the first with enhancements. At the same time, there's a separate project for a different group in the same company that intends to leverage a large part of the original big project.

This means that I'm making additions to the main project at the same time as borrowing sub VIs for another project. It's a really eye-opening opportunity. When you delete all but a dozen nodes from a ten-state state machine VI to replicate the functionality of that VI in another project, you really start to wonder about the tradeoff between overhead and flexibility.

Wednesday, August 6, 2008

Documentation

Everyone knows that LabVIEW is self-documenting (wink, wink).

There's really no such thing as "self-documenting". When you write code and think it makes perfect obvious sense, you're delusional. There needs to be documentation with the code for it to be as readable as possible. In LabVIEW, this includes VI Info and liberal use of free labels on the diagram.

I guess in a way, this makes LabVIEW partially self-documenting. If you use the tools available to you, it's easy to make your VIs easier to understand. And it doesn't take that much more time to do than leaving your code undocumented. Beyond that, it will save you and your successor countless hours of figuring out "what does this code do?"

I sheepishly admit that in my first days of LabVIEW programming, I thought it would be easier to leave the documenting to the end of the project. After all, code changes so much throughout development, to document it right when you write it would be a waste of time.

This foolish attitude changed drastically and dramatically the first time my first boss made me go back through my whole project at the end and add VI Info to all my VIs. Even though this was only about a hundred VIs, it was painful and mind-numbingly boring. Imagine this on a large scale thousand-VI project! I am glad to have learned that lesson the hard way, but not too hard.

Monday, July 28, 2008

Evolution of Programming

Today I was working for an old customer, on code that was written a long time ago. For a point of reference, the code was sized to fit into an 800x600 resolution. When's the last time you saw a monitor that incapable?

Now they have a modern 4294967296x4294967296 monitor like everyone else, and it gives me a lot more room to make a mess. But for the most part I stick within the original framework.

But what strikes me most when I work on this code is the way it was written. It is in the good old state machine architecture, pre-event structure style, with an array of booleans driving a case structure putting a string on a shift register to select the next state and there are a dozen shift registers carrying all sorts of crap all over the place. All neatly packed into 480000 tiny little pixels.

The thing is that I used the shift registers religiously, like if there's any data in the program at all, it must be on a shift register. Of course, half of them are used in maybe one other state, and the mass of shift registers serves to confuse the issue more than clarify or assist.

It's always like this, it seems, when you look at old code. At least for me. Whenever I look back at something I did, sometimes even a few weeks ago, I often can see a better way to do it. I think this is a tick mark in the column for design and prototyping a concept with a first draft of code with the intention of rewriting it once you've finished. You can't do it all the time, but often times a little time and distance brings clarity. And clarity brings better code.