4 ms·
If you're using throw for control flow, something is deeply wrong with your code.
by smt88 5y ago
If you're using throw for control flow, something is deeply wrong with your code.
- the_other 5y ago`throw` is a control flow instruction. Simply using it at all controls flow within your code. Are you suggesting never to use it?
- golergka 5y agoI think that the parent comment means "for control flow unrelated to error conditions".
- smt88 5y agoI am suggesting to use it to throw exceptions. This narrow purpose is subtly hinted at by its name.
- the_other 5y agoI don’t see how it’s hinted in the name. “Throwing an exception” is a phrase from within computing that exists because the feature exists in some languages, not because it came into programming from English. There’s also nothing about “exception” that means “error”. If anything, modelling exceptions as errors is the anti-pattern. If you modelled all exceptions as descriptors, you could save Errors for when something actually goes wrong. Honestly though, I’m employing Socratic argument. I am only defending this position to learn about the opposite.
- regularfry 5y agoNow I'm wondering about throwing functions as continuations. This sounds incredibly messy. I love it.
- emteycz 5y agoWhy not generators then?
- regularfry 5y agoBecause that wouldn't be perversely abusing a language feature for completely the wrong purpose.
- shadowgovt 5y agoI... May have done something like this once in a scripting language interpreter to implement return and break. Not in JavaScript. And using continuations passed down from above would have been the much more correct way to do things.
- onion2k 5y agoThere isn't though. Exceptions are flow control. There's nothing particularly special about them internally in a JS engine. https://ripsawridge.github.io/articles/exceptions/ https://ripsawridge.github.io/articles/exceptions/
- smt88 5y agoI know. But what happens when someone inherits code that has a lot of this in it? They read a line, get to a throw, and then... how do they find all the catches that might apply? Using that pattern is like a guarantee that your code will be spaghetti and meatballs. This concept is so fundamental that it's even the topic of possibly the most famous CS article ever written[1], and I'd even argue that throw/catch is the worst-possible version of goto. 1. https://en.m.wikipedia.org/wiki/Considered_harmful https://en.m.wikipedia.org/wiki/Considered_harmful
- onion2k 5y agoWhen Dijkstra wrote his article [1], in 1968, I have no doubt he was correct. His point was essentially that code should reflect the flow of the application as closely as possible because it's immensely hard to keep everything in your head if it doesn't in the context of programming in 1968. The thing is though, tools have moved on in the past 54 years. Seeing the flow of your code is a lot easier now. Debuggers are amazing things. While I think his broad point (that code should be as simple and easy to follow as possible) still stands I don't really agree with it here. "goto considered harmful" could be used to argue that anything that changes and obfuscates the flow of a program when you compare the source to the compiled output is bad regardless of what it is, and that's plain silly. We'd have to throw away all manner of useful things like exceptions, macros, inheritance, etc. I like Dijkstra's writing a lot, and he did groundbreaking work, but we have to remember to consider everything in the context of when it was written. This is a good example of that. [1] http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html http://www.u.arizona.edu/~rubinson/copyright_violations/Go_T...
- smt88 5y ago