Notifications
Clear all

[Closed] Object Oriented techniques - Example 1 - "Hello Class"

302 Posts
10 Users
229 Likes
19 K Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6893
 

@frogandtoad I doubt that very much, after a 20 year break from coding, my C is barely ok, my hour meter for C++ is in the low doublr digits and I don't have enough time left to get it up into at least mid 4 digits to be confident in my abilities.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @zander

@frogandtoad I doubt that very much, after a 20 year break from coding, my C is barely ok, my hour meter for C++ is in the low doublr digits and I don't have enough time left to get it up into at least mid 4 digits to be confident in my abilities.

I like to think of it this way:

Programming is not so much about the code and syntax, it's more about understanding programming concepts such as data structures, flow, iteration etc...  If one understands these programming design concepts, then the same concepts can be easily applied, and are applicable to just about any language out there.

I haven't ridden a bicycle for 20 years, but I'm pretty sure that I still can 😉

Cheers


   
Inst-Tech and Ron reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @frogandtoad

@zander

Posted by: @zander

@frogandtoad I doubt that very much, after a 20 year break from coding, my C is barely ok, my hour meter for C++ is in the low doublr digits and I don't have enough time left to get it up into at least mid 4 digits to be confident in my abilities.

I like to think of it this way:

Programming is not so much about the code and syntax, it's more about understanding programming concepts such as data structures, flow, iteration etc...  If one understands these programming design concepts, then the same concepts can be easily applied, and are applicable to just about any language out there.

I haven't ridden a bicycle for 20 years, but I'm pretty sure that I still can 😉

Cheers

Ron... life is a learning experience - It's never too late to jump back on that bike mate - I know you can do it! 🙂

Here is a beautiful example of what OOP can do for you in C++.

Procedural Version

OOP Version

Bill Earl is a respectable guru for Adafruit, and IMO, he has provided some of the best tutorials for Arduino going around!

Lets analyse and discuss the procedural vs OOP version and see what people think!

Cheers


   
Inst-Tech reacted
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @frogandtoad

@zander

Posted by: @frogandtoad

@zander

Posted by: @zander

@frogandtoad I doubt that very much, after a 20 year break from coding, my C is barely ok, my hour meter for C++ is in the low doublr digits and I don't have enough time left to get it up into at least mid 4 digits to be confident in my abilities.

I like to think of it this way:

Programming is not so much about the code and syntax, it's more about understanding programming concepts such as data structures, flow, iteration etc...  If one understands these programming design concepts, then the same concepts can be easily applied, and are applicable to just about any language out there.

I haven't ridden a bicycle for 20 years, but I'm pretty sure that I still can 😉

Cheers

Ron... life is a learning experience - It's never too late to jump back on that bike mate - I know you can do it! 🙂

Here is a beautiful example of what OOP can do for you in C++.

Procedural Version

OOP Version

Bill Earl is a respectable guru for Adafruit, and IMO, he has provided some of the best tutorials for Arduino going around!

Lets analyse and discuss the procedural vs OOP version and see what people think!

Cheers

Btw... once you (or any other member have had a look at this fantastic tutorial), please feel free to ask any questions about it, and also... see what you can improve in this code - What did we learn about initialization???

Cheers 😉


   
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6893
 

@frogandtoad Agreed 100%, but C++ has a VERY different syntax and when I looked up the current version they have 'improved' it even more. Since I am not likely to have much need for it, I will not spend the little time I have remaining pursuing this language.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Inst-Tech reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6893
 

@frogandtoad I took a quick look and remain not convinced. The procedural code is waaaay too verbose, and I can't foillow the C++ version but do see a lot of lines of code that I think are just overhead. Sorry, I REALLY have no interest in re-learning C++. The first time I tried I did see some concepts that I applied to my sbset G C to advantage but most of the concepts are of the 'solutions looking for problems' types.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6893
 

@frogandtoad Sorry, I am not able to look at the code very much. I set the coding standards at IBM and DJ and hate looking at non-standard code. If I 'borrow' a snippet from someone I will completely re-write it in my style and naming conventions. Also remember I think I told you I am on the spectrum so anything outside my 'gift' area is almost impossible and at 80 I am not going to struggle if not needed. That being said I strongly support @frogandtoad in his efforts but I also strongly believe any formal/semi-formal teaching needs it's own 'DBWS/Languages/C++/Here is how to .....'

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
frogandtoad and Biny reacted
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@zander Yeh, one of the things I disliked about "commercial" C++ back in the day was that it emerged during a naming fad that used really really long and complex naming standards.

It was my impression that the pendulum had swung too far. Thankfully the modern IDE with its context sensitivity and readily available info (scope, type, yada yada) removed the need for names that had the equivalent of four or five "data fields" encoded in the name.

I had the same reaction as you to that - made the code very unreadable.

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
frogandtoad reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6893
 

@binaryrhyme I think you misunderstood me. My naming standards were not like that. Most of my interest was in the naming of source files, procedure and functions. Of course being IBM it extended to reports, data files, JCL. When I moved to subset G it got better, you were allowed 32 chars instead of 8 so the standard name could be followed by a descriptibve name.

We did play with the data element naming stuff but only for the data dictionary/IMS stuff that I was not very involved with.

Also, my focus was on the maintenance aspects since I came from a hardware field engineer background. As I was the only person in a 300 person software shop that read core dumps, having every program named main was abhorrent to me.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny reacted
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 
Posted by: @frogandtoad

Btw... once you (or any other member have had a look at this fantastic tutorial), please feel free to ask any questions about it, and also... see what you can improve in this code

