When is big enough to consider splitting a sketch up

Status
Not open for further replies.

DaQue

Well-known member
I've reached 300 lines of code plus a header file with definitions etc. So when do you all start adding/moving code to .cpp files?
 
Non sequitur for this forum. Read this stuff then go to stack overflow.

Ranade and Nash, The Elements of C Programming Style.
Steve McConnell, Code Complete.
 
In Arduino IDE you can use tabs. My current project has 12 tabs with each around 1000 lines. I'm more at a point to switch to another editor :p .
 
300 lines?

Lets see, the rs6000.c file in the PowerPC compiler that I work on is 42,083 lines. Of course it has been modified by many people over the last 25 years. :cool:

Any way it is up to you. Do you find it hard to work in a large file? Then split it up. Do you find it hard to keep track of things in multiple files, then keep it in one file.

Generally, what is best to do is to try and break things by function/class. And move things into separate libraries, so that you can reuse the useful bits in other sketches. For example, I have all of the neopixel stuff in several libraries (one for the container classes, a separate library for each separate pattern, a library that picks patterns to do at random, and some other glue/configuration libraries). Thus when I add a new sketch (such as the uncanny eyes sketch), I can easily add the neopixel support in the new sketch without having to replicate too much stuff.
 
1000 per tab! Ok I'm no where close but I do hate the editor. I also have atmel studio working and compile in both. It's much better when debuging. You can collapse functions etc... My last post on the subject as it doesn't fit the forum here.
 
You can use pretty much any external editor you want by configuring it in the settings of Arduino. That way you combine the advantages of Arduino (toolchain integration, Teensyduino, uploading) with the usability of another editor. Some people also use Eclipse with the Arduino plugin.
 
NotePad++ also works and there are customization schemes for C++ and maybe Arduino. It also works well just for viewing library .h and .cpp files.
 
For most of my editing I use Sublime Text 3. I can use it do builds as well. but typically I still use the Arduino IDE to do the builds.
 
A better question than when to split code into other files is *how* you go about splitting it up.

I've seen many open source projects where the code was scattered across hundreds of files and several levels of directory hierarchy. In fact, the Arduino IDE source code has become structured that way in recent years. The LUFA library for AVR chips is another example. I'm sure each individual split decision made great sense in the moment for each person deciding to do it, but over the long term it can make a large code base far more difficult to understand.

Then again, I've heard a couple of times from people who were quite displeased by my own personal code structure decisions....
 
When I show my code to my 'higher level' colleagues, the first thing they always say is 'doesn't that language have namespaces?' :) .

I try to organise according to functionality. I have a tab for Ethernet code, one for SD, one for analog etc. However my two beefs with Arduino IDE are:
- You can't declare global variables anywhere but the tab that contains your main loop(), overloading this tab when you have a lot of variables and a complex main loop.
- When detecting errors during code verifying, the IDE often highlights code in the wrong tab.
 
However ... You can't declare global variables anywhere but the tab that contains your main loop()

huh ? ... once a sketch gets large I use main tab for intro text + global declarations, second tab for setup(), third for loop(), and 4,5,6 etc for functions or device specific code. You can add local declarations in any tab (including before/between functions)

This works fine for me, as I much prefer multiple tabs to one a looooong main tab.

I use Notepad++ for formatted viewing of ino, h or cpp files. I may move to this for coding too one day, but the IDE has been fine for me so far.

And TyQt for much improved teensy uploading, Serial output, resetting etc

There is one hassle using tabs that more experienced coders may know how to avoid: sometimes if I make a small coding error (like omitting a semi-colon at line end) I start getting out of scope errors that hide my small error, and are hard to find. The workaround is simply to add prototypes for every function to main tab, so all the scope errors dissapear, then the remaining error messages will point me to my simple error.
 
Last edited:
I have been using visual micro ( http://www.visualmicro.com/ ) and it's been working well. Seems to compile faster. A lot faster. It's a paid plugin for Visual studio. As I come from Borland C++ builder and Visual Studio, it makes my life a lot easier knowing the IDE.

Granted, I have not dug deeply into the options regarding linking, parsing, compiling, but so far it's been working very well.
 
Status
Not open for further replies.
Back
Top