Advantage of libraries

Dear All, From my extremely limited experience of Arduino, I understand that libraries make it easier for other users and keeps the program looking to doer, but if all you were interested in was the performance of the board, are there any other advantages of using a library rather than having all the code in the sketch itself. Many Thanks

but if all you were interested in was the performance of the board, are there any other advantages of using a library rather than having all the code in the sketch itself.

No. The way that the compiler works makes using a library or putting all the code in separate functions (or in loop()) exactly the same. But, good luck getting rid of all libraries. The HardwareSerial class, instanced as Serial, for instance, would be a difficult function to replicate in the sketch.

I recently took a library and changed it from a class to a set of local functions and saved a modest amount of code space.

If code space is an issue it is certainly worth taking a close look at the canned libraries and pruning or replacing as needed. They aren't necessarily designed with space efficiency as the first priority.

Writing flat code may be smaller in size, may be faster but is definitely more work. IMO.

Also, a well written class will perform no differently, there is no overhead associated with simply moving functions into a class, adding things like initializer lists in a constructor, or how you use the class decide this. If the code size drops after stripping the code into global functions, it is because the class was doing something that the global functions do not, or… wait for it… your code was inlined. To achieve the same result with a class, you can place the code ( function definitions ) somewhere your sketch can see, or a sneaky trick is to simply include the .cpp file instead of the .h.

Many Thanks All