Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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?



I 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/closur...


I 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()
    ...




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: