How to manage long cods (programs)?

I began by writing separate bits of codes for each task I need for my Robo-cat.

Then I included all code pieces, one by one, in the same program.

Currently I have 3 tasks included and I already feel lost in long code, despite extensive comments and separators (comments consisting in equal signs).

I read the forum sticky which is useful. But I found it is not enough to have a clear code -line at the end.

I was thinking to use IDE tabs to simulate "subroutines" but I do not have a clear idea of what to place where and how are the compilers links made.

++++++

Questions are:

a) Is there any editing/structuring techniques (besides comments and comment titles) in IDE or outside IDE to make code logic more visible?

b) Is the C++ itself better equipped to handle long codes? Or perhaps I could refer to C++ itself to find some engineering techniques there? (I have only a very incipient idea about C++, stil thinking if deserves more learning besides basic ideas from Arduino).

c) Is there any reference/tutorial about tabs in IDE somewhere?

d) Is there a chance (from your experience) to find a free editor more versatile in terms of handling long codes? I do not look for technical performance or advanced debugging, I just want to better see the code structure.

Thank you!

Arduino code is C++

Is your code organized into functions (as in Planning and Implementing a Program) ? By breaking the code into several short single-purpose functions it will be much easier to develop, test, debug and maintain. That allows each function to be tested on its own.

If you have the code organized into functions then it may be a good idea to put all the functions for a particular part of the project (for example movement, or sensing) into a separate Tab.

I use the Geany editor for all my programming. And I have written a short Python program to compile and upload my programs using the Arduino IDE command-line .

...R

d) Notepad++
eclipse with Sloeber plugin

Robin2:
If you have the code organized into functions then it may be a good idea to put all the functions for a particular part of the project (for example movement, or sensing) into a separate Tab.

Indeed, my code is organized into functions (it is even planned and thought like that). However, since each function = code has been tested separately, when I put all together I have to pay attention to elements like libraries, which are to be included only once.

Things go even worse when coming to variables. I have to group them or to declare them locally, which is very very difficult when are many of them.

A pre-coding arrangement by modules (functions) could work, I thought. But I do not know sufficiently C++ to program outside the IDE.

And I didn't see such a pre-coding (engineering) operation anywhere, so looks like it is impossible. Or I didn't know where to look for. I know block diagrams and flowcharts, but it looks like nobody use them anymore. Perhaps it is something similar, more evolved, to be applied to C++ specifically? I do not know :-((

You should learn C++, there are zillions of tutorials and books.

Whandall:
d) Notepad++

I had a look on it. Looks very friendly. It is even Regex (Pearl) compatible! May I ask you whether it works flawlessly? I run Windows 7.

Whandall:
You should learn C++, there are zillions of tutorials and books.

Is it universal (standard)? I will learn C++, to many people say it is very good. It looks very unfamiliar to me, since structure, classes and alike generated a rich terminology which is far from the procedural language.

I'm using Notepad++ (on Win-7) for any bigger code or libraries, which are a pain in the a** to edit with the IDE.

It is not as comfortable as eclipse, but launches much faster and is pretty complete for an editor.

There is nothing special about C++, it's very much like Java with some syntactical and conceptual differences.

C++ runs on systems with very limited resources, Java needs to much RAM for small systems.

Whandall:
You should learn C++, there are zillions of tutorials and books.

I suspect by "learn C++" you mean "learn Object Oriented Programming". That's certainly not a bad idea (though I would prefer to use Ruby for that rather than C++ - pity it does not work on an Arduino).

But I'm not convinced that learning OOP automatically means that a person will visualize a programming problem in a way that makes the implementation easy (or at least straightforward). It is just as easy (maybe even easier) to produce a nightmare with OOP as it is without OOP.

My feeling, based particularly on Reply #3, is that the OP has not identified a systematic approach to the problem he is trying to deal with.

There was another very recent Thread in which the person has a huge amount of repetition amounting to about 400 lines of code that, I suspect, could be reduced to 20 or 30 lines plus a few arrays with data. Seeing a different approach to a problem probably has as much to do with experience as with learning.

...R

I think he should learn basic C first, there are some statements/questions in his posts
that make me believe that he has not enough trainig in the basics.

Grouping data and functionality is one of the main benefits of OOP,
which will lead to "make code logic more visible".

Picking block diagrams and flow-charts (techniques that I know) as example, they may help on a more structured approach. However, I found no reference on OOP done on such techniques. Perhaps are obsolete, I do not know. Or perhaps I was just less inspired in looking for something similar which does work on OOP.

Indeed - I have 2 distinct problems:

a) preparing to code (I will experiment on Notepad++, use existing capabilities like tabs in IDE)

b) do elegant coding in C++ (which seems to require OOP and C++ in extenso, not just IDE and Arduino alone).

======

I must confess that libraries makes me nervous, since they occupy a lot of spaces and I have no idea what they actually do (I know U can look inside, but they are optimized I guess, so for me like a beginner the code is less intuitive.

More libraries and more focused libraries may be useful for the beginners like me, but once moving forward, I found then difficult to manage. My guess is they turn extremely useful in the Arduino world, since they offer a rapid result, if the number of things to be done is low (3-4 maybe).

falexandru:
b) do elegant coding in C++ (which seems to require OOP and C++ in extenso, not just IDE and Arduino alone).

I am not aware of anything you can do in C++ that cannot be done with the Arduino IDE. Other editors just making the editing easier.

I must confess that libraries makes me nervous, since they occupy a lot of spaces

The compiler and linker only include the parts of any library that you actually use.

However that does not necessarily mean that libraries are written efficiently. And many of them are poorly documented so you can waste more time trying to figure them out than it would take to write the code without them. And, partly because the Arduino is small, it often happens that libraries conflict with each other.

...R

falexandru:
a) preparing to code (I will experiment on Notepad++, use existing capabilities like tabs in IDE)

I'd urge you to look at ATOM and its Arduino plugins

Well, in Newfoundland they've been handling long cods for ages.

@Robin
Very useful info!

Then the compiler brings in only parts of the library? Did I get it correctly? If so, why not writing "costumised" libraries? How difficult that may be?

I only want to allocate time to tasks that I can actually complete. So, it is extremely relevant for me at this stage to evaluate whether is worth spending time in learning how to write libraries.

@ Bulldog

Atom looks colorful :-)- so this helps following the code. Do you have experience in working with it?

Then the compiler brings in only parts of the library? Did I get it correctly?

Yes. For example, I sometimes include the entire Time library just to use one convenient definition. Since I don't call any functions or code, the compiler doesn't use it and it does not increase my memory usage by even a single byte.

falexandru:
I only want to allocate time to tasks that I can actually complete. So, it is extremely relevant for me at this stage to evaluate whether is worth spending time in learning how to write libraries.

learn to create a class...

C++ is C "with classes"

learning classes is fundamental to the language, but I'm sure that there are lots of hobbyists that have done this for years and never learned to write one.

falexandru:
@ Bulldog

Atom looks colorful :-)- so this helps following the code. Do you have experience in working with it?

Yes (except when it comes to members of the opposite sex) I'd only recommend those that I've tried... and liked.

Aside from a few lines of code for samples, I don't use the arduino IDE's editor. It has no talent for editing. ATOM's open source community continues to increase its power.

@aarg
Then, what happens when the Arduino is disconnected from the PC and goes independent? Does whole the library have to be in the memory of the Arduino itself in this case, or only the commands I included?

falexandru:
@aarg
Then, what happens when the Arduino is disconnected from the PC and goes independent? Does whole the library have to be in the memory of the Arduino itself in this case, or only the commands I included?

There is no difference in the operation of the Arduino's memory between being connected to a computer or not. The compiler looks at all the source code, but only includes the functions that are actually used.