Arduino-PID-Library icon indicating copy to clipboard operation
Arduino-PID-Library copied to clipboard

proper c++-Style with many changes

Open orgua opened this issue 10 years ago • 2 comments

I implemented some compiler-optimizations and made a first attempt to get closer to recommended c++-style:

  • used constness wherever possible
  • cleaner constructor with proper initialized variables
  • replaced defines (preprocessor) by const type to get better compiler-errors
  • inlined the display-functions --> typically found in the header
  • made the class uncopyable to get no strange undefined behavior if someone tries it
  • controllerDirection and Mode have been int-type, but char will suffice

The changes are tested. My own code behaves the same and the examples still compile. Footprint of code & ram-usage is reduced.

orgua avatar Apr 28 '15 23:04 orgua

Well, enums are fine, but the given implementation in PR#21 breaks old code (tradeoff for better capsulation). You would get the same result by moving my "constants" into the class-public-area. Scott Meyers has done a good job explaining all the differences between defines and const/enums in "Effective C++ third edition" - Item 2. The essence is: defines are simple string-replacments in global namespace handled by the preprocessor. The later coming compiler has no clues of these replacements. By using consts you can operate in local namespaces, have to think about the type of your constant (less errors) and in case of an error you get a reference to the name of the constant involved. I have not searched for any ardu-libs using that style. Should that be an important point? Good Practice allows current and coming compilers to optimize code as needed, regarding size, speed, ramusage. 2: I have tested the new code in a heating-control of an espresso-machine and in quadcopter-code. so far i noticed no differences in the behavior.

orgua avatar May 13 '15 07:05 orgua

2015!! What happened to this? the library clearly needs to be refactored. @orgua there should be no difference other than probably more efficient code generation. Specially using floats instead of doubles.

arielbernal avatar Mar 21 '19 17:03 arielbernal