Live data from Hacker News

Modular C: Reusable and Maintainable Code Using the C Language (1994) [pdf]

metamodular.com

31–35 of 35 posts

Re: Modular C: Reusable and Maintainable Code Using the C Language (1994) [pdf]

#31
post #27
post #19

Earlier quoted context omitted.

> the proper modern programming language I understand wanting to advocate for your favorite programming language but this kind of elitist attitude doesn't do the cause any favors, it's more likely to turn people away than anything. Rust is not "the proper programming language". It's just one of many excellent choices.

You're absolutely right. These people have seriously marred my view of the language.

[deleted]

Re: Modular C: Reusable and Maintainable Code Using the C Language (1994) [pdf]

#32
post #5

This is old-fashioned (uses C89 instead of C99) and some of its "idioms" are questionable, like this one: list *p; list l = (list) malloc(sizeof(struct list)); l -> val = val; for(p = &the_list; *p && (*p) -> val next)) ; l -> next = *p; *p = l; Here p is a doubly indirect pointer and I would personally think this is an unwarranted cleverness. Furthermore this is the year 2019. Why would you still manually traverse a…

A a pointer to a pointer is a perfectly valid C idiom. I'd say typedef'ing a pointer would raise more eyebrows :) E.g. I have a singly-linked list of struct x { int y; struct x* next }; I want to locate an element here and then maybe remove it (1), maybe insert a new one before (2) or after (3), maybe replace (4), or maybe just return what I've found (5). How do I write this base locator function? If I do it naively…

> By the way, will your C++ code work with singly-linked lists?

Yes, but you'll use merge instead:

    list.merge(std::forward_list{val});
I chose to use std::lower_bound for binary search. If you are content with linear search like in the C example, you can even write a single function that works for both doubly linked lists and singly linked lists.

> Also, look: your C++ example calls four methods. The C code calls only one (malloc), all the rest is core language. It's 4:1 ratio for extra complexity.

This is what I call unnecessary manual inlining and reinventing the wheel for no good reason.

Re: Modular C: Reusable and Maintainable Code Using the C Language (1994) [pdf]

#33
post #5

This is old-fashioned (uses C89 instead of C99) and some of its "idioms" are questionable, like this one: list *p; list l = (list) malloc(sizeof(struct list)); l -> val = val; for(p = &the_list; *p && (*p) -> val next)) ; l -> next = *p; *p = l; Here p is a doubly indirect pointer and I would personally think this is an unwarranted cleverness. Furthermore this is the year 2019. Why would you still manually traverse a…

> Why would you still manually traverse a linked list? Oh right because C doesn't have parametric polymorphism (genericity) so you can't abstract out the traversal in a standard library.

It's pretty easy to make something like this work:

    list_insert(&itemList, struct Item, &item, &compare_item);
Which would be just a thin macro, for example defined like this

    #define list_insert(list, type, item, compare) \
        __list_insert((list), \
            sizeof (type), __alignof(type), \
            (item), (compare))
You could further "improve" to even allow

    DEFINE_LIST_FUNCS(itemList, struct ItemList, compare_items);

    itemList_insert(&itemList, &item);
It's not the end of the world. The reason C programmers usually don't make or use such "libraries" is that these are pretty easy to craft on the spot, and there are just a billion way of making it work. Allocation strategy and intrusively vs extrusively linked are two example considerations. I for one prefer to think about the best way to organize my code and data, so I'm not interested in such a library.

Re: Modular C: Reusable and Maintainable Code Using the C Language (1994) [pdf]

#34
post #32

Earlier quoted context omitted.

A a pointer to a pointer is a perfectly valid C idiom. I'd say typedef'ing a pointer would raise more eyebrows :) E.g. I have a singly-linked list of struct x { int y; struct x* next }; I want to locate an element here and then maybe remove it (1), maybe insert a new one before (2) or after (3), maybe replace (4), or maybe just return what I've found (5). How do I write this base locator function? If I do it naively…

> By the way, will your C++ code work with singly-linked lists? Yes, but you'll use merge instead: list.merge(std::forward_list {val}); I chose to use std::lower_bound for binary search. If you are content with linear search like in the C example, you can even write a single function that works for both doubly linked lists and singly linked lists. > Also, look: your C++ example calls four methods. The C code calls on…

Binary search on a list??

Re: Modular C: Reusable and Maintainable Code Using the C Language (1994) [pdf]

#35
post #5

This is old-fashioned (uses C89 instead of C99) and some of its "idioms" are questionable, like this one: list *p; list l = (list) malloc(sizeof(struct list)); l -> val = val; for(p = &the_list; *p && (*p) -> val next)) ; l -> next = *p; *p = l; Here p is a doubly indirect pointer and I would personally think this is an unwarranted cleverness. Furthermore this is the year 2019. Why would you still manually traverse a…

Forgot to check for null return from malloc().

Excuse me, but this sort of complaining about toy code is pretty pointless, at least when it comes to bashing the language. I don't use malloc directly in real code...

You'd never object "forgot to catch std::bad_alloc from new'ing", would you?

Post reply on HN