5 ms·
It 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
by smitherfield 11y ago
It 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 11y 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 11y 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 11y 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.