I had a look at the example and its a good tutorial on how one may use classes.  However, at the end of the day all the code was doing was to flash 3 led's in. what was termed a multitasking way, blinking the LED's at different rates and turning some motors.   

So, not necessarily 'improving' the code, how would it be done without classes?  As I've no spare motors I will just show blinking 3 LED's independently in bog standard Arduino code.  This can be compared to the final class code as shown in the tutorial you refer to (albeit ignoring the motor code).  Note some on board LED's come on when associated pin is high, and some come on when pin is low, so the highs and lows may be the wrong way round for the board I used.  Also I've used magic numbers relating to the on / off times buried in the individual functions with is not good, but hey, its only a quick example.

Maybe this illustrates that whilst the use of classes can be of value especially for the larger sized code, but for the normal smaller Arduino sketches maybe the standard Arduino way can be just as good, maybe more readable?   I think I know what @frogandtoad would say, for for any others, you pays your money and you make your choices 😀 

 

#define LED_1 11
#define LED_2 12
#define LED_3 13

void setup() {

    pinMode(LED_1, OUTPUT);
    pinMode(LED_2, OUTPUT);
    pinMode(LED_3, OUTPUT); 

    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
}

void loop() {
    blink_LED_1();
    blink_LED_2();
    blink_LED_3();

}

void blink_LED_1() {
    static unsigned long LED_1_Millis = millis();
    // flash on every 123 and off for 400 ms
    // if LED is on, has 123 ms passed, if so go high
    if( digitalRead(LED_1) == LOW) {
        if (millis() - LED_1_Millis > 123) {
            digitalWrite(LED_1, HIGH);
            LED_1_Millis = millis();
        }
    }
    // if LED is off, has 400 ms passed, if so time for a low 
    else {
        if (millis() - LED_1_Millis > 400) {
        digitalWrite(LED_1, LOW);
        LED_1_Millis = millis();
        } 
     }
}

void blink_LED_2() {
    static unsigned long LED_2_Millis = millis();
    // flash on every 300 and off for 300 ms
    // if LED is on, has 300 ms passed, if so go high
    if( digitalRead(LED_2) == LOW) {
        if (millis() - LED_2_Millis > 300) {
            digitalWrite(LED_2, HIGH);
            LED_2_Millis = millis();
           }
     }
    // if LED is off, has 300 ms passed, if so time for a low 
    else {
        if (millis() - LED_2_Millis > 300) {
            digitalWrite(LED_2, LOW);
            LED_2_Millis = millis();
            } 
     }
}

void blink_LED_3() {
    static unsigned long LED_3_Millis = millis();
    // flash on every 200 and off for 222 ms
    // if LED is on, has 200 ms passed, if so go high
    if( digitalRead(LED_3) == LOW) {
        if (millis() - LED_3_Millis > 200) {
            digitalWrite(LED_3, HIGH);
            LED_3_Millis = millis();
        }
    }
    // if LED is off, has 222 ms passed, if so time for a low 
    else {
        if (millis() - LED_3_Millis > 222) {
            digitalWrite(LED_3, LOW);
            LED_3_Millis = millis();
        } 
    }
}
This post was modified 2 years ago 2 times by byron

   
Biny and Inst-Tech reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6893
 

@byron After a quick look at that code I bet it could be reduced quite a bit with a called procedure with I think 3 parameters.

I have noticed so far that few arduino examples use functions/procedures, but just copy/paste code. I guess my days of hand writing code to give to a keypuncher made me one way while youngsters with copy/paste in their blood produce a different kind of code.

Also, easier to understand when smaller, think divide and conquer.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny and Inst-Tech reacted
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1121
 
Posted by: @zander

After a quick look at that code I bet it could be reduced quite a bit with a called procedure with I think 3 parameters.

Maybe, but just one function may be rather unreadable, and the merit in being more verbose is that one can come back to read and understand one's code with greater ease.  I'm all into the zen of python coding. 😀 

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.

   
Biny and Inst-Tech reacted
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6893
 

@byron I disagree, but that may be due to the way my autistic brain works. When I read code, I am comprehending a small parft then Excluding (my fav editor feature) it from view. Obviously the ultimate Exclude is to replace 6 or 8 lines by a procedure call or function reference.

Also, I spent many years reading other people's code in order to find their bugs so again, my requirements may have been slightly different.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Biny and Inst-Tech reacted
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

@frogandtoad Excellent tutorial of classes and constructors..!!

I'm sure I'll find a way to use those oop's in the future, as a matter of fact, I think my endeavor to create a PID control could use this type of coding.. 

Thanks for the links..

regards,

LouisR

LouisR


   
frogandtoad and Biny reacted
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 
Posted by: @byron

[snipt]

but just one function may be rather unreadable, and the merit in being more verbose is that one can come back to read and understand one's code with greater ease.  I'm all into the zen of python coding. 😀 

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.

If you call subroutines meaningless names like sub1_A2(int a, int b), then I'd agree. But if you use meaningful names for the routine and the parameters like findCommonDenominatorFor(int a, int b) or calculateAreaOfSphere( float radius ) then I think your argument disappears (or at least recedes).

I think that the correct place for verbose comments is inside the subroutine. Repeating verbose comments for expanded code re-used multiple times in a routine makes it vey difficult to read without an adequate supply of No-Doze.

I like to keep all the routines short so that I can read them as a unit in one page (or less).

 

As an aside, this is one reason why I don't like the idea of this forum being used for a non-related topic like teaching a language course. By the time everybody has their say about it, we've swamped out the recent posts column on the right with stuff that's not relevant to the vast majority of forum members.

Anything seems possible when you don't know what you're talking about.


   
Inst-Tech and Biny reacted
Page 10 / 21