[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

I have a C++ question, I'm fairly new to programming and

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 20
Thread images: 1

File: C.png (5KB, 550x380px) Image search: [Google]
C.png
5KB, 550x380px
I have a C++ question,

I'm fairly new to programming and I'm wondering why people say it's bad practice to
 using namespace std; 


I was wondering if anyone could explain the reasoning behind this to me.
>>
You should stop using C++. It's complete garbage.
>>
>>60138798
because when you use
 std::cout 
, it will force you to understand namespace better.
>>
>>60138815
My uni does this thing where they'll make you take a classes that use Java for one semester, the C++ for two, and then Java for another. I just finished an OO class and I feel like every time I get into one language I get pushed to another. It's probably a good thing, nothing wrong with learning another language, but it seems hard to get really good with either language that way.

>inb4 Java and C++ are shit-tier.
>>
>>60138826
In what capacity?
>>
>>60138798
its (((bad practice))) because when you inevitably scale your code size, no matter how mundane of a problem you are solving, you're prone to namespacing errors that might not be caught by the compiler.

if you're solving simple math problems, doing algorithms w/e, use it who cares. if you're working on a large project with multiple namespace definitions, you will probably be smart enough to stear clear of namespace std and use std::
>>
>>60138845
Java and C++ are shit-tier.
>>
>>60138898
Thanks man, that actually makes a lot of sense.
>>
>>60138921
kek
>>
>>60138798

Because the std namespace contains a huge amount of things with very common names that will conflict with anything you put in your global namespace. It's unreasonable for every person to memorize the name of everything in the standard library, so you can't rely on that to stop you accidentally colliding with them.

Newer C++ standards add to the std namespace too, which means your code might be correct on C++14 and break in C++17 because a new standard library feature conflicts with one of your names. Good examples are std:: byte, span, apply, invoke, variant, optional.
>>
>>60139026

Also if you use "using namespace std;" in a library header you push this problem on to all of your users. If you try putting it inside of your library's namespace, then you're now exposing the contents of the std namespace through your own namespace, e.g., everyone's IDEs are going to show ypurlib::string, yourlib::vector, etc. as existing.
>>
>>60138845
CS isn't just there to teach you programming.
>>
>>60138798
There's no reason. use namespaces man it's good. fuck writing std:: before shit.

Also cout is fucking slow garbage, use sparingly.
>>
>>60138798
Because the C++ standard library is absolutely terrible, and most large organizations just write their own instead.
>>
using namespace std;
using namespace boost;

master race reportin in
>>
>>60140691
in general, print sparingly, use a debugger if you are looking for something.
>>
>>60138815
>>60138921
>>60140807
you people are so angry, what went wrong? :3c
>>
Google it?
There's a stackexchange discussion about it.

I get you're lonely and just want to talk. But you should not go to /g/ for that.
>>
Lets say you want your own implementation of string class.
now, if you do using namespace std, you wont be able to call it, cos compiler wont know what to do.
But if you do separate it to its own namespace, you'll freely be able to specify which string you want.

std::string or lets say myns::string.
>>
>>60138798
1. basically you don't want to have two libraries that provide the same functionality.
2. Your IDE interprets the code so it can figure out the context. Having a huge search space means it takes more time for the IDE to parse the code which is a waste of time.
3. It gets more readable.
You can easily read where the functions are coming from, which means the IDE functionality is not a requirement for the code to be read.

One thing I don't like is when you have to use cout.
I often put it in so I so I quickly can see some values or something and it takes some time to write.

There is generally two solutions to that problem.
The first is to make a wrapper so you basically just write
LOG("some" << val)
WARN("some" << val)
ERROR("some" << val)
Or whatever syntax you prefer.
I would keep the stringstream syntax because you can then use the operator overloads to have a dedicated print functionality.
This is generally a good thing to do anyway because now you can control what to print and when. You can control where to print it to, say a log in a graphical application or to a file or to the terminal.

The other is to use your editor and make a snippet.
I do this for a lot of things, for loops, printing, making basic class structures etc.
Thread posts: 20
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.