What I am thinking when I am writing C and Java

It is really different feelings. When I am writing the C code it forces me to must think about data structure at first time. But when I write C++/Java I always think about abstraction and behavior (methods) as starting point.

Once upon a time and even today I like C++/Java thinking style very much and abstract class and behavior and make general OOP design.

But as time go when I deal with some really complicated problems I am aware of like a lot of expert said data structure is much more important than abstraction, behavior or algorithm. Especially recently I am reading source code of Lua and Redis I am so surprised these C code is amazing. And also I put myself to think if I am author how I can start and implement them as C++/Java programmer. A lot of abstraction and a lot of methods fill in my minds immediately I am almost lost and I can’t find a easy starting point although there are so much methodologies about OOD. Maybe I can write pretty much code at abstract level such as VirtureMachine interface including compile and execute abstract methods but I know it’s nothing. I will take a lot of time to think about non-meaningful code, I am escaping from the most difficult part, like this.

As another simple example with pretty much circumstances we hope a method execution running with a specific context or container. When I use C++/Java it is pretty obviously that I provide a Context interface and inject to domain object and at this time I haven’t known anything about Context details and I just put everything, in general a lot of geXXX() methods other service methods which I need get from Context. Everything is good.

But when I suppose I am a C programmer it seems becoming different. I have to think about its internal implementation and carefully design structure and a series methods on it and also these methods sequence dependency such as init method must be called before other methods. At this time possibly you will said structure is just like a class only containing data you can do same thing with OO language but it is different as OOD it is abnormal that a class not containing methods. And OOD always firstly emphasize behavior’s importance.

But with a lot of circumstance data structure is much more important than behavior and abstraction. It is so directly to focus the core problems and avoid bad taste of over-design and it seems impossible that C program has over-design problem, and it decide how you start any other things.   Around data structure to design system is always a tradition of unix/linux C programmers.  It is not easy to master and I think that’s why OOD is so popular you are always able to start writing something even you are not clear how to implement it.

A good C programmer either can not start programming at all without data structure or resolve problem smoothly and almost perfectly around the several  data structure.

As a Java programmer it take so many times to design abstraction and carefully use all kinds of design patterns to generate tons of code but the problem is still there. Refactoring and redesign almost distribute the whole development life cycle.

2 thoughts on “What I am thinking when I am writing C and Java

  1. Pingback: Tech News / What I am thinking when I am writing C and Java

  2. Pascal Bourguignon

    On the other hand, code = data. 😉

    What sicp (and the abstract data type theory) teaches us is that you can (and should) use functional abstractions to represent your data.

    You must be careful to distinguish methods that represent functional abstrations of some data type, from methods that represent some processing or “pure” function.

    For example, when I design a collection “data structure”, I start with:

    extern Collection collection_new();
    extern void collection_add(Collection c,Element e);
    extern void int collection_count(Collection c);
    extern void Element collection_at(int index);

    How Collection is implemented, and what Element is, just don’t matter. You can have stub implementations of this functional abstraction and start coding the rest of the program even before you decide on the actual data structure behind the collection. And this is how it should be, because you will choose one concrete data structure or another DEPENDING on the access patterns on the collection! This is something you can really know only when the program is completed. Therefore you should never start by implementing concrete data structure, but by defining functional abstraction, use them, monitor them, and then decide on what concrete data structure to use.


Leave a Reply

Your email address will not be published. Required fields are marked *