Adding a namespace vs adding std:: to every line of code [duplicate] - c++

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 6 years ago.
I've been coding for many years as a hobby and I'm now studying computer science which is a great way to answer the "why"s as well as the "how"s of the programming world. I've seen many great code examples across multiple message boards including this one and I'm wondering why some people would rather add "std::" to every single line of code rather than just adding "using namespace std;" once and never having to worry about it ever again. I understand some projects involve multiple namespaces, but what about the ones that only use "std::"? I hope someone might be able to shed some light on this, and I'm sorry if this comes off as a dumb question.
Thanks!

Well, if it is not a matter of coding style, people including myself here usually goes to "std::" to avoid name collisions that may happen when you start "using" a few namespaces in your code.
By specifying the entire scope (ex "std::string") of a type you are reducing the name collision risk to pretty much zero.

Related

Why is it not good practice to use 'goto'? [duplicate]

This question already has answers here:
What is wrong with using goto? [duplicate]
(6 answers)
Closed 5 years ago.
I read in the book I am learning C++ from about loops and it taught be goto first, but near the end it said not to use it, as it was bad practice, and said it created 'spaghetti' code. I don't understand what that means and why it is bad. Could someone please explain?
Spaghetti is a code poorly structured and which makes it hard to update because of multiple undocumented and unexpected links or dependencies. You touch it in one place and you see other things get broken or modified in an unexpected way. Just like sticky spaghetti, you pull one end and you see a number of places start moving.
GOTO usually violates the principles of structured, procedural programming, hence the term, which suggests the tangled and arbitrary nature of the program flow.

What does using something::something::something mean (c++)?

So I just started as an intern and the code that I'm supposed to be working on is scattered within like 10 different directories and 100 files and who knows how many namespaces. I've never worked with this before, and I'm really confused how they are all being linked together.
I keep seeing these sorts of statements one after the other, ex:
using something10::something2::something6;
using isThisANamespace::iHaveNoIdeaIfThisIsAClassNameOrWhat::something599;
using randomName::otherRandomName::randomName99999;
using something4::something3::something8766678788787987987698;
Whenever I try to google what using does, I only find results with using namespace. Is using now just a shorter way of using namespace? Some of the things they are using appear to be folder names and file names though. My company mentor doesn't know c++ so he can't help me alas.
Also, if these are all namespaces, then wouldn't using so many of them conflict? Can you call namespace::classname::function? And when you do this do you still have to include the filename for that class?
Please help me learn how to call a function buried in another directory that is also in 4 namespaces. I'm very lost.
This is a using-declaration. It introduces a single name from another namespace into the current scope so that it can be used without qualification. It's not the same as a using-directive, which begins with using namespace.
check below link, hopefully it will help you on what you are looking for
https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm
If there really just random naming, than it's very-very bad. BUT I hope it's just yours personally vision, as beginner in C++, and in reality there is a good code, but kinda complex.
I don't know yours background, what languages you know, so I would say that namespaces are kinda boxes. They can build very good, hierarchically structured, modules\subsystems. I suggest you to not panic, take paper and try to wrote out all namespaces, their relations, what is in them (classes, structures, maybe functions) and your own understanding them purposes.
P. S. sorry for bad English. This topic very close for me, but my writing skills in English are not very good for a free conversation about this. I hope moderators will fix typos.

Should I make my local reused data global in webservice in c++? [duplicate]

This question already has answers here:
Where should you put global constants in a C++ program?
(6 answers)
Closed 7 years ago.
I know this question has been answered many times and globals are bad but I have a variation. I have lots of regular expressions and I create local variables of those. Also, I have lost of strings which are constant in nature. This code is part of C++ web service. Thus this objects are getting created and destroyed all the time. Thus, I wonder if I should put them in as global inside a namespace so that I can still use them without polluting the namespace.
You might have your terminology mixed up.
By definition, variables which are in a namespace are not global. When people say "It's bad to have variables/functions/etc. defined globally", their express solution to that issue is usually to put those things in a namespace.

Difference in loading times with and without namespace

I'm fairly new to C++ so I hope this question isn't dumb, plus I've searched around and couldn't find an answer.
So this brings my to my question, is there any difference in loading times between calling std::cout and calling cout when using namespace std is added to the code? I imagine that if there is a difference it should be unnoticeable, but I'd like to be sure before turning in my project.
By the way, in my university we are told that using namespace std is okay (I've already read at lengths on that and whether it is good practice or not is not in question right now).
There is no difference. And performance should be the last of your concerns when making these kinds of "micro" code decisions.

Why are global variables bad? [duplicate]

This question already has answers here:
Are global variables bad? [closed]
(28 answers)
Closed 9 years ago.
Why are global variables considered bad practice in C++? Is there a time where they are acceptable to use? If so when, and where? Ive heard mixed answers to this question, some say if it works it works, and others tell me to avoid using global variables as much as possible.
There are several reasons why people often advise against using globals, but fundamentally they all center around maintennence. Programs that use globals are much harder to maintain than programs that don't.
For one, they introduce state to the entire application. This makes is much more difficult to find bugs that result from code reading from or writing to these variables that you didn't intend, or at a time when you didn't expect. This becomes more difficult the larger the application becomes. The more interractions with a single global there are, the more complex the application becomes and the more difficult to understand.
For another it becomes more difficult to make multithreaded access safe and efficient. Can you lock down one of these variables without locking down the entire application?
Using globals also obfuscates ownership semantics. Who exactly owns these variables? When should they be created? When should they be destroyed?
It is of course possible to write correct programs using global variables. These programs are easier to write (excepting threading issues), but tend to become much more difficult to maintain. Forcing yourself to not use globals makes it sometimes harder to write the program in the first place, but it also puts many of the issues to rest early and finally making these programs easier to understand, debug and extend.
Besides polluting the global namespace (which means code someone is writing that includes the declaration of such a global, might introduce a bug because it's referring to your symbol unwillingly), there is the Static Initialization Order Fiasco. A global is not static per se, but in any case the biggest problem is ensuring a valid state before first use.
Aside from that, global state is difficult to reason about and has the nasty side effect of watering down a strict separation of concerns in the code. Which leads to... Spaghetti monster of sorts.
Trust me, rewriting your code to use local variables instead will force you to clean up your design, and better code is a free result.