5 ms·
As a non-java programmer, I'm wondering why you would want to opt-in to a safety system instead of out of it? My instinct would be to say that you should be sa
by kbenson 10y ago
As a non-java programmer, I'm wondering why you would want to opt-in to a safety system instead of out of it? My instinct would be to say that you should be safe be default, and if you find you need performance, then you verify it won't cause problems in your case and opt out of the default safety mechanism. Why is defaulting to non-safe ever the right choice?
- kodablah 10y agoBecause of practicality. Think of it this way, "why would you want to opt-in to better performance instead of out of it?" The main reason behind sensible defaults is that they suit most for the best benefit. Whatever language you are familiar with, I can undoubtedly show you safer yet less convenient (and less frequently needed) ways to do things to which you could pose your same question.
- kbenson 10y ago> Think of it this way, "why would you want to opt-in to better performance instead of out of it?" Because it's unsafe some portion of the time? I don't think it's controversial to say safety should be the default, unless it imposes undue constraints. We have plenty of experience with the alternative. See C. > The main reason behind sensible defaults is that they suit most for the best benefit. Are we not talking about some small percentage of performance loss in string concatenation in exchange for a program that doesn't crash in the case it's used in a particular manner with threads? Am I misunderstanding this? > Whatever language you are familiar with, I can undoubtedly show you safer yet less convenient (and less frequently needed) ways to do things to which you could pose your same question. Yes. And when you point out where the same API exists for two constructs with different names, that achieve the same end, with small performance differences between them, and one is safe and one can possible cause the program to crash in some edge cases, I will side with the safer way as a default. It's not like we as an industry or hobby need help writing buggy software. We've accomplished that just fine for decades.
- kodablah 10y ago> Because it's unsafe some portion of the time? Yes, in the rare case of using string buffering across threads simultaneously. This "some" is so small as to have no practical benefits to default to the safe way. I don't know of any code that does it or would use it (and if that code did exist, you would opt in to the safety guarantees by using a different class for that rare purpose). > Are we not talking about some small percentage of performance loss in string concatenation in exchange for a program that doesn't crash in the case it's used in a particular manner with threads? Am I misunderstanding this? A bit. New code makes a conscious choice to be thread safe or not. I would not recommend anyone edit old code to improve performance. But for new code, thread safe string buffers are like thread safe anything else in the language, they are opt in because the default is assumed to not be thread safe unless you really mean it. All programming languages I know that support cross-thread racy invocations have a thread-unsafe-by-default approach to their stdlib. This is a reasonable approach/default for practical reasons. > I will side with the safer way as a default This is specifically why I used the word "practicality". In your mind I assume that there never should be such things as dynamic languages? If safety trumps all other forms of practical choice, your programming environment and library choice will be really limited. We can all theoretically tout we choose safety over everything else, but we all know we have to make practicality sacrifices for many reasons that are unrelated to safety.
- kbenson 10y ago> In your mind I assume that there never should be such things as dynamic languages? In what way are dynamic languages "unsafe"? To be clear, what I mean here by unsafe, and everywhere I've used the word, is "can cause a crash or memory corruption", which I consider different than "not the output I expected", such as weird order of operations. Can using one over the other cause a Java program to crash in certain circumstances? Then I advise the one with the possibility to crash not be the default, if at all possible. Is it extremely rare that it could cause a crash? Are we sure that will always remain the case? Could interesting developments later on the JVM make it markedly less rare (maybe some auto-threading loop constructs)? Perhaps we shouldn't use current common current behavior as a justification for what will be safe in the future? Is it not a case of actual crashing or corruption, and just a non-intuitive ordering of operations than someone might expect? Well then we aren't really talking about the same thing at all, and I don't really care as much what the default should be, as a matter of industry best practices.
- veddan 10y agoUnless your program is designed to work that way, the fine-grained synchronization that StringBuffer offers over StringBuilder is almost certainly not very useful. And since Java is a memory-safe language, the only meaningful difference between StringBuffer and StringBuilder in "accidentally" multi-threaded code is the exact manner in which your output will garbled.
- kbenson 10y agoUp-thread, lmm, said "You wouldn't necessarily know, or notice - your users would just experience occasional random crashes." Are you saying that's incorrect? If so, that's less an issue of safety, as much as it is data integrity, which is to my mind a less important constraint to keep. In that case, an argument about defaults with respect to safety is a non-sequitur, so I don't really have a point to argue with respect to this example (if true).
- mcguire 10y agoI think your definitions are wrong there. A crash is the best thing that can happen in the case of most, if not all errors. A program that generates bad data, or even worse, converts good data to bad data is much, much worse.
- jdmichal 10y agoThread-safety is implicitly different than other types of safety: There are multiple answers to the same problem, and the default one present in StringBuffer is not necessarily the one you want. For instance, StringBuffer's thread-safety only guarantees that every write will be executed without corruption. What are the possible outcomes of this executing in two threads: stringBuffer.append("a").append("b"); // Thread 1 stringBuffer.append("c").append("d"); // Thread 2 The answer is, any of the 6 permutations of "abcd" such that "a" appears before "b" and "c" appears before "d". However, maybe what you really wanted was just the two permutations of "abcd" or "cdab". Just saying, "thread-safe!" doesn't answer the question of how it's thread-safe and what its behavior will be if multiple threads are contending for it. All it means is that the program won't crash or corrupt just because multiple threads are in the same place at once, which is not really all that useful a guarantee when you're looking at very expensive locking mechanisms to get there.
- kbenson 10y agoI don't consider that a matter of safety, as much as data integrity. Safety, as I've defined it elsewhere within this thread, being "does not cause memory corruption or crash". If neither technique cause that, then I don't really have an opinion. If one can cause a crash in certain circumstances, and the other cannot, I don't think it's responsible to endorse the one that can cause a crash. Does that mean you may need to figure out why your data looks weird at some later date? Possibly. But to me that much preferred to entire program failure in hard to reason about circumstances, or in memory corruption, which can lead to the same.
- jdmichal 10y agoActually, I'd rather my program just crash at the point of use than silently corrupt my data. First, my existent data will be preserved, and after fixing the issue I can rerun the operation. Second, I will get a clear crash that immediately tells me where the problem is, instead of getting garbage output that I have no idea where it sourced from. Oh, and since this is a threading problem, it's probably even transient data corruption.
- mcguire 10y agoStringBuffer doesn't actually give you any useful safety guarantees. If your program has threading bugs, a mangled string builder is probably the least of the problems. Thread safety isn't something you build up from tread safe components. It has to be designed in from the top. Further, almost all string builder uses are trivially thread safe.