r/cpp • • Aug 25 '21

Experience C++ developers: do you enjoy using C++ (even for personal/hobby projects?)

Do you actually enjoy using C++, or do you only use it for work, or purely for performance? This question is aimed at people with years of experience, who've worked on large projects in C++. I see people who dislike the language, but use it purely for performance reasons. This sort of discourages me from learning it. I don't want programming to feel like work.

204 Upvotes

327 comments sorted by

View all comments

Show parent comments

-1

u/ohell Aug 25 '21

can lead to naming collisions

I have come around to loathe this argument. If there are naming collisions, compiler (and IDE) will tell you.

Personally I have had it with the demented verbosity of the language. Typing std:: 1000 times in each file - Why??

It is not a big deal to treat vector, string, deque etc as de facto reserved keywords, and be safe in the knowledge that if some 3rd party includes use these words to name things, it will be flagged up and disambiguation is easy.

13

u/braxtons12 Aug 25 '21
  1. Its not just types that get included when you using namespace. It's EVERYTHING. That creates needless namespace pollution which in turn is going to make your IDE's auto-complete and symbol resolution slower. It also pollutes ADL which can cause build breaks or run time issues that are insanely hard to catch and debug. It also sets you up for potential build breaks if you ever migrate to a newer standard because there could be new symbols in the std headers you're including.

  2. Continuing from (1), Naming collisions: "your compiler (and IDE) will tell you". Yeah, if it's a TYPE naming collision. For functions, you'll only get collisions if they're the exact same signature. If the standard has something that takes a Thing and you write something with the same unqualified name that takes a ConvertibleToThing, refer to what I said in (1) about ADL issues.

  3. Is it really that hard to just write std::?

Basically, the main technical reason, is sooner or later, if you using namespace std; ADL might ruin your week, and it's a lot easier to just not risk that and type the extra std::.

For reference, see Titus's talk from a few years ago: https://youtu.be/BWvSSsKCiAw

Or the associated paper (SD-8): https://isocpp.org/std/standing-documents/sd-8-standard-library-compatibility

0

u/ohell Aug 25 '21

On 1 and 2 we'll have to disagree, I'll only say that this is classic case of hampering your base case workflow just to guard against vanishingly unlikely corner cases.

3 depends on typing speed, but 1 guess 1 second each time? More importantly I find it slows down my reading of the code. Imagine I gave you an essay to read where random words were prefixed with ohell::!

7

u/braxtons12 Aug 25 '21

I'm gonna have to disagree that it hampers readability. Explicitness aids readability, not the other way around.

If I'm reading a story and there's a blanket/box/wand/whatever, I wanna know who's thing that is. Same in code. If I see a type or function, I want to be able to immediately see whose type or function that is, without having to think about it. EG: std::vector whose vector? std's vector. outcome::result whose result? outcome's. Makes it immediately clear where something is coming from and what it's semantics and guarantees are.

If I just see vector, is that a std vector? Is it an eastl vector? Is it a math vector? If I'm not familiar with that part of the codebase I now have to read more of the code to just infer what actual type this is. With the namespace prefix it's immediate.

0

u/ohell Aug 25 '21

But when I am reading code, I am trying to understand the algorithm, control flow, an inaccuracies etc. The less fluff there is in the code, the easier that task is.

