by sickbeard · 10y ago · 0 comments
However, sometimes endl is indeed what you want. There is nothing to "fix" here, except maybe your attitude.
This violated the principal of least surprise enough that someone made a 10 minute long video on it :) It's really hard to blame the user this time.
It was weird to ever merge two unrelated functions into something, and then name it something as cryptic as "endl". Especially when the most common case novices would encounter this would be std::cout, where terminals will automatically flush text to the screen upon receiving "\n" anyway.
// 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()The main thrust of that article [1] was suggesting a new way to use the existing class and pointing out common mistakes he'd seen. Yeah, the docs were slightly outdated and needed a little TLC. They didn't mention the alternatives that had recently been made possible, but that's hardly an earth-shattering mistake.