Krista's Coding Corner

09.02.2013

Spaghetti code, part 2 -how to fix it?

Had to chop my post into two ;)

I'll take the picture from previous post as an example:

Here are some notes about what you should at least notice:

That picture is just mess but it could be a lot worse. In these cases you probably have to write everything again, or at least that is the easiest solution. Using variables would make this code A LOT cleaner, and it would prevent problems that come from writing to an output from many places.

First, you can chop those long lines into smaller ones with variables (that are then used as inputs and outputs for different parts).

Second, make sure that you don't write into variables or outputs in different places. Sure in some specific case that is necessary but mostly it means trouble as you can.t know what is actually in the variables. You can of course use variables and inputs in many places but writing stuff is much more sensitive transaction. Always know what affects what and how.

Third, if you have function blocks, your lines shouldn't go backwards, it makes things messy. And lines crossing each other also decreases the readability of the code.

Fourth, lines should always go somewhere and blocks should have a purpose. If they don't, something is going wrong :)

Then, debug, debug, debug.

blog comments powered by Disqus