4

I'm working on programming a microcontroller in C for an I/O circuit board board; it communicates to a computer which is running applications written in C++. My partner, who is working on the PC code, helped write the initial communications routines for the MCU in C.

Initially the code confused me, because it seems to be written in object oriented style, with lots of passing structs around, get() and set() type routines, etc.

Is it typical for more advanced programmers to write code like this in C? Should I attempt to make the rest of my code more "object oriented" like this as well?

Is it beneficial in C to use static global variables and get() set() methods? Or should this be left to object oriented languages?

  • 8
    This would be better asked on http://programmers.stackexchange.com – David Heffernan Mar 26 '13 at 14:33
  • The following link could help http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c – MOHAMED Mar 26 '13 at 14:33
  • 2
    Good or bad is determined by the readability. As long as the flow is clear, I dont see any objection by using or not using it. Completely depending on the code and workings. – RvdK Mar 26 '13 at 14:34
  • I haven't been around here for a while, I always thought stackoverflow was the programming site. Also, my question is not _can_ it be done, but _should_ it be done and when. –  Mar 26 '13 at 14:35
  • Well, it's more for programming *issues* (like a stack overflow ;)). But of course, the boundaries aren't very sharp. – leemes Mar 26 '13 at 14:36
  • But it's an microcontroller, with very limited resources, so, depending on microcontroller, objects can be too memory expensive (I guess?) – ceruleus Mar 26 '13 at 14:36
  • 1
    @w1res it is "the programming site", but this is a fairly subjective question. Subjective questions are *more* ok at programmers.se, where here they are extremely frowned upon – Earlz Mar 26 '13 at 14:36
  • @w1res: this site is more about problems with code, while programmers is more about coding itself. Syntax, books, what is better. etc. – RvdK Mar 26 '13 at 14:36
  • There are lots of stackechange sites for various different reasons. Stackoverflow is more for specific problems with code or algorithms, code review is for peer review, and programmers stackechange is more for general concepts and design patterns – Mike Mar 26 '13 at 14:37
  • @w1res: Perhaps a better question to ask might be "[What is object oreinted](http://www.paulgraham.com/reesoo.html)?". – autistic Mar 26 '13 at 14:38
  • 1
    Also worth noting: You have to fairly well understand how OO mechanisms are implemented by compilers. Not only inheritance, but also v-tables for example, if you need them. Otherwise your code is likely to perform too slow. Also, I think OO-style C is more difficult to maintain than both C and C++. You'll never get the automatic mechanisms like calling con-/destructors, nice syntax for member function calls etc. These are the difference between "C with classes" and a real OO language like C++ (which is only an example, of course). – leemes Mar 26 '13 at 14:41
  • about the static global variables, it may or may not be good for your code depending on things such as if you are using a scheduler or some OS which would use those functions from different tasks or threads, since you would have no reentrancy and strange things can happen if you do not implement semaphores. – RobertoNovelo Apr 03 '13 at 06:10

0 Answers0