2 ms·
Readability is about how quickly you can figure out what's going on. There are only a few bits of syntax you need to know what's going on in the first example,
by seabee 12y ago
Readability is about how quickly you can figure out what's going on. There are only a few bits of syntax you need to know what's going on in the first example, but it requires mentally matching together the angle brackets correctly, and it emphasises the type over the function name.
In most cases you can scan over the type ('map blah blah...iterator') but that's not reading and in any case will quickly fail you with nested template types, if you're unfortunate enough to have those.
- millstone 12y agoFunctions that return iterators are never called getSomeIterator(). Instead they're called stuff like erase(), insert(), or even nonsense words like crend(). And whether a function returns an iterator is not consistent even within the STL: auto loc = container.find("name"); What type is loc? If container is a std::map, then loc is an iterator. If container is a std::string, then loc is a size_t. If you had used the explicit iterator type, you would at least get an error message immediately with the above line, instead of hoping your code does something that size_t doesn't support.