Defensive programming?

This is a discussion on Defensive programming? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by cpjust It will only clash with names that you #include. If you only #include <iostream> and decide ...

  1. #31
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by cpjust View Post
    It will only clash with names that you #include. If you only #include <iostream> and decide to use variables named 'string' (a bad idea since you might decide to use STL strings in the future) then there won't be any problems.

    If you do have name conflicts (extremely unlikely in my experience) then you have two choices:
    • Rename your variable or function to something else.
    • Explicitly specify the namespace that the name belongs to (std:: or whatever:: )
    But still, if i only included <iostream> in a program, why not just use "using std::cout" and "using std::cin", instead of putting the entire std namespace in scope? Besides, i don't see much of a point in namespaces if you put "using namespace std;" in all your code?

  2. #32
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by cpjust View Post
    It will only clash with names that you #include. If you only #include <iostream> and decide to use variables named 'string' (a bad idea since you might decide to use STL strings in the future) then there won't be any problems.
    Fine if you're just making programs all in standard C++ - But larger projects tend to involve all sorts of 3rd party libraries from different sources, and maybe legacy 'C' code which hasn't been written with the STL in mind. Name clashes aren't common in small programs, but in bigger ones involving code written by different people, they can cause alot of aggro.
    Namespaces, when used well, reduces the problem quite alot, but abusing the using namespace directive can undermine their purpose. Really its an issue of coding habits - if you can get into good habits, even when those habits seem unnecessary, you're less likely to fall into hidden traps when it matters.

  3. #33
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    In short: You can and should use namespace using declarations and directives liberally in your implementation files after #include directives and feel good about it. Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable.
    ...
    Most people understand viscerally why a using directive (e.g., using namespace A; ) causes pollution when it can affect code that follows and that isn't aware of it: Because it imports one namespace wholesale into another, including even those names that haven't been seen yet, it's fairly obvious that it can easily change the meaning of code that follows.

    But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widget; ) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way.
    Read the rest of it at this site:
    http://www.ubookcase.com/book/Addiso...9lev1sec2.html
    Read the whole book online or buy a real copy like mine. It's worth it.

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> If you add using namespace std; after your #includes you don't need to prepend std:: all over the place.

    I agree with Neo1 on this issue. This advice is not "better" style. Whether it is worse or not, I guess can be debated (I think it is), but it is not better.

    It is very simple. Using a using declaration or using directive in any scope can lead to bugs in your program. Placing std:: in front of names from the std namespace will remove that specific possibility (however remote). The C++ Coding Standards advice should be considered, but it applies to other namespaces much more than it does to the std namespace.

    While I wouldn't tell somebody not to use a using directive or declaration for the std namespace in a source file or in function scope, I don't see how you can advise somebody that they should be using it.

  5. #35
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't think there is anything inherently wrong with individual using statements vs. using namespace, but using namespace saves some time when you are using a lot of different objects from the same namespace. If later you find out you have a conflict, you can easily switch to individual using statements...

    The main point here is to put using something to make the code a little cleaner and easier to read.

    Whether you choose individual usings vs. using namespace, it all comes down to: How much is your time worth? vs. How likely is it that a name conflict will occur in the file you're working on?

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> The main point here is to put using something to make the code a little cleaner and easier to read.

    My main point is "don't do that" and "don't advise people to do that". If somebody prefers to explicitly put std:: in front of everything, why advise the more dangerous solution? You think the code is cleaner and easier to read. I disagree. I think putting std:: in front of everything makes the code easier to read.

    There have been multiple posts on this site and others where the issue was solved simply by switching from a using directive to explicitly typing std::. It may be rare but it does happen.

    If your preference is to use using declarations or directives, fine, but I strongly disagree that the alternative is in fact worse.

  7. #37
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If they fixed the problem by explicitely qualifying the name then what's the problem? In those specific cases, it was required, but 99.999% of the time it doesn't matter which way you go. The using declaration was added to make life easier. You can shoot yourself in the foot with any tools, but that doesn't mean you should stop using them.
    If you do run into a problem, then by all means, go ahead and fully qualify the name, but otherwise just use the tools available to you.

  8. #38
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> If they fixed the problem by explicitely qualifying the name then what's the problem?
    The problem is that they didn't know what the error was and only when they posted it to a programming forum was somebody able to figure it out. The errors can be subtle and cause issues that don't always happen at compile-time.

    The using declaration was intended to make your life easier in some cases, but that doesn't necessarily include the std namespace. Explicitly typing std:: is not harder, IMO, and it is better.

    My point is not that you should stop using using directives and declarations, my point is that you shouldn't tell people that they are better than explicitly prefixing the name with the namespace specifically when it comes to the std namespace.

  9. #39
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    OK, it's better for the compiler if you explicitely qualify the names, but it's better for humans to remove the clutter. I prefer to lean more towards the readability side. I tell people what works best for me. They can either use it, or not.

  10. #40
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by cpjust View Post
    OK, it's better for the compiler if you explicitely qualify the names, but it's better for humans to remove the clutter. I prefer to lean more towards the readability side. I tell people what works best for me. They can either use it, or not.
    You may be of the opinion that omitting std:: increases readability, But then this is getting into the territory along the lines of "Always use <some coding style> to name your variables/functions/classes/etc". and such discussions generally emit more heat than light.

    If readability is solely behind the "using" argument, then I would argue that typing fully-qualified names also explicitly documents your intention to use a name from that class/namespace (As opposed to something from another library or from elsewhere in your own code with a similar or same name).

    In any case, if you feel comfortable with the syntax enough to use it all the time in your own code, then there's certainly no argument to be doing otherwise. On the other hand, if you don't feel comfortable with it, then don't use it, that's fine aswell.

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Using a using declaration or using directive in any scope can lead to bugs in your program.
    Writing code can lead to bugs in your program (thus the larger and more complex the software, the more bugs it probably has). So, that using a using declaration or using directive in any scope can lead to bugs is of no consequence. What is important is whether the use is appropriate, e.g., a using declaration when overloading a member function of a base class in a derived class is undeniably a Good Thing.

    Placing std:: in front of names from the std namespace will remove that specific possibility (however remote).
    I agree that always fully qualifying names from the std namespace will avoid such bugs. On the other hand, as you say such bugs with respect to using declarations and using directives are a remote possibility that is especially unlikely when used in a limited scope such that the potential error is more easily spotted and corrected.

    The C++ Coding Standards advice should be considered, but it applies to other namespaces much more than it does to the std namespace.
    What is your reasoning? It seems to me that chances of name conflicts from a namespace other than the std namespace may be even greater in view that library designers are more likely to avoid names that may conflict with those from the std namespace.

    While I wouldn't tell somebody not to use a using directive or declaration for the std namespace in a source file or in function scope, I don't see how you can advise somebody that they should be using it.
    Personally, I would rather give a code example with names fully qualified than use a using declaration or directive for brevity's sake and then have to explain the appropriate use of the using declaration or directive (thus removing my gain of brevity, heheh).

    However, I can see how one would advise someone else to use using declaration and directives properly: they are useful to reduce unnecessary typing, and in that sense, reduce code clutter. Daved, you recommended Accelerated C++, and Koenig and Moo introduce using declarations with that in mind in chapter 2.5. Admittedly they probably also had the ulterior motive of reducing code length in the book, but they do make it clear that a possible name conflict can occur, e.g., if the reader goes on to define something named cout.

    The using declaration was intended to make your life easier in some cases, but that doesn't necessarily include the std namespace.
    You might have forgotten that using std::swap is useful when implementing a swap as it would use a free function swap (likely to be more optimised than the generic swap) if it exists, or std::swap if such a swap function does not exist. Okay, that is probably nitpicking , but yeah, the usefulness of using declarations does extend to the std namespace, even if one prefers to prefix std:: under normal circumstances.

    If readability is solely behind the "using" argument, then I would argue that typing fully-qualified names also explicitly documents your intention to use a name from that class/namespace
    A using declaration would have the same effect, especially in a limited scope.

    In any case, if you feel comfortable with the syntax enough to use it all the time in your own code, then there's certainly no argument to be doing otherwise. On the other hand, if you don't feel comfortable with it, then don't use it, that's fine aswell.
    I agree.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #42
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Just my two sense but at college/uni many of my proffessors encourged us to do this:

    Code:
    using std::cout;
    using std::endl;
    
       cout << "Hello World!" << endl;
    Rather than:

    Code:
    std::cout << "Hello World!" << std::endl;
    Just the way I was taught, and as that is how I have been coding I would never use the std:: prefix against the using declarations. But I do see the point in it as for one it can reduce having to type all the using declarations under the header file eg:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    #include <string>
    
    using std::string;
    using std::getline;
    But like I said I was taught this was the better thing to do, and that is the practice I have followed since. But I do agree at the end of the day this is totally a style issue over a programming essential. What I do see more rarely is

    Code:
    using namespace std'
    begin used as a short cut. At least the using decllarations show at least some effort on the coding side. Even large industiral console programs to do not use the above anymore. It is good to see it is a practice dying out.
    I'm just trying to be a better person - My Name Is Earl

  13. #43
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> So, that using a using declaration or using directive in any scope can lead to bugs is of no consequence.

    It is of consequence. If you have two options, one which can lead to bugs and another that removes that possibility, then the latter has an advantage over the former. In this case, some consider the former to have separate benefits, e.g. easier to read code. I personally disagree that those separate benefits exist, and even if one believes they do, that is a personal style choice that should not easily overrule the reduction of bugs.


    >> What is your reasoning? It seems to me that chances of name conflicts from a namespace other than the std namespace may be even greater in view that library designers are more likely to avoid names that may conflict with those from the std namespace.

    I was thinking it would be the other way around. I would imagine most specific libraries have far fewer names. I can see an argument for people not knowing the names in the std library as well as for people not knowing the names in another library. This isn't a major point, though.


    >> I can see how one would advise someone else to use using declaration and directives properly

    I don't mind books teaching it. The two mentioned here are my two favorite books to recommend. I just disagree with implication that explicitly using std:: is wrong and that adding using declarations or directives is preferred.


    >> A using declaration would have the same effect, especially in a limited scope.

    Only in limited scope would it have that effect, and the scope would have to be very limited.


    >> OK, it's better for the compiler if you explicitely qualify the names, but it's better for humans to remove the clutter. I prefer to lean more towards the readability side. I tell people what works best for me. They can either use it, or not.

    This advice I'm fine with. Apparently I read the original statement as saying that using declarations/directives were better rather than another choice, which as I read it again I probably read too much into it. Personally I think they make things less readable for humans (the compiler doesn't care about readability, only the potential for bugs that can be caused). I also like to be consistent, so using std:: in a header means I might as well use std:: in the source file as well. But either way, I think you're fine with your suggestion and Neo1 is fine choosing not to follow it.

  14. #44
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Daved View Post
    >> If they fixed the problem by explicitely qualifying the name then what's the problem?
    The problem is that they didn't know what the error was and only when they posted it to a programming forum was somebody able to figure it out.
    So, your argument is that the feature is bad because newbies have trouble with it? Let's toss out pointers as well, then.

  15. #45
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> So, your argument is that the feature is bad because newbies have trouble with it? Let's toss out pointers as well, then.

    You actually just made my argument for me, even though you misunderstood it. In C++ we prefer to use other solutions over pointers in many cases because pointers are more error-prone. Likewise, a reason to explicitly state the namespace is because not doing so is more error-prone.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help please.. linkedlists/classes
    By swayp in forum C++ Programming
    Replies: 10
    Last Post: 01-18-2005, 04:14 PM
  2. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 12:36 PM
  3. Bowling for Columbine
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 97
    Last Post: 09-09-2003, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21