0

I've taken 2 classes on C++ so far, one each at a different school, and both of them have used 'using namespace std;' to teach basic programming. It may be a coincidence but I had to go out of my way to find out it's not a good practice to do so.

gr33kbo1
  • 303
  • 4
  • 5
  • 15
  • 8
    Why are you asking us? Ask the schools! – NPE Sep 03 '13 at 14:46
  • 3
    Given the size of programs people are writing in C++ classes, I can't imagine there's any downside. Anyway, this question is 100% opinion-based. – Carl Norum Sep 03 '13 at 14:48
  • 2
    This is not really on topic for SO; there is no problem to solve here. – nikolas Sep 03 '13 at 14:50
  • 4
    Because the professor usually intends to explain everything that appears inside `main()`, while leaving everything outside as *magic-incantation-boilerplate*. `using namespace std;` removes `std` from the set of things requiring explanation. (BTW: [Curses](http://en.wikipedia.org/wiki/Curses_(programming_library)) on those who teach [cargo cult programming](http://en.wikipedia.org/wiki/Cargo_cult_programming) like this) – Ben Voigt Sep 03 '13 at 14:50
  • 5
    This is an excellent question, and you make an excellent point in asking it. However, Stack Overflow is the wrong forumn for this discussion. Voted to close. – Robᵩ Sep 03 '13 at 14:59
  • 1
    5 characters less on each use in the slide... optimizing for slides rather than maintainability? – David Rodríguez - dribeas Sep 03 '13 at 15:33

2 Answers2

5

Because best practices when writing sample code are not necessarily best practices when writing large projects.

In a C++ course, you write mainly small programs (up to a few hundred lines of code) that have to solve a relatively small problem. This means little to no focus on future maintenance (and avoiding sources of confusion for future maintainers).

Because many teachers simply do not have coding experience in large projects, the problem doesn't even get acknowledged (let alone discussed) in most C++ courses.

utnapistim
  • 26,809
  • 3
  • 46
  • 82
3

Because college computer science professors do not necessarily know how to write good code.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • You cannot claim that if there is "using namespace std", it is a "bad" code. – khajvah Sep 03 '13 at 15:11
  • @khajvah And if they never explain it or its implications to the students? Do they even know? – Neil Kirk Sep 03 '13 at 15:12
  • @khajvah I claim that. So there. – juanchopanza Sep 03 '13 at 15:13
  • @NeilKirk It is another thing that they don't explain (well I had such experience with professors) but when working on small projects(like in college) you cannot prove that using that line is wrong practice... – khajvah Sep 03 '13 at 15:16
  • @juanchopanza Good code for me is ... short, intelligent and fast code. It really doesn't matter if you use that line or not (at some point you will have to stop using that though) – khajvah Sep 03 '13 at 15:18
  • @khajvah Well my comment wasn't just on that one thing but my experience of them in general. – Neil Kirk Sep 03 '13 at 15:38