5 ms·
https://github.com/baidu/bfs/blob/master/src/client/bfs_client.cc https://github.com/baidu/bfs/blob/master/src/client/bfs_clie... std::string pad; if (
by GoToRO 10y ago
https://github.com/baidu/bfs/blob/master/src/client/bfs_client.cc https://github.com/baidu/bfs/blob/master/src/client/bfs_clie...
std::string pad;
if (path[path.size() - 1] != '/') {
pad = "/";
}
Else?
- toxik 10y agoThe value constructor for strings is the empty string.
- GoToRO 10y agoI imagined that. I am more like "let me tell you exactly what I want" rather than relying on behavior defined elsewhere. It would not pass review where I work.
- knorker 10y agoNot to be rude, but I'm glad I don't work where you work. How would that be consistent with your own classes? "Oh no, you can't just use a 'Tree' object, you need to explicitly set that there are no leaves yet, no branches yet, no squirrels yet, etc… etc…" Do you .clear() your vectors before you use them? This sounds like newbies that do: #define TRUE (1 == 1)
- GoToRO 10y agoIt's not about initialization, it's more about specifying clearly what happens in all cases. Anyway the reason for which it would no pass review is that today you use one compiler, tomorrow you have to use another and then you have to review all these little details again. It's about saving money more than anything and you do that by not relying on compiler behavior.
- pritambaral 10y ago> the reason for which it would no pass review is that today you use one compiler, tomorrow you have to use another Good thing then that it's mandated by the language reference, and not up to the compiler to decide. According to C++11, §21.4.2/1, an uninitialized std::string should be an object of class std::basic_string with non-null data and a size of 0.
- bn-usd-mistake 10y agoIt is specified clearly though. The behavior is not compiler dependent, it's specified in the C++ Language standard. See http://en.cppreference.com/w/cpp/language/default_initialization http://en.cppreference.com/w/cpp/language/default_initializa... and http://www.cplusplus.com/reference/string/string/string/ http://www.cplusplus.com/reference/string/string/string/ If a different compiler breaks this behavior, it's not standard compliant and thus could do all sorts of stuff in every possible line, including in: std::string pad = "";
- knorker 10y agoSure, I would have preferred a non-branching: std::string pad = descriptive_name_here(path); with the added bonus of being able to add "const" to that, for the benefit of the reader. This is not relying on compiler implementation! Can you name one language that has strings that initialise to anything but a valid object containing an empty string? This is not an obscure side-effect. This is like assuming "std::vector<int> v;" creates an empty vector, not a undefined-state vector container. (I don't want someone coding C++ as if all objects are references. Coding in one language as if it were another is a well-known antipattern)
- rbadaro 10y agoIn Java the default value for a String (or any object) is null, not an empty string.
- knorker 10y agoDid you not read my whole comment? Please read the whole thing before replying.
- rbadaro 10y agoRead the whole comment. Still don't see nothing that invalidates my answer to "Can you name one language that has strings that initialise to anything but a valid object containing an empty string?".
- knorker 10y agoYou specifically mentioned Java, and I specifically mentioned reference-based languages, with Java being the most obvious example.
- knorker 10y agoLooks like I can't edit, so I'll create another comment. I ran into this article that puts quite nicely why the problem isn't the "else", but the "if" itself: https://medium.com/@bartobri/applying-the-linus-tarvolds-good-taste-coding-requirement-99749f37684a#.pji8wp94m https://medium.com/@bartobri/applying-the-linus-tarvolds-goo... This is what I meant in the other comment by preferring the non-branching.
- GoToRO 10y agoThat define is very clever: it always lands on it's feet! :)
- knorker 10y agoExcept there's already a 'true'. When I see this I know that whoever wrote is completely incompetent (as in "does not know programming", not "is stupid"). It's clever, yes. The bad kind of clever that's also misguided.
- GoToRO 10y agoSometimes there is a 'true', sometimes there isn't. Sometimes the code is new, sometimes it is legacy code. You are making too many assumptions. When I see this I know that...
- knorker 10y agoThe point of defining true to (1==1) is that it's "future proof" in case implicit typecasting to bool works in a world where 0 evaluates to "true". That would break approximately ALL C code. You're being ridiculous. You might as well try to protect against the meaning of "if" changing. I've seen amateur code that tries to protect against "stdio.h" going away and therefore reimplementing everything in it. This is like that. Believing that the meaning of everything can change means that you cannot use anything you didn't code yourself. You can't trust documented APIs, then that's some sort of programmer NIH nihilist.
- stomato 10y agoIt's a distributed real-time filesystem. I'm guessing optimizations are key?
- GoToRO 10y agoProbably.
- noselasd 10y agoBut to a C++ programmer, that code tells exactly what is going on... There's no hidden logic, no non-standard or compiler dependent features.
- yorwba 10y agoI have not used C++ much, but isn't pad default-constructed to be an empty string here?
- yvxiang 10y agoIt's an interface that's no longer in use. Would you like to write an issue to us or make a pr to fix it? :)
- snnn 10y agowhat if path is empty?
- pkolaczk 10y agoThen they have a problem. Undefined Behavior.