3 ms·
Odd. I typically prefer the foreach(x in list) if(unusual_condition(x)) continue x2=perform_data_transformation(x); x3=perform_data_trans
by archgoon 15y ago
Odd. I typically prefer the
foreach(x in list)
if(unusual_condition(x))
continue
x2=perform_data_transformation(x);
x3=perform_data_transformation(x2);
to
foreach(x in list)
if(!unusual_condition(x))
x2=perform_data_transformation(x);
x3=perform_data_transformation(x2);
Because
1) reduced nesting in the first,
2) makes it more clear that unusual condition should simply be skipped.
Is this a personal style thing, or should is it generally preferred to do the second option?
- DanielRibeiro 15y agoI usually prefer: foreach(x in list.reject(unusual_condition(_))) x2=perform_data_transformation(x); x3=perform_data_transformation(x2); I find collections and their functional methods are great for clear expression of intent[1] [1] http://metaphysicaldeveloper.wordpress.com/2009/05/02/closures-collections-and-some-functional-programming/ http://metaphysicaldeveloper.wordpress.com/2009/05/02/closur...
- michaelcampbell 15y agoI do the same; it more clearly shows intent IMO. I think your second example is more ubiquitous because of the teaching that "jumps" are bad; break, continue, goto... all of them bad. Don't do that. Etc. Where I use this pattern a lot is argument validation on the tops of method bodies. And sometimes, "avoid the impossible conditions early" rule if (something_bad) return do_good_stuff() ...