Hacker News new | past | comments | ask | show | jobs | submit login

Delimiter-first thinking leads to better code when you need to write a loop to produce some separated list.

   for (x in items; let comma = false) {
     if (want_to_print(x)) {
       if (comma)
         print ", "
       print x
       comma = true
    }
   }
A delimiter-separated list should be regarded as all but the first item being preceded by the delimiter, rather than all but the last item followed by a delimiter.

If you that the latter view, you write silly code which tries to guess whether more items are going to be printed in subsequent iterations, and thus whether to generate a trailing comma now.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: