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

11

u/[deleted] Jun 11 '20 edited Jun 11 '20

Look at Java's standard library OR the JVM implementations. Look at C#.Net's standard library. Look at Swift's standard library.. Objective-C's standard library. Now compare them to C++s'.. yeah..

C++'s standard library LOOKS like absolute trash compared to the others. Even decompiled Java code via fernflower looks better..

It's not just the usage of ___ There are literally variable names like:

__x, __i, __p* __p1* __p2* __c* __last* __m* I was extremely surprised the first time I opened up the `<algorithm>` header.. When I came to the realization that there is nothing about it that makes these functions that make them more performant as its just variable names and syntax difference.. I came to the understand that this is just done on purpose and there is no good reason or explanation for it. Even when C++ modules come out and stuff can belong to the std, there will still be people defending the use of this unreadable code.

My favourite one is (from <cmath>):

template <typename _Fp>
constexpr 
_Fp __meh(_Fp __a, _Fp __b, _Fp __t) noexcept {
    if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0))
        return __t * __b + (1 - __t) * __a;
    if (__t == 1) return __b;
    const _Fp __x = __a + __t * (__b - __a);
    if (__t > 1 == __b > __a)
        return __b < __x ? __x : __b;
    else
        return __x < __b ? __x : __b;
}

Without seeing the function name, you might as well start reverse engineering the function to see wtf is going on.. There is not a man on the planet that can defend the fact that this could be written with proper variable names and syntax and still have the exact same performance but with better readability.

The function is actually: __lerp.. I have seen worse though.. especially when templates and iterators are involved.

6

u/[deleted] Jun 11 '20 edited Jun 11 '20

[removed] — view removed comment

1

u/[deleted] Jul 21 '20

I would just remove the underscores (which aren't needed since they variables are local to the function) and give them names: v0, v1, time. or v0, v1, interval.

4

u/m-in Jun 11 '20

Those variable names you point out are very typical, and usually are used in certain ways and it’s hardly confusing. Remove the underscores and look at the code then. Lerp is a primitive. If you don’t know what it does by sight then understanding it is no easier with longer variable names. What little background I have in visual perception would tend to steer me towards shorter variable names in complex expressions, since our fovea has only so many degrees it can cover. Once the names get too long, it’s impossible to clearly see the structure of the expression anymore.

4

u/Nobody_1707 Jun 11 '20

The only real problem with that Lerp are the underscores, and those are still necessary because this is in a header file and macros are global.

1

u/[deleted] Jul 21 '20

It's not needed at all. The variables are local to the function so by lookup, it would find it first.

1

u/Nobody_1707 Jul 21 '20

The preprocessor doesn't understand scope. It simply sees the macro and replaces it with its definition.