4 ms·
But why couldn't it have been named std::eol_flush? or something else that communicates what it actually does?
by dicroce 10y ago
But why couldn't it have been named std::eol_flush? or something else that communicates what it actually does?
- jjnoakes 10y agoWho said it couldn't have been named std::eol_flush? And why do you think its name (std::endl) doesn't match its effect (ends the line)?
- smitherfield 10y agoIt does match one effect, but it doesn't match the most important effect (flushes the output stream). And so a lot of people get the wrong idea. (I had multiple experienced professors teach me that std::endl was the "C++ way" as opposed to "\n" being the "C way"). 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()
- jjnoakes 10y agoYou assume "ending the line" means only "move to the next line". But "ending the line" could just as easily mean "ensure the line reaches its destination". 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.
- smitherfield 10y ago>You assume "ending the line" means only "move to the next line". But "ending the line" could just as easily mean "ensure the line reaches its destination". 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.
- jjnoakes 10y agoYou would propose std::eof_nl and have it not mean "end of file something"? > [std::endl] is one of the most common sources of performance issues with the language. Citation needed.
- kazinator 10y agoBecause that name is too verbose for regular use. People would hate it and make their own ad hoc nicknames for it not to have to type eol_flush: // 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.