Tuesday, August 23, 2011

Why cout is Stupid

Maybe it's because I started with C and not C++, but I absolutely hate cout. I'm talking about std::cout. You know, the C++ interface that every beginning C++ coder is taught in their very first program. Here is something what the program might look like.


#include <iostream>

int main()
{
std::cout << "Hello, world! I am using cout." << endl;
return 0;
}


I don't even know where to begin with my hatred of cout. Part of it, is because the above example is exactly how C++ is taught, and the code above makes little sense until the programmer learns advanced topics such as operator overloading.

I mentioned that I started with C, but before that I actually started learning C++, and the above example was exactly what I saw. There is absolutely nothing standard about that example. It doesn't really give you an impression of what a function is. It doesn't give you an impression of what a class is. It just makes C++ look really freaking weird. I mean serious "<<" to print out text? Why the heck would "<<" be used to print out text? And what the heck is "cout"?

You know what makes more sense. The following example:


#include <stdio.h>

int main()
{
printf("Hello, world!\n");
return 0;
}


The above example makes a lot more sense. So you are saying, "Print 'Hello, world\n'"? Yes, that is exactly what I am saying. Print something. Granted, there is a confusing "\n" in there, which can easily be explained as "a code for a new line", and why the heck does print need an 'f' at the end is a little unusual, but the code makes sense. It is less like the "magic" of cout somehow putting text on the screen..

That's the next big gripe I have with cout, the fact that operators are overloaded for output. I can understand why cout is a class, as opposed to being a function, it is C++, it is object oriented, but using operator overloading to print? That is just way too confusing to a new coder. I would be a little more sympathetic to cout if the print function was more like this "cout.print("Hello.\n")" (which is actually what Java does with System.out, and Java, I approve of). This is still object oriented, and it won't be so confusing to the new programmer. They may wonder why they need periods everywhere, but other than that, at least they see the word print.

Of course, there might be the complaint that with cout you can concatenate strings, as in the example:


std::cout << "Two plus three is " << (2+3) << ". Isn't that nice?" << endl;


Indeed, I can see how someone could make that argument, but let us look at the alternative.


printf("Two plus three is %i. Isn't that nice.\n", (2+3));


Now, I will grant you that my alternative might actually be a little bit confusing, after all: What the heck is %i? The cout version sort of makes it clear that the sum (2+3) will appear right after the word "is", but it still has the problem of using "<<". Java's String class makes much more sense than cout. The cout example would make much more sense if it was something like:


std::cout.print("Two plus three is " + (2+3) + ". Isn't that nice.\n");


Which is actually something you could almost pull off in C++, it would probably have to be something more like this:


std::cout.print(CString("Two plus three is ") + (2+3) + ". Isn't that nice.\n");


But at least this makes a little more sense than "<<".

Finally, what really bothers me about cout, is that it is only usable in the console. Once you go to windows programming, or the like, you will inevitably need to format a string for output, and cout isn't going to do that for you, the trusty "sprintf" will, though, and if you know how to use "printf", "sprintf" is about as easy.

Okay, I know that you can argue that you can use std::string or std::wstring to format text in a similar way that cout does, and that is probably true. In fact, I have not gripes against std::string, it makes a lot more sense than cout. I understand why object oriented programmers want everything to be an object. I kind of like the fact that everything in Java has to be a class.It forces the practice of OOP. However, the fact is that valid C functions are valid in C++, so why not use them?

I spoke of Java a little. I prefer to use "System.out.print(String.format("Hello, %s!\n", "world"));" instead of "System.out.println("Hello, " + "world" + "!");" (Granted, that that particular example is stupid.)

The point I want to make is that I just can't get past my hatred of cout. Whenever a new C++ programmer wants me to have a look at their code and I see "cout" (or even worse, "cin") I wince a little. I want to tell them they should use printf. I want to tell them that other than programming DOS style applications they will never use cout again. The worse thing is when they ask why cout works, and all I can say is, "Well, cout is an instance of a class that has the left shift operator overloaded to send a string to the OS for output to the console... It's magic".