BEGINNERS: Re-using code. WHY, WHAT, HOW

Reusing code snippets is and can be very productive,
BUT -
Cutting & Pasting in itself doesn’t work.

For using existing code - especially if it’s written by someone else,
You must understand WHY you’re using it !
If it’s because you lack experience, you’re lazy, or don’t know how to solve the problem, that’s a recipe for disaster later in the project. Read on.

WHAT does the existing code do ?
Do you really understand, or does the program you copied it from do something you want?
Are you sure this piece of code will work standalone without the othe parts you left behind?

HOW does it work ?
Do you know what each variable, function and I/O operation does?
If not, then stop now, and figure it out.
Software is not magic... it’s completely logical - which you must be too.
Yes, there are some hidden parts e.g. libraries, but this makes it even more important that you don’t blindly expect that everyone else is perfect.

There are plenty of bad coders and libraries out there. Don’t copy them and become another one !

And remember, rarely will good software run on bad hardware.

I could not agree more!!

I'm a "tinkerer" myself. I have to many little bits of information to know all about one subject.
Jack of all traits kind of thing.

I've been playing with Arduino's for a few odd years and you're statement is spot on.
I have never written my own complete code to try and make something work.. NEVER!
There is always a copied part somewhere.

But, I make a point of understanding what I copy and why.
And everyone else is definitely not perfect.
I have had my fair share of debugging due to faulty copied code.

It's thanks to forums like this one, and a whole lot of how-to videos, that I have built all the toys I have.

Thanks to all the Info donors. And the creators of Arduino.

What recommendation would you make to someone that wants to test out code snippets? How can the Arduino environment be segregated for testing so that the "hidden" libraries and stuff do not impact sketches that otherwise work fine?

Thanks for your advice

Bill

That’s a really good question...!

Libraries are only ‘hidden’ to the extent they won’t affect your code unless you actually invoke or call them.
They are usually provided with sample code to demonstrate their basic function, so that really that shouldn’t be a problem however there’s no harm in ‘trying’ then posting your code - along with what you didn’t understand.

The underlying concept you’re looking for is writing & testing code in isolation, then stitching the pieces together. A good starting point is to write your ‘functional’ code as functions.

These functions will generally stand alone, and can be put aside for use when needed, but managing libraries will still require some practice and understanding of what each library needs.