5 ms·
I think parent's point is that s/he wants b,c,d on different lines if they were long variable names.
by lowboy 14y ago
I think parent's point is that s/he wants b,c,d on different lines if they were long variable names.
- doublerebel 14y agoThat's addressed in the second link. The code should be refactored. condition = (a) -> a in [ b, c, d ] switch when condition(a) e() This is a technique which is already possible in JavaScript, an example (not mine) from 2007: http://saintjohnshawn.com/2007/05/24/javascript-example-switch-statement-using-a-range-of-values/ http://saintjohnshawn.com/2007/05/24/javascript-example-swit...
- crazygringo 14y agoSorry, but that's completely bizarre. The whole point of a switch statement is to be able to test a single value against a list of single values, or groups of single values. The supposed "correct way" you support moves the values outside of the switch statement, which completely destroys reading comprehension of the code. It explodes the number of lines of code necessary. This isn't refactoring, it's complete code obfuscation. It also makes the switch statement itself completely superfluous, since you might as well just do a series of if / else if's, and save an extra line of code. I mean, the whole point of switch is to pass a parameter and compare it against values.
- doublerebel 14y agoSorry, what's completely bizarre is having variable names which push the 80 character limit. That doesn't help reading comprehension at all. For instance, look at the recently posted Matches.js [1] -- there are many reasons to factor out the test case when the test case is unusual. 80 character test cases are unusual. [1] http://news.ycombinator.com/item?id=4516576 http://news.ycombinator.com/item?id=4516576 EDIT: 80 character test cases (variables or otherwise) should be factored out. The readability and purpose of the method is compromised if you have 5 groups of 30 constants. Factor the groups out into individual maintainable functions. Then there is one function per case, and an overview of the entire switch can be seen in a few rows rather than several pages.
- crazygringo 14y agoWhat are you talking about? My particular use case that led to all this was just translating a simple, clean, elegant JavaScript switch like the following. It's ideal, really, in terms of readability: switch (operation_code) { case SUGGEST_ACTION: ... break; case PERFORM_ACTION_PREAUTHORIZE: case PERFORM_ACTION_AUTHORIZE: case PERFORM_ACTION_POSTAUTHORIZE: ... break; case DELETE_ACTION_REVERSIBLE: case DELETE_ACTION_IRREVERSIBLE: case DELETE_ACTION_NOT_PREAUTHORIZED: case DELETE_ACTION_PREAUTHORIZED: ... break; case UNDO_ACTION: ... break; (5 more groups of 30 more constants...) } It's not variable names that push an 80 character limit. It's about translating groups of shorter constants, that taken together go over 80 lines, and which have similar prefixes, so they look very nice listed vertically, and make it very clear where their differences and similarities are. I stand by my point: telling people that, in CoffeeScript, it's proper to "refactor" this into a switch statement with no parameter and creating entire new functions, is bizarre.
- jashkenas 14y agoHey, that's a really good use case for multi-line cases. Not exactly the fallthrough behavior of the JavaScript example you posted ... but a good reason to want linebreaks in your list of values to match. You should open a ticket suggesting a syntax improvement for it.
- antidoh 14y agoParent's point is that he wants better documentation, and possibly better consistency, on things like breaking lines; breaking lines was just an example to illustrate the larger point.