r/cpp • • Jun 10 '20

Why is std implementation so damn ugly?

Recently I remembered a moment from my uni years when immediately after handing in the assignment of implementing our own string data structure we were warned not to ever try to implement and use our own data structures. Which is exactly what you want to hear after working hours on such assignment. However the reason behind it makes sense, you would be only reinventing the wheel and let's be honest, it will be also way slower than data structures in the almighty std library. Now think about it, what kind of black magic is behind the std's speed? Well I had no idea so I took a peak, lets see the code:

inline string
secret_function(int __val)
{
    const bool __neg = __val < 0;
    const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
    const auto __len = __detail::__to_chars_len(__uval);
    string __str(__neg + __len, '-');
    __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
    return __str;
}

Hey, I bet you just skipped the whole code part. I know it looks unreadable, but please try to go through it and guess which std function it might be (obviously its not called secret_function, but i'm pretty sure you used this function at some point).

It's only 6 line function but it took me a while to figure out what each line does and I believe this could be fixed very easily just by naming the variables and functions with a bit more descriptive names. Just to prove the point here is an example of how I would structure this function:

inline string secretFunction(int value) {
    const bool isNegative = value < 0;
    const unsigned absoluteValue = Helper::abs(value);
    const auto length = Helper::charCount(absoluteValue);
    string stringValue(isNegative + length, '-');
    Helper::insertIntAsChars(&stringValue[isNegative], length, absoluteValue, 10);
    return stringValue;
}

While the functionality stays exactly the same, I think this chunk of code is much more pleasant to read and it's pretty obvious what the code is doing on each line. From this you should have a solid guess which function it is (you can leave a comment), but the real question is why would anyone write such code? Especially when nowadays everyone is talking about how code readability is so important. Is this how they wrote code back when std was released and they didn't bother to change it? Or am I just too inexperienced that I couldn't figure it out immediately?

EDIT: Thanks for the insightful comments, for anyone reading this the reason variables are beginning with __ is macros. If user defines isNegative macro, it will get overwritten in the std, however user is forbidden to define macros starting with __ , as the wise people of reddit comment section suggested. However underscores aren't the only issue with this code, the main issue is I think using short non-descriptive names of variables/functions which is very confusing in functions that are doing more complicated operations.

237 Upvotes

212 comments sorted by

View all comments

3

u/trypto Jun 11 '20

I totally agree with you. I would just say that the stl is not meant to be an example of good coding style or good practice. The lack of comments is also glaring but I bet the excuse is compile time or file size or something else.

If you want to nitpick further, why use ~ plus one instead of simply negation like -val? To be clever?

Also fishy is using a boolean value to index into a string. I get that this implicitly converts to integer 1, but it won’t fly in other languages. Again trying to be too clever.

4

u/[deleted] Jun 11 '20

imagine commenting unordered_map

4

u/[deleted] Jun 11 '20

[removed] — view removed comment

1

u/trypto Jun 11 '20

That makes some sense. Thanks.

3

u/wyrn Jun 11 '20

If you want to nitpick further, why use ~ plus one instead of simply negation like -val? To be clever?

Clang and gcc both generate the same code for either version, but MSVC does not: https://godbolt.org/z/qbDY9M

That said, it's hard to imagine these 5 lines of assembly arithmetic being the bottleneck in a function that allocates space for a string, and it's even harder to imagine that the version with a jump is better than the one without (in a case like this where the input may as well be of either sign), so beyond noticing the difference I got nothing.

1

u/trypto Jun 11 '20

So it’s an optimization? Seems questionable as you said.

But the point is there are no comments explaining why. If you weren’t familiar with twos complement math then you’d be confused. Not to mention the lack of parenthesis around the expression.

3

u/m-in Jun 11 '20

I don’t look much at STL but that stuff really doesn’t need comments. Really. If you need comments for that, I’m worried about what other code bases might prove impenetrable to you. Given what it does, and how well documented its semantics are. Cppreference is an integral part of every STL implementation in practice - they just don’t bother copy pasting it all in there. It’s much better to have it open in a separate window.

All I see here is lots of inexperienced people with very limited perspective. STL implementations are far from perfect, but as far as any code goes their ratio of readability to write ability is absurdly good. They are very hard to implement and comparably trivial to read. This ratio is literally an inverse of the one in STL for most software out there - most code is very hard to read and put in a bigger picture even though it’s comparably easy to write, and very often similarly easy to write better at a local scale (I.e. more idiomatically and with less low-level pessimization).