Why put something in a class and use it only once?

One strange thing I could never understand about you programmers who say you program for Arduino is when you make a library why do you always feel the need to put everything in a class only to use the class once in your program. I hardly ever use libraries I just read the datasheet and and write my own code that does what I need it to do and only what I need it to do. I have found by doing this my program takes less flash spaces and my code is more efficient.

I may only use the lib once in a program BUT I then use the lib in many programs. What do you think Serial (for example is) Why keep writing the same code over and over!

Mark

Can you give an example ?

Putting things that logically belongs to each other in a class has several advantages.
For me it encapsulates functionality which makes programming (abstract level) and debugging easier.

Here is a good example http://www.seeedstudio.com/wiki/File:TFT_Touch_Shield_libraries.zip it is coded in such a way that you can have only one tft screen because it uses port manipulation to constant addresses. So there is no point to put it in class because it will only be used once. Also on the Arduino uno which this library supports you don't have enough pins to have more than one tft screen hooked up. You would need a demultiplexer or io expander or shift register which the library does not support and I think that the user of the library should add those things not the library author. The print functions are in class because they can be used more than once. The print functions are inherited into the Serial class which this does make sense. Other classes can re-use the print functions if needed. What I am talking about is libraries that will be used only once and cannot be re-used. Also high abstract level is not always a good thing if there are performance costs of having it. If the performance is greater than or equal to a lower abstract design than please do it but otherwise don't. Also you don't have to put something in a class do re-use it in different programs. Just put related functions in different files. However if you go that route I recommend you enable link time optimizations. I use avr-gcc directly by the way I don't use the arduino "ide". In the arduino "ide" you don't have control over the CFLAGS.

Still the TFT class encapsulates all the logic of the TFT so that it easier to search for problems in the code.
divide et impera - old latin saying

and yes a Class can always be coded otherwise e.g. a cpp file
and yes optimizations can kill any good design principle

robtillaart:
Still the TFT class encapsulates all the logic of the TFT so that it easier to search for problems in the code.
divide et impera - old latin saying

and yes a Class can always be coded otherwise e.g. a cpp file
and yes optimizations can kill any good design principle

You can also just create C functions that do the same thing this is what I did with that library arduino-camera-tft/TFT.c at master · ComputerNerd/arduino-camera-tft · GitHub

I'm not sure this is a very productive thread.

I think the answer is "do it whatever way you prefer as long as it works and is reasonably easy to understand".

Some people seem to be addicted to classes and others are not.

In my opinion the real shortcoming is the fact that authors of open source software rarely spend much time writing documentation.

...R