Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/20/07 04:21
Modified:
  06/20/07 06:19

Read: times


 
Msg Score: +2
 +1 Informative
 +1 Good Answer/Helpful
#141063 - Wasn\\\'t Natural For Me
Responding to: ???'s previous message
Steve said:
Once you approach software from an electronic engineering background, I think OO is just the natural way to write code!

Whats OO but wiring little functional blocks together...which is exactly what an EE does. Some of the wilder aspects like polymorphism are a bit too abstracted for me - and lead to code bloat anyway, but fundamentally I found I had ALWAYS been writing in an OO style, whatever label was used to describe it.

I think my viewpoint is similar, but not quite the same as yours. Or maybe we just have different understandings of what "object oriented" means.

I find myself naturally writing highly modular programs, where a group of functions and the data that they "own" all live together in a single source file, with a limited and well defined interface to other modules. But to me, code that's merely modular is not necessarily object oriented (although the converse is certainly true).

I think that code gets to be object oriented as soon as it starts to have functions (methods) that operate on multiple and distinct data structures (objects) without knowing or caring which of those structures they are working with at any given moment. I consider the example that I gave at the start of this thread to be object oriented code, but similar code that could only deal with a single UART wouldn't be.

It took me a long time to understand and appreciate the OO paradigm, and getting there was anything but a natural process. Part of the problem I think were the made-up examples that folks sometimes use when they're trying to explain it. How often have you seen some introductory tutorial use "shapes" and "ellipses" and "circles" as examples of objects. C'mon! Those are abstract concepts if there ever were any. Remember, Mr. Tutorial Writer, I'm just learning this stuff. It's hard enough without you telling me that a "shape" is an object. Please give me some examples I can relate to. Like rocks. Rocks are objects. Thank you.

A second problem was that some folks seem convinced that object oriented programming is an all or nothing deal. A big light bulb came on for me the day I finally realized that the C++ gods weren't going to rend me asunder if I didn't try to make every single "thing" in my program a member of some class, even if that meant contriving artificial and useless classes to make them members of. I've been in a twelve-step program ("Hello, my name is Russell and I create abstract classes for no reason"), and it's helped a lot. These days I'm quite content to call an integer an 'int' and not feel ashamed about being too lazy to write the CNumericVariableThatCanOnlyContainCountingValues class.

Once you get the hang of it, I think the key to using OO techniques effectively is to apply them in moderation. Use them where they make good sense and ignore them otherwise. As an example, I find that it almost never makes sense to create a class unless you're going to need multiple instances of it. If you just have one of something, just hide it in a module by itself and be done with it. On the other hand, if you do have seven things that are all alike (like my UARTs), then by all means take the trouble to "objectify" them. Your code can be a lot smaller and cleaner than if you try to deal with them individually.

-- Russ


List of 10 messages in thread
TopicAuthorDate
Object Oriented Programming in C            01/01/70 00:00      
   Make it ugly. :)            01/01/70 00:00      
      Ugly and scary!!!            01/01/70 00:00      
   Logical approach            01/01/70 00:00      
      Wasn\\\'t Natural For Me            01/01/70 00:00      
         Nice post Russ.            01/01/70 00:00      
         Agree 100%            01/01/70 00:00      
         OOP in C            01/01/70 00:00      
   more OO in C            01/01/70 00:00      
      All good hints, for sure            01/01/70 00:00      

Back to Subject List