by dicroce · 10y ago · 0 comments
// ef -> std::eol_flush
template class <typename X, typename Y> basic_ostream<X, Y> &ef(basic_ostream <X, Y> &os)
{
return std::eol_flush(&os);
}
A four-letter name could have been found with better mnemonic value covering the full semantics. We can look toward assembly languages for inspiration: they pack a lot into one, two, three or four letters. For instance std::nlfl (add NewLine and FLush).It's been years since I used C++ iostreams. I totally forgot that endl also flushes. When I saw this submission, my reaction was: what is this fuss about. Then "oh, right! That endl manipulator flushes, even on a fully buffered stream!". The name didn't remind me of this at all; but "nlfl" would almost certainly have served that purpose.
And why do you think its name (std::endl) doesn't match its effect (ends the line)?
You always want to follow the Principle of Least Astonishment. Suppose a hypothetical programming language has two DSLs for interfacing with SQL databases. Which of these is immediately understandable without having to consult the docs? Which one are you more likely to "learn" in a way that litters your codebase with subtle bugs?
// Returns the SQL query as a string
DB.table("users").where("id").in($args).toSQL()
// Returns the results of the query
DB.table("users").where("id").in($args)
// Locks the DB connection and returns the results of the query
DB.conn.lock() { DB.table("users").where("id").in($args) }
vs. // Returns the SQL query as a string
DB.query.find("users", ["id", sqlSafe($args)])
// Returns the results of the query
DB.query.find("users", ["id", sqlSafe($args)]).run()
// Locks the DB connection and returns the results of the query
DB.query.find("users", ["id", sqlSafe($args)]).exec()If it was called std::newline I would agree with you about it being misnamed. But it isn't called that.
Could the name be better? Maybe. Bike shedding.
Besides the other suggestions, I'd say something like std::eof_nl would make that interpretation a lot more obvious. Although like others I'd question the need for it at all as opposed to just an explicit "\n" << std::flush .
>Bike shedding.
I don't think anything is "bike shedding" when it comes to defining the standard library of a programming language (especially one for which all updates to the standard must maintain 100% backwards compatibility).
(Not) choosing descriptive identifiers might not have the glamour of defining semantics or optimizing performance, but it can have huge real-world implications.
The (std::endl == "\n") misconception is enormously widespread even amongst experienced C++ people, and is one of the most common sources of performance issues with the language.