2

Am am working on a small project for a USB device firmware.

I started from a working example, and am gradually adding new functionality and testing things. However, the new functionality is not all intended to be used in the final project. I am writing it to learn how to do things on this concrete platform, and fixing unexpected problems in the process (for example, on of my test PCs had non-functional USB ports).

What should I do with the old code? The code is version controlled, of course. I see two possibilities:

  • Change old code into new one. An example here would be, when moving from having a Loopback over the UART (for testing purposes), and moving to more sensible use of the UART, change the DMA code to read not from the input of the UART, but from the CPU buffer, that is to be sent out.
  • Use a bunch of #if statements to make the source code configurable, so that any version of the code can be compiled by only providing the correct #define.

If the latter approach is recommended, where is the dividing line? I can't keep every line of code that I write, hiding it behind an #if.

Vorac
  • 7,073
  • 7
  • 38
  • 58
  • 9
    if you use source control then just let it live there and provide a reference to the relevant revision in the bugtracker – ratchet freak Dec 04 '13 at 10:14
  • Agree with ratchet freak. Label each version and provide clear notes for each version - what was added, what bugs where fixed, etc. Mothballing the prototypes just clutters the source repo. – andy256 Dec 04 '13 at 10:19
  • You should post that as an answer @ratchetfreak. – yannis Dec 04 '13 at 10:31
  • I don't think it is a duplicate to the question @gnat linked to, but they are definitely related. – Bart van Ingen Schenau Dec 04 '13 at 18:31
  • [Ruthlessly delete code that isn't being used. That's why we have source control systems!](http://www.codinghorror.com/blog/2006/05/code-smells.html) – Vorac Dec 05 '13 at 15:57

1 Answers1

5

If you are using source control then let it live in there.

To find it more easily later you should provide a reference to the revision with the prototype in the relevant bug report where the enhancement is discussed.

gnat
  • 21,442
  • 29
  • 112
  • 288
ratchet freak
  • 25,706
  • 2
  • 62
  • 97
  • In the example with the UART, suppose that I have working UART Loopback. Now I change that to the UART pumping useful data out of RAM. This happens not to work. I suppose I am supposed to checkout the previous version and try it, to ensure that the problem is in the new changes, and not in *cosmic rays* e.g. hardware problem with the PCB. **Why** is this better than having both variants in the code and reconfiguring the build with e.g. `cmake`? – Vorac Dec 04 '13 at 10:53
  • because here the HEAD revision only holds code currently being used, while with `#ifdef`s the other code can get out of date just like comments and documentation (a fix/workaround found in the working version is not applied to the prototype code) – ratchet freak Dec 04 '13 at 11:11
  • @Vorac This is probably the kind of situation where you'd want to make a new branch to develop/prototype new functionality and merge from/to as appropriate. The feasibility of that might depend on what source control you're using though. – Evicatos Dec 05 '13 at 20:04