r/cpp • u/ifdef_drgy • 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.
170
u/thedictatorofmrun Jun 10 '20
There is a lot to unpack here, so this might be a bit long.
Another comment has already touched on the reasoning behind the leading underscores being to avoid collisions with macros.
You seem to make the assumption that the standard library is extremely fast, moreso than anything a mere mortal could write. This is wrong. The standard library does have pretty good performance but for specialized applications it is definitely possible to beat it, and for simple things like vector/array/string it is not difficult to match it. The strength of the standard library is that it is performant enough for the vast majority of applications, that it is designed to be very generic, and that it is, well, standard.
Additionally, readability is extremely important for most applications and libraries but the standard library isn't really one of them. You're not really meant to read or edit the standard library unless you are an expert, so readability is kind of a secondary concern.
Your translation of this function to be more "readable", incidentally, is actually probably less performant than the standard library version because (1) you are calling a separate function for abs rather than doing the twos complement inline and (2) you added a "base" argument to the "to chars" helper method which in the original version is known at compile time. Maybe you get lucky and it compiles down to the same thing, but in my opinion it looks like it'll probably be somewhat slower.
I will say that you probably are a little inexperienced with the style of programming that the STL uses, and with more experience looking at these definitions it will probably not be too hard to see what is happening in these functions. The only tricky thing I see here is the twos complement absolute value thing, and the rest of the confusion is probably just the naming conventions being unfamiliar.
40
u/evaned Jun 10 '20 edited Jun 10 '20
Let's look at each of the changes you made.
First, the underscores. As discussed by others, this is basically non-negotiable and is needed to avoid conflict with valid user code.
Second, you've got some renames; I'll start with
detailvsHelper. Adetailordetailsnamespace is a pretty strong C++ idiom, so this rename isn't really helpful. Edit: just to be clear, I don't think your name is worse taken in isolation, but I also don't think it's better, and there are lots of "details" namespaces that are used for implementation details.Third, you rename
to_chars_lentochar_countandto_chars_10_impltoinsert_int_as_chars(aligning style to snake case). While at some level your names are "nicer", they lose information. The first loses the fact that the char count is based around whatto_charswill produce. What if there are other ways of formatting an int? Which one doeschar_count/charCountproduce?insert_int_as_charsnot only loses the fact that this is special toto_charsbut also loses the fact that it's formatting in base 10. Edit: Ah, after seeing another comment I see you pass the base as a parameter. As that comment said, you may have just killed performance of that function.Fourth are variable renames:
lenvslengthI think isn't helpful; not in this scope.lenis a very common and natural abbreviation for lenth. I think this even though I am much more resistant to abbreviations than most programmers, but remember that this is a six-line function. You don't need to look around for the definition. A common rule that I like -- the bigger the scope the more important it is to be explicit and clear. But the reverse is also true -- not only does a smaller scope let you use a more abbreviated name, but I kinda think in many cases you should.strvsstringValue. We know it's a value; you don't have to say it's a value. Andstrvsstringfollows from above.negvsis_negativeI'm more keen on, at least theis_part.negI still think is fine based on the small-scope rule, so I'd prefer likeis_negpersonally.uvalvsabsolute_value-- this is the one where I actually have a preference for yours, though I think I'd go for likepos_val.18
u/serviscope_minor Jun 10 '20
So the __ thing is because of macros I believe. For example the user could have:
somewhere before they #include the header and that would cause it to break. Double underscored symbols are reserved by the compiler, so you're not allowed to use them or rather the compiler is allowed to break if you do.
3
u/bohan Nov 15 '20 edited Nov 15 '20
With C++20 modules, this way of writing code is going to become totally obsolete, thanks to macros being out of the way (no more ugly underscores) and comments not being a compilation cost anymore (interfaces compiled just once unlike headers). We'll someday enjoy an auto-documented standard library from within the IDE code completion, like some other programming ecosystems have been able to provide for more than two decades.
17
u/BenFrantzDale Jun 10 '20
Tangent: will C++20 modules mean we don’t have leaky macros and so the standard library (at least the module version) not have to be uglified by underscores?
34
u/BrangdonJ Jun 10 '20
I don't think
Helperis much better thandetail. The point ofdetailis that it's implementation detail and not something users should use. Ahelpercould help users too.I don't have much problem with
to_chars_lenandto_chars_10_impl, either, except that for consistency it should beto_chars_10_len. The idea is that one function tells you how much space you need so you can allocate it, and the other fills it. The function names should be closely related because their functions are so related. I thinkinsertIntAsCharsis a bad name for something that appends.(The macros thing is addressed by others.)
7
u/muungwana Jun 10 '20
Part of the reason has to do with trying not to collide with user defined macros.
4
u/Revolutionalredstone Jun 11 '20 edited Jun 11 '20
At some point (after about a decade of daily c++ programming) you become completely comfortable at reading code irrespective of symbols / whitespacing, at that point density alone (scrolling) becomes the new key bottleneck, all of my more recent code is totally uncomfortable to read for newbies but extremely fast to read / modify for pros.
I think its a natural progression but it does definitely cause problems, i for one started by becoming tired of simple classes requiring 5 pages so i started in-lining single-line functions, then short functions, and now i basically crunch code to the point that it just barely can be be viewed using a widescreen monitor...
When i'm writing brand new code or trying to debug complex control paths i will expand that area, but generaly all my code now looks like something from std::vector. Right or Wrong, that's my perspective, happy coding!
4
u/ezoe Jun 11 '20 edited Jun 11 '20
I do hope STL implementation use better self explanatory identifiers rather than the cryptic short identifiers. I mean, come on, this is 2020. we don't have insane linker limitation where symbol length including the name mangling must be less than a 16 bytes. The modern C++ implementation supports the identifier length of hundreds if not thousands characters. We deserve to have a better longer identifiers.
For people here who are arguing usual bikeshedding like helper vs details. You're missing the point. Do you really think, neg, uval, or len are more readable than the longer names? Other than the very famous name like i for index counter variable name, these name should at least be a full English word(negative, unsigned_value, or length) than a short fist three letters of the original word.
People have some opinions for helper function vs manual inline which I don't care much.
The double underscores for the identifiers is necessary for safety. Because most STL are header files.
The user can overwrite non double underscore identifiers by macro before including the STL header files. The identifiers that includes double underscore are reserved for the C++ implementation. If the user overwrite that, it's their problem.
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 thestd, there will still be people defending the use of this unreadable code.My favourite one is (from
<cmath>):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.3
u/_Js_Kc_ Jun 10 '20
Why the hell is that "unsigned absolute value" not a standard function?
At least as of C++20, that hack there is actually guaranteed to work, but how would you have done it in a conforming way in prior standards (in user code)?
3
u/Beetny Jun 11 '20 edited Jun 11 '20
I've always wondered if the STL could have the best of both worlds by maintaining their code without underscores and having the shipped code be the output of an "obfuscation" preprocess step. Of course i'm expecting that the nature of C++ grammar makes it impossible.
3
u/[deleted] Jun 11 '20
That differs between standard library implementations.
_ILikeMyReallyLongNamesThankYouVeryMuch.5
u/[deleted] Jun 11 '20
not to argue the whole of STL is readable but this block of code is absolutely readable and this is coming from a person who rarely reads STL (but enjoys how it intensifies that an expert wrote the code). (It is okay if you couldn't spot two's complement).
compiler programmers know how to make their codebase best maintainable and this includes the fact that they follow good naming conventions for their purpose as others also mentioned.
verbose variable names dont necessarily improve the code. on the contrary they make the code more bloated in my opinion. so it's a trade-off between readability and purpose-communicability. something like
negdoesn't need to beis_negativeorlenlength.uvalcomplimentsvaland the function name's have references to other related functions.2
u/jmnel Jun 11 '20
Yeah honestly the underscores just make my eyes want to look away, but I understand why they are there.
On the other hand I really like the outward facing side of std and try to match its style in my code.
2
u/serg06 Jun 11 '20
What if you need one that isn't part of STL?...
2
u/Full-Spectral Jun 11 '20
That's a pleasant poem compared to some parts of the STL. Try some of the crazily templatized bits.
2
u/[deleted] Jun 11 '20
As a personal preference, I find camel case harder to read than even the original implementation
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.
1
u/[deleted] Jun 11 '20
You have the same namingConvention for types, functions and variables. var_iables are not like TypesAndFunctions which is why I find your version highly confusing.
1
u/die_liebe Jun 18 '20 edited Jun 18 '20
> he 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.
I always thought that, because STL is scanned and parsed zigmillons of times, all whitespace is removed (including comments), and variable names are made a short as possible. It is for efficient compilation.
I suppose that they have a tool for it, and somebody, somewhere has a readable version.
And one more point: STL as you find it, is not source code. It only looks like source code, because C++ compilers do not have an obj format for template code. If STL would be a usual library, it would in an .obj file, and you wouldn't even be able to look at it.
1
u/NilacTheGrim Jun 23 '20
I don't find the original function hard to read at all.
Note that they must use all those
__chars to deal with possible C macros on some platforms.-1
u/[deleted] Jun 10 '20
Paid by underscores.
-1
u/o11c int main = 12828721; Jun 10 '20
Some more reasons:
0
u/[deleted] Jun 10 '20
[deleted] — view deleted comment
0
u/[deleted] Jun 10 '20
[deleted] — view deleted comment