Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

While studying the C programming language and learning safe practices, I'm inclined to write a layer of functionality over several parts of the standard library. This would serve two purposes: I could use standard parts of the language in ways that feel more familiar or rational to me, and I could easily replace that functionality with my own, if I needed to.

I could benefit from this, but should I do it? As an example, we can consider memory management. If I've written malloc() into the constructors of each of my objects, then decide that I need to handle memory allocation on my own, I have to edit the constructor associated with every object. By referencing my own function, I can change the contents of that function without writing a new constructors.

It seems obvious that I should do this, but I'm used to Python. I'm extremely comfortable in that environment and have no problem linking to any part of the standard library from any part of my program because I know I will almost certainly leave that relationship untouched for the life of the project. The situation I'm running into with C feels like I'm trying to hide the language from myself.

Will writing a layer of functionality over the C standard library help me in learning the language and developing a codebase, or will it stifle my understanding going forward?

share|improve this question
    
You can see this happening in certain kinds of libraries. See memory manager interface in jpeglib . However, if you want to do this on a massive scale because of your personal taste (of Python), I'd say C might not be the appropriate language. Lack of function overloading, virtual methods, generic programming etc. will severely limit what can be achieved. This goal is somewhat more achievable in C++. If you really must try, learn about COM, MS's attempt to add OOP to C. –  rwong Oct 30 '13 at 4:41

4 Answers 4

up vote 7 down vote accepted

I think there could be some use in writing your own layer, so long as you retain appropriate perspective.

When you interact with other people about your code, you'll still need to talk about it in terms of the standard functions.

But it could help you crank out similar applications if you wrap-up a nice set of routines that's useful. At the worst, the artifact you may end up with will be little more than a well-organized snippets file. And that's not such a bad thing. This is iff you keep perspective.

There is a danger of going waaaaay overboard. Basically implementing your own language within C. And, well, that's dangerous, crazy territory! You may seize upon something really cool and becomes the next big thing (it could happen!) or you may bounce around the endless circles of speculation: oh, and this could be expressed like this, and all of that would be simpler through this interface, and you convert it all to a new super-canonical representation, and ...

xkcd: "Standards"

http://xkcd.com/927/

I believe the danger zone I'm talking about is approximately the same as the "Architecture Astronaut" paradigm. <yoda>Remember: don't build another game engine, build a game. Then a second game. Then a third game. Then you may build an engine. </yoda>

Writing a layer over the API is a possible method of building an application. It is the approach taken by one of the most-readable X11 book I've ever found: X Window Applications Programming (Johnson & Reichard) (the series kept using K&R function syntax until at least 1996!), but for pedagogical value, it's priceless. Like I alluded to above, a well-organized snippets file.

share|improve this answer
    
Perhaps implementing that layer with each new project (at least while I develop a foundation within the language) would keep the layer lean while still requiring me to play with lower level functions. –  Jack Stout Sep 19 '13 at 6:15
1  
Yeah, that sounds like a reasonable way to approach things. But just be careful about thinking that you need to make a better interface than the standard one. You may easily end-up rewriting functions that already exist, and just wasting time instead of building useful programs. There's usually loud condemnation of such notions whenever they arise in discussions in comp.lang.c. –  luser droog Sep 19 '13 at 6:19
    
I can understand that. It's easy to get lost in the philosophy of over-analyzing a problem when you could have written a solution. –  Jack Stout Sep 19 '13 at 6:27
    
Indeed. Here's one of my trainwrecks: stackoverflow.com/questions/13459300/… . –  luser droog Sep 19 '13 at 6:35

The temptation of writing a layer above a standard API is common among beginners. I've been there too.

Although you may find short term benefits, I strongly advise not to do so. The standard API is, by definition, standard. It is known and used by all developers and sooner or later you'll have to know and use it. Don't delay that learning by encapsulating the standard API with something that only you will be familiar with.

share|improve this answer

+1 @mouviciel.

I came into C from the Pascal highway (Turbo Pascal and yea, it was a long time ago). I decided that C needed "Begin" and "End" and struggled for months to no avail. In the end I gave up because there were bigger fish to worry about. Any interpretive changes you make will have to be logical to whoever has to update your code in a few months (or years). Odds are that won't happen because they aren't you and don't have your POV. Your code (and reputation) will get thrown under the proverbial bus.

I suggest just bite the bullet and dig in. Embrace the preprocessor. You will have to contend with the failure of the presentation layer to control the functional (the Pythonic White Space Rules paradigm). I'd also recommend jumping to C++ since you've already gotten past the O-O hurdle. You're gonna love multiple base classes and having to do your own GC.

My favorite reference was the Harbison-Steele but that (as noted) was many moons ago.

share|improve this answer
1  
+1 for "bite the bullet", -1 for "jump to C++". real C coding is a very valuable experience. –  Javier Sep 19 '13 at 17:56
    
But +1 for "You're gonna love..." –  mattnz Oct 30 '13 at 2:55
    
lol, I have a friend that used to work with a team of ex-Pascal devs being forced to write in a C-language who used macros to convert begin..end blocks into {}. He said it used to drive him nuts –  Matt Allwood Oct 30 '13 at 10:45

Moving from Python to C/C++ is a substantial jump in complexity and is no surprise that you are wanting to find a way to manage that complexity in your code.

In reality, trying to write your own complexity management layer will fail. In part, because you probably don't know enough about the language and how to use it effectively to do a good job, and secondly there are already libraries out there that provide what you are after (albeit it a C/C++ way, not python)

Have a good read through the Standard Template Library code, particularly around the shared_ptr and auto_ptr classes as ways to manage the heap memory safely. This will, over time give you a good understanding of the way to solve problems in C++, rather than trying to use C++ to write python code.

share|improve this answer
    
I can take a look into that. I haven't spent any time with C++, but should. I'm missing out on a large part of the discussion without it. –  Jack Stout Sep 19 '13 at 6:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.