As am experienced programmer I am often asked for advice by novices (or maybe I just give it?) on coding topics, or how to approach problems, especially related to Arduino topics. Herewith a collection of these pearls of wisdom …
- Architects may doodle before they put final pen to paper, but they don’t start building in a haphazard way and expect the building to keep standing. You should not underestimate the insights that you need in order to write an elegant solution to a problem, and these usually come from brain work, not fingers on keyboards.
- Think about the problem you are trying to solve and how you would solve it before you launch into coding. You may need to write small aspects of the code separately to clarify your thoughts. Print out results to check that what you expect is happening. Then the same things applied in more complex code won’t be as daunting.
- Design requires an understanding what you are trying to build. There are standard functions that you always need in real-time programming (open, close, read, write, update, flush, etc), so studying the standard system libraries that exist will give you a clue around what types of classes and methods you will need to implement.
- Stretching the building analogy, make sure that you have a good foundation for what you are doing. A separation between the code to handle the hardware and the application makes it easy to implement relatively sophisticated functions relying on solid underlying services.
- Don’t be afraid to initially implement and get a simple system working before building all the bells and whistles. Remember the simple system will provide a solid foundation for the rest. When building a library, work on the application while you work on the library.
- The first version of what you do will not be the final version. In fact, often you need to see the first version as a way to work out the final ‘real’ structure of what you are producing. Just like for any textual document, editing and revising code makes what you have better, more structured and more concise.
- Read other people’s code to see how things are done in standard ways. These standards make your life easier and make your code much more readable to others. Certainly in the case of Arduino coding, Finite State Machines are king as you can’t rely on multitasking constructs, so learn the standard ways these are implemented.
- You need to get your head around variables and variable types. A variable is like a pigeon hole that holds a value. That value can be many different types (like int, long, char). You can also define your own types (like uint8_t defined in the Arduino libraries) or others that are derived from or groupings of different types. Data is just bits – how you interpret those bits is up to you as a programmer.
- Use good code techniques. This means good and consistent formatting, lots of pertinent comments, etc. I always think of it as if I was writing prose rather than code. Remember then when you come back to the code in 12 or more month’s time, you will NOT remember much about what is there. If you code and comment for this situation, your code will be readable and maintainable for everyone.
- When changing code, implement and thoroughly test each feature as you go. If you find bugs they can only be in the small changes you have made since the last good baseline. In a similar vein, make sure that you use a versioning system (like git) so you can go back to a last known good version when all else fails.
What other advice would you give a novice? Add it in the comments below.