withBreak blocks

Isiah Meadows isiahmeadows at gmail.com
Sun Feb 18 10:02:10 UTC 2018


As previously mentioned, this is effectively already covered by
labeled blocks. They aren't slated for removal (some things are just
nearly impossible to express without them), just they have a tendency
to get abused like `goto` in C - people have this tendency in C/C++ to
get a little over-eager to reach for that shotgun when a
`while`/`for`/`if`/etc. would do. So a strong discouragement is
generally warranted, but mainly to get less-skilled coders to not code
themselves into an unmaintainable hole before they can properly wield
the tool and understand why it exists. (It makes for a nice way to
break from non-trivial logic when a separate function would only serve
to complicate the code.)
-----

Isiah Meadows
me at isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Sat, Feb 17, 2018 at 4:02 PM, sagiv ben giat <sagiv.bengiat at gmail.com> wrote:
> I hope I'm on the right medium for this, I would like to propose a language
> feature.
> withBreak blocks
>
> Well the name might not be the best, but it's just my way to be the most
> clear about this feature proposal.
>
> I often find my self doing this in my code:
>
> const doWork = () => {
>   // try catch omitted for brevity
>   const response = fetchData();
>   do {
>     if (response.error) {
>       log(response.message);
>       break;
>     }
>     if (!response.data) {
>       log("No data");
>       break;
>     }
>     if (!response.data.todos) {
>       log("No Todos");
>       break;
>     }
>     return action({ data: response.data });
>   } while (false);
> };
>
> I'm doing this instead of doing bunch of  if / else if / else blocks or ugly
> nested if blocks.
>  What i would like to have is a block that will let me break without being
> in a loop context.
> Something like this:
> withBreak {
>   if (response.error) {
>     log(response.message);
>     break;
>   }
>   if (!response.data) {
>     log("No data");
>     break;
>   }
>   if (!response.data.todos) {
>     log("No Todos");
>     break;
>   }
>   return action({ data: response.data });
> }
>
> This can be a synthetic sugar for do{}while(false)  behind the scenes.
>
> Best regards,
>
> Sagiv B.G
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list