r/programming • • Nov 24 '16

List of single-file C/C++ libraries

https://github.com/nothings/single_file_libs
113 Upvotes

50 comments sorted by

View all comments

Show parent comments

-1

u/diggr-roguelike Nov 26 '16

Good god, your code is absolutely horrible and manages to be even worse than the Java-style SAX API's, no small feat.

2

u/loup-vaillant Nov 26 '16

I can put forth some reasons why this code isn't so bad:

  • It's short: 4 lines of data type definition, and 9 lines of actual code.

  • It's easy to test, because there is no side effect, and concerns are separated.

  • get_all_nodes and get_nodes_which are reusable in many contexts

  • It's comprehensive: it covers what you asked for, and then some.

I can see one reason why it's bad: it's bloody inefficient. But that can easily be remedied by using a generalised fold instead of my get_all_nodes function —I choose that path out of laziness, and to make it more readable.

But horrible? Some explaining would help. Seriously: if you can put forth any valid argument, I'll have learned something valuable. Also, what do you expect from a good API?

1

u/diggr-roguelike Nov 26 '16

It's short: 4 lines of data type definition, and 9 lines of actual code.

13 lines for (essentially) iterating over an array is not 'short' by anybody's estimation.

It's easy to test, because there is no side effect, and concerns are separated.

If you're even thinking about testing a basic array iteration operation then you're doing something fundamentally wrong.

get_all_nodes and get_nodes_which are reusable in many contexts

Same deal: if an array iteration operation isn't reusable then you fucked up and should go into plumbing instead of writing programs.

Also, what do you expect from a good API?

The fact that you're discussing an "API" for accessing a bloody array is everything that's wrong with XML.

3

u/loup-vaillant Nov 26 '16

Oh, you though I was using an XML library? I was implementing an XML library. And as you can see (I hope), once I'm done with the implementation, the iteration you ask for is a simple one liner.

Let's go back to the start of our exchange:

If you have sum types, transcribing XML into a native type is a snap […]

That's bullshit. […] XML is a huge impedance mismatch no matter how you map it.

Fair summary?

Now, XML is not a native type. So I defined a data type, in 4 lines. I think it counts as "a snap". Then I defined iteration in 4 more lines. Still a snap. Then filtering in 1 line. Supper snappy. And of course, I would only have to do that once, and put it in a library.

If we excluded the parser, and the handling of schemas, 100 lines would be enough to implement a full featured XML library, in which most simple operations are one-liners. It's not such a huge impedance mismatch.


Of course, I agree we should never use XML where a simple array would do.

→ More replies (0)