{"id":5,"date":"2019-02-15T22:06:07","date_gmt":"2019-02-15T22:06:07","guid":{"rendered":"https:\/\/www.npasson.com\/blog\/?p=5"},"modified":"2019-02-20T13:04:12","modified_gmt":"2019-02-20T13:04:12","slug":"dont-use-using-namespace-std","status":"publish","type":"post","link":"https:\/\/www.npasson.com\/blog\/dont-use-using-namespace-std\/","title":{"rendered":"Don&#8217;t use &#8220;using namespace std&#8221;"},"content":{"rendered":"\n<p>I&#8217;m a big fan of never using <code>using namespace std<\/code>. There are a ton of <a href=\"https:\/\/stackoverflow.com\/questions\/1452721\/why-is-using-namespace-std-considered-bad-practice\">technical reasons<\/a>, but I want to talk about the readability part. Generally, if it is avoided, it&#8217;s easier for somebody checking your code to see which variables are yours. Sure, <code>cout<\/code> and <code>cin<\/code> are unmistakable, but what about <code>vector<\/code> or <code>hash<\/code> or <code>pair<\/code>? <!--more--> Again, people will have to scroll up to see where they were declared. And they won&#8217;t find them, since they weren&#8217;t declared anywhere. If I read code, I keep track of the function and object names. If I encounter a variable I don&#8217;t know, I need to find out where it came from. And if you randomly throw the word <code>vector<\/code>  into the code, it will confuse me a lot more than using <code>std::vector<\/code>, which is <em>instantly<\/em> obvious.<\/p>\n\n\n\n<h2>But what about partial imports?<\/h2>\n\n\n\n<p>Partial imports are things like <code>using std::cout<\/code>. I don\u2019t like them. I give them out as advice for people who just won\u2019t drop the whole <code>using namespace std<\/code>, but I personally don\u2019t like them. If you want to use a namespace because the lines would be too long, do it locally, i.e.:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">double calculate(double x) {\n    using namespace std;\n    return floor(sin(x)\/cos(x)) + ceil(cos(x)\/2);\n}\n\nvoid print(std::string s) {\n    \/\/ using directive doesn\u2019t apply here!\n    std::cout &lt;&lt; s &lt;&lt; std::endl;\n}<\/pre>\n\n\n\n<p>But generally I\u2019m against the <code>using<\/code> directive. If you want it because of otherwise long names long names you have a few options:<\/p>\n\n\n\n<h3>Using <code>auto<\/code> <\/h3>\n\n\n\n<p>Quick, what does this code do?<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;std::chrono::high_resolution_clock::time_point> vec;\nstd::vector&lt;std::chrono::high_resolution_clock::time_point> vec2;\n\n\/\/ fill both vectors\n\nstd::vector&lt;std::chrono::duration&lt;long long int, std::ratio&lt;1, 1000000000>>> durations;\n\nfor(std::chrono::high_resolution_clock::time_point&amp; tp : vec) {\n    for(std::chrono::high_resolution_clock::time_point&amp; tp2 : vec2) {\n        std::chrono::duration&lt;long long int, std::ratio&lt;1, 1000000000>> dur = tp - tp2;\n        std::cout &lt;&lt; \"Measured: \" &lt;&lt; dur.count() &lt;&lt; std::endl;\n        durations.push_back(dur);\n    }\n}<\/pre>\n\n\n\n<p>No clue? If we use <code>auto<\/code> a bunch of times (and one <code>decltype<\/code>), it will become apparent:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6,8-10\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;std::chrono::high_resolution_clock::time_point> vec1;\nstd::vector&lt;std::chrono::high_resolution_clock::time_point> vec2;\n\n\/\/ fill both vectors\n\nstd::vector&lt;decltype(vec1[0]-vec2[0])> durations;\n\nfor(auto&amp; tp1 : vec1) {\n    for(auto&amp; tp2 : vec2) {\n        auto dur = tp1 - tp2;\n        std::cout &lt;&lt; \"Measured: \" &lt;&lt; dur.count() &lt;&lt; std::endl;\n        durations.push_back(dur);\n    }\n}<\/pre>\n\n\n\n<p>Not only is the code easier to read, but we also avoided <code>using namespace std<\/code>! (Even that wouldn&#8217;t have helped here, we could just have removed the first, small part. Of course, one could have used  <code>using namespace std::chrono<\/code>, but that introduces yet <em>another<\/em><code>using<\/code> directive.)<\/p>\n\n\n\n<h3>Using <code>typedef<\/code> <\/h3>\n\n\n\n<p>Of course, <code>auto<\/code> comes with a lot of trouble if something is deduced incorrectly. So sometimes you need to actually explicitly give a type. One of my favorite ways to do that is using a <code>typedef<\/code> struct.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct timer {\n    typedef std::chrono::high_resolution_clock clock;\n    typedef std::chrono::duration&lt;long long int, std::ratio&lt;1, 1000000000>> duration;\n    \/\/ ...\n}<\/pre>\n\n\n\n<p>Now you can use <code>timer::clock<\/code> or <code>timer::duration<\/code>  instead of the long name. Generally, in my opinion, <strong>everything that isn\u2019t declared locally should have a prefix<\/strong> of some sort.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of never using using namespace std. There are a ton of technical reasons, but I want to talk about the readability<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.npasson.com\/blog\/dont-use-using-namespace-std\/\">Continue Reading<span class=\"screen-reader-text\">Don&#8217;t use &#8220;using namespace std&#8221;<\/span> <i class=\"fas fa-angle-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":""},"categories":[3,2],"tags":[4,6,5],"featured_image_src":null,"author_info":{"display_name":"npasson","author_link":"https:\/\/www.npasson.com\/blog\/author\/npasson\/"},"_links":{"self":[{"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/posts\/5"}],"collection":[{"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/comments?post=5"}],"version-history":[{"count":10,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/posts\/5\/revisions"}],"predecessor-version":[{"id":36,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/posts\/5\/revisions\/36"}],"wp:attachment":[{"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/media?parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/categories?post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/tags?post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}