No labels?

Posted on
  • Espruino apparently does not implement labels because “they're generally considered bad practice”. Really? Goto is (still) broadly considered harmful, but JS does not provide goto; its labels are used for multilevel breaks (and continues), a feature that repairs the relative uselessness and occasional opacity of C's implicitly targeted break statements. It seems a little weird to implement break (which has also been argued against by control flow purists, though I think received opinion by now is that they were arguing more out of a desire to simplify their control flow analysis algorithms than any true readability problem) and not label.

    Anyway, I don't really mean to rant about language design philosophy, especially over a feature that I wouldn't notice was missing if it hadn't already been in the spec, but what's the recommended alternative on this platform?

  • I'm not sure what "platform" refers to. Maybe I'm misunderstanding, but are you asking about alternative firmware for the nrf52832 chips? There's arduino of course, and then there's adafruit's circuitpython. You can also work with the nordic sdk directly.

  • Labels aren't implemented because the way Espruino interprets code, it would be very difficult and inefficient to make them work - plus honestly I can't remember the last time I saw a label and goto in real code. The last time someone asked me about this was maybe 8 years ago?

    So specifically you're interested in break to a label?

    Honestly, it's hard to suggest 'an alternative' without any example but there's almost certainly a way of re-writing code in such a way where you don't have to use them.

    It's worth noting that if you were planning on using breaks as a performance enhancement, it may not work the way you expect on Espruino because it's interpreting (and executing as it parses).

  • This may be a flaw in the Espruino documentation, then—it says there that the reason labels are not implemented is that they are bad practice, not because of the implementation strategy.

    My particular use case is of this form:
    search: for (…) for (…) if(…) break search; // found it!

    One could of course throw an exception, use “continue” variables, or any of a number of other solutions; they're all to varying degrees confusing or inefficient, and I just wondered what the received idiom is.

  • ...oh boy... or girl... or ??? I don't know what to say here. I guess - since I do not have to say something, I just let it be, or go, or be gone.

    Still, it gets me thinking, and - @user135362 - I'm thankful for getting me going. (Same 'last' word, but no contradiction though, see Parser errors in arrow functions (2v10.90 Bangle emulator).)

  • search: for (…) for (…) if(…) break search; // found it!

    One could of course throw an exception, use “continue” variables, or any of a number of other solutions; they're all to varying degrees confusing or inefficient

    what about making it a function named 'search' and return found item from it? if it does something and has a name isn't function that idiom?

  • Yep, that's a definite option, thanks—does Espruino optimise parameterless functions called in place into low-overhead control flow? Is treating return as a value-carrying nonlocal exit the expected idiom?

    ((()=>{ … return;})()); is unquestionably uglier than foo: … break foo; though! :) )

  • To be fair, it's difficult to implement efficiently and I think labels are generally considered to be bad practice now (which is what we say in the docs). You're the first person to request the functionality in a very long time, so I think it probably was the right decision.

    Having said that, it's all open so if you felt like adding labels to the parser yourself and could do that without impacting the performance in other areas, I'd be very happy to accept a PR!

    Since Espruino executes in-place right now there's no optimisation pass. However if your reason for using break was for speed then I'm afraid you're likely to be disappointed. Espruino's execution speed really isn't all that spectacular.

    It is however possible to include C functions inside your code, so if you want to use labels or any other C-like functionality and be super fast that's always an option https://www.espruino.com/InlineC

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

No labels?

Posted by Avatar for user135362 @user135362

Actions