Why does cpplint discourage Streams?
Asked Answered
S

2

15

I was just playing around with cpplint and tried running it on some code I had written for fun. I realized that the following lines were flagged with the error message :-

#include <iostream>
...
#include <fstream>

yoohoo.cpp:3:  Streams are highly discouraged.  [readability/streams] [3]
yoohoo.cpp:5:  Streams are highly discouraged.  [readability/streams] [3]

I'm curious about why using streams is discouraged.

Sharma answered 12/10, 2012 at 13:26 Comment(0)
S
16

The main reason that streams may cause an issue is internationalization.

Whenever you need to generate text with dynamic content in a couple different languages, things get awry because each language has its own grammar rules. For example, in English you would use:

void print(std::ostream& out, int i) {
    out << "You retrieved ";
    switch(i) {
    case 0: out << "no file."; return;
    case 1: out << "1 file."; return;
    default: out << i << " files." return;
}

And that's great right ?

So when you translate to French, you simply decide to move all those 4 sentences parts in a table in which you will look them up by key, and it works!

And then you discover Polish, from the gettext documentation, here are the plural forms of file (plik):

1 => plik

2,3,4 => pliki

5-21 => pliko'w

22-24 => pliki

25-31 => pliko'w

Hum... suddenly things are getting difficult, right ?

Actually, it can get worse. Not all languages need to place your dynamic entries in the same order!

This is why streams cannot actually be used for internationalized text short of writing an overloadable C++ function for each text to display, and have the translators provide the overloads! Hum...

There are pros and cons for both, Google Style Guide is just very opinionated to ensure consistency as much as possible.

Staff answered 12/10, 2012 at 17:24 Comment(0)
B
4

Is this the one that checks c++ against the google c++ coding guidelines? If so, then the reason is that google's c++ guidelines are generally considered to be somewhat eccentric and not really applicable to what many people think is good practice for modern c++.

Bluefarb answered 12/10, 2012 at 13:33 Comment(3)
Here's their reasoning: google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Streams There's no need to follow their guidelines if you don't want to, of course.Crazy
Banning stringstreams is stupid. What do you want to use ? snprintf is very restricted and asprintf may not be available...Erfert
The google discussion fails to mention two important advantages of streams. Streams can be extended to new types. You cannot make printf accept new format specifiers. Also the streams interface is independent of the destination. With printf you have to use sprintf, fprintf etc, which makes writing destination independent code hard. That's said I still use printf frequently. I see no need to mandate one or the other.Haematogenous

© 2022 - 2024 — McMap. All rights reserved.