It is sufficient for me to know that sone values are being stored in a vector, which means they are contiguous, address can change, insertions are not a good idea and so on. Never do I wonder whether this is standard vector or something elsethough I do like boost::small_vector. Indeed, other types of vector would only feature in my code if I add them (or if an included library does, in which case I'd expect that there is no problem because code compiles)

I guess we are coming at it from different perspectives: you value following the lbest practises in order to have a bulletproof library or program, whereas I want a program that works, has acceptable performance and the code is legible, without needing too much tinkering and typing.

5

u/braxtons12 Aug 25 '21

It seems to me like you're coming at this from the perspective of "I own 100% of my code so I know where things come from". If that's the case, then you probably don't have any concerns about maintenance or other users or contributors.

For most people this isn't the case. You could be working in a team on a large codebase and not know what third party dependencies have been brought in, you could be trying to submit a patch to an open source project, etc. I'm looking at it from a perspective of "sooner or later someone else is going to have to read or change this code" and the easier it is to read and reason about then the faster they can get their work done.

Also, like I mentioned above, code compiling doesn't necessarily mean nothing is broken. Again refer to ADL, Titus's talk, and SD-8.

10

u/Pleasant_Sir_3469 Aug 25 '21

for the types that you use a lot just write: using std::vector; using std::string, using std::deque; at the top instead of using namespace

3

u/[deleted] Aug 25 '21

Normally I'll employ using std::string or whatever in the namespace in the source file and leave the header files with the full prefixes.

there are naming collisions, compiler (and IDE) will tell you.

It is not a big deal to treat vector, string, deque etc as de facto reserved keywords, and be safe in the knowledge that if some 3rd party includes use these words to name things, it will be flagged up and disambiguation is easy.

These aren't usually the problem. My problems have always been all of the other things in the "used" namespace which get included transitively when you include headers and all of the functions/types they will implicitly add to the global namespace. It'll be like "wait, rotate is colliding? There's a std::rotate?"

3

u/_JJCUBER_ Aug 25 '21

There are tons of reasons why this is a valid argument, though I don’t have time to go over all of them right now. A simple reason as to why people might name their own class as vector, string, or any of the other commonly used ones is because many companies may have their own implementation that gets used everywhere, as they may find the STL variant to be not as fine-tuned to what they need (amongst many other reasons). It makes no sense to clutter the global namespace with tons of names, especially in a header file that gets included in multiple other places. Plus, it’s good to disambiguate what is being used from where.

-3

u/SkoomaDentist Antimodern C++, Embedded, Audio Aug 25 '21

I have come around to loathe this argument.

It's just one more example of a very persistent issue with the C++ community: The huge number of people willing to tell others how those others must write their code, despite the first group having literally zero idea what the other people are doing and often having years or even decades less experience.

Be aware of the issues and then do whatever the fuck you want while ignoring online naysayers.

3

u/braxtons12 Aug 25 '21

Yeah this is not my experience. My experience is the community as a whole generally says "don't do x" because it's a lot easier and less error-prone to say "don't do x" to encourage good practices by default, than it is to say "don't do x, unless you really know what you're doing and are willing to deal with the possible consequences and promise you won't go ranting about how the next standard broke your code when really it was your fault because you did x". This way you don't end up with people who have one year of experience, thinking "they know what they're doing", writing questionable things and then wondering why a compiler or standard upgrade broke their code.

2

u/ohell Aug 25 '21

Amen! As a scientist who has to program in C++ when I don't find any existing tools that will do the job, and data is too much for python to handle, I just want to write a program and be done with it.

Personally I feel that C++ barrier to entry appears artificially higher because everyone pretends they are writing a library that the whole world will use for the next 50 years!

So many recommended practises only make sense in the context of writing libraries. For writing applications I just throw something together using as many standard containers and algorithms as possible, and if performance is a problem then I'll run perf to see if I replacing a map with unordered_map, passing by reference etc might help.

3

u/[deleted] Aug 25 '21

if performance is a problem then I'll run perf to see if (...) passing by reference might help.

Outch... I understand that you are not writing C++ every day and then it my be difficult because you need to ask yourself each time if you need to copy or reference (or move... don't forget the move please). But actually it is the very basics of C++.

Actually, the default (if any) should be to const ref when speaking about complex object like std container, not to copy. Ask yourself when you need to copy.

1

u/ohell Aug 25 '21

So, I have teammates who do worry about this stuff. And for a laugh I have changed the code for a long running task to use references, unordered_map etc. For a task that runs on the cloud in 2 hour, saving is < 2 minutes. At AWS prices that is 4c. I am happy to say that all of us are paid well enough that spending even 1 extra hour refining code this way will only pay off if our program was run ~5000 times :)

Having said this, I can't wait for the dream to become reality

1

u/[deleted] Aug 25 '21

in : X (by value) if cheaply trivially copyable (defined by platform ABI) Otherwise, X*

Oh no please, it is completely unmanageable!

I am happy to say that all of us are paid well enough that spending even 1 extra hour refining code this way will only pay off if our program was run ~5000 times

It depends, for the reference case just write it by ref by default. It doesn't take time at all. For map and unordered map, I agree with you.

1

u/Grafakos Aug 25 '21

I have come around to loathe this argument. If there are naming collisions, compiler (and IDE) will tell you.

Sure, but what good does that do you when the header is maintained by some other team, or vice versa, if some other team is using your header that causes naming collisions?

1

u/ohell Aug 25 '21

Yeah, writing libraries is obviously a different kettle of fish - have to ensure all users of the library don't run onto issues.

And like I said above, it is unnecessary to apply library-writing guidelines when writing programs - waste of time, readability, keyboard durability.

1

u/Adverpol Aug 25 '21

fwiw I agree with you, I don't understand why people don't mind having std::vector<std::pair<std::string, std::string>> instead of vector<pair<string, string>>. I do think that gets in the way, and no I don't think adding using to the top of my cpp file helps because that means that all of my header files, i.e. the public API, still has this clutter.

3

u/braxtons12 Aug 25 '21

The short version of the reason is we like things to not break our, or our users', code.

Doing a using namespace std; is asking for ADL issues to happen eventually.

If you don't want to deal with verbose type names, it's fine to do a using for a type, eg using std::vector; or similar, in a source file, but don't pollute your headers with it and force users to deal with that.

I would also argue that explicitness is not clutter, it makes readability and maintainability better because you immediately see where things come from.

For reference on why it's bad, along with a lot of other things you should not do, see Titus's talk from a few years ago: https://youtu.be/BWvSSsKCiAw

Or the associated paper (SD-8): https://isocpp.org/std/standing-documents/sd-8-standard-library-compatibility

1

u/Adverpol Aug 26 '21

I would also argue that explicitness is not clutter, it makes readability and maintainability better because you immediately see where things come from.

I keep reading this with respect to std::, and I couldn't disagree more. I don't think it adds anything but unnecessary clutter.

I would just like it that there are a couple of standard types that are "just there", like vector, string, tuple, pair, ... You know, the way rust does it... All of the reasons not to do it are the reasons why I do use std:: when writing code, but that doesn't mean I have to like it.

1

u/braxtons12 Aug 26 '21

Except in Rust, "tuple", and "pair" aren't library items, they're language constructs, and Rust doesn't suffer from ADL, overloading, or ODR, so the ONLY problem that can happen is a build break.

1

u/Adverpol Aug 27 '21

I'm not sure what the point is you're trying to get across? Mine is that I think having to use std:: everywhere hurts readability. I understand that there are reasons that we are stuck with this, that doesn't change the fact that I think it's a shortcoming of C++. I do type std:: for all my types, I just don't like it, and I disagree with people arguing that it's actually a good thing.

1

u/braxtons12 Aug 27 '21

You used Rust as example of how you would like C++ to behave when it comes to standard library features. My point was Rust only gets away with how it does things because it doesn't have to deal with any of the complicated features we have in c++ that make c++ what it is, so using Rust as an example of how things could or should be in this regard is rather silly.

Also, IMO shortcoming implies something that could be changed or fixed in a relatively compatible way but just either hasn't been or isn't going to be. But to change this, it would require changes so drastic it would literally be a different language.

1

u/ohell Aug 25 '21

And then,

std::for_each(std::begin(vector_of_string_pairs), std::end(vector_of_string_pairs), [] (auto &&sp){ std::cout << std::get<0>(sp) << ' ' << std::get<1>(sp); });

std::fml ¯\(ツ)/¯

1

u/braxtons12 Aug 26 '21

Why would you not just:

std::for_each(vec.begin(), vec.end(), [](auto&& p) {
    std::cout << std::get<0>(p) <<  ' ' << std::get<1>(p);
});

Or even cleaner:

for(auto&&[first, second] : vec) {
    std::cout << first << ' ' << second;
}