While pursuing to improve the hardware of a project of mine, I'm trying to keep the code functional on all previous boards. Why? Because I still have many prototypes that work just fine and shouldn't go to waste.
Now it seems that I've successfully created my private little hell, which consists of lots of
#ifdef <version>
// do this
#else
// do that
#endif
spread across my code. It all works, but readability is converging to zero. This is especially annoying inside functions. Of course I could just #ifdef ... #endif whole functions, but then why not create completely separate files, which I don't want.
Is there a better way for this? Or should I just try to get rid of the prototypes (and maybe make a kid or some hacker happy) and forget about it?
You might look into a software versioning system that allow code branches; set up properly (not an easy task, for certain), this could make it easier to keep the code in sync with changes, while at the same time providing versioning. The "main players" you typically see mentioned are subversion, cvs, and of course, Visual Source Safe - though there are others, just like any software...
I've been able to solve similar problem by making classes more "fine grained" (a larger number of simpler / smaller classes with work divided into smaller bits).
Ah, the good old divide and conquer. I still hate that Nobody was able to properly explain why some algorithms scale as N*log(N) when using that approach (instead of say N²). Most of the time recursive programming was used as well. Shudder. And of course most forgot to set the condition when to terminate. BOOM
Classes... object orientation... currently I use none of this. And honestly I'm not a fan of having to solve inheritance issues and similar headaches
And I already use GIT to keep my head from spinning too much and at least keep a 'safe' revision history.
But my code already looks as messy as the 'arduino bootloader'... I've already abstracted away the bare hardware interaction, so at least the 'end-user' code in main/loop doesn't have to deal with it directly. I could change some options to be run-time configurable and remove their #ifdef-s, but for others that option just doesn't make any sense.
In my case, I could help more if I know some of the details. If you post an example or two of the most troublesome routines I'm willing to spend a bit of time looking them over.
Assuming that you don't need to update the code for previous hardware versions, I would just keep separate files for each hardware version. It may be crude, but it sure makes the code easier to read.
Yes, I like that substitution. Now all I need to add is a few comments with 'RTFHF'
But I doubt that inlining the SyncAndDelay function gives any noticeable improvements, as I'm dealing with delays in the ms range here. Nevertheless it works.
Now all I need to add is a few comments with 'RTFHF'
;D
But I doubt that inlining the SyncAndDelay function gives any noticeable improvements, as I'm dealing with delays in the ms range here.
The intent with inline wasn't to improve performance but to make it possible, if you'd like, to put the whole thing in a header file (I think that works).