Giter Club home page Giter Club logo

Comments (5)

jashkenas avatar jashkenas commented on July 23, 2024

I'm not a huge fan of .try(:symbol) in Rails, but Ruby's postfix rescue is a little better. Note that you can already do this in one line like so:

result: try attempt() catch e then null

Do you think that's too verbose? JavaScript doesn't have very good (or much use of) exceptions. I don't think it's too common to need to catch them and assign the result in a single line.

from coffeescript.

weepy avatar weepy commented on July 23, 2024

Personally it's too verbose for me to use it much. What do you mean by 'nor very good use of' exceptions?
You're probably right that's it's not very good coding practice. More often than not what you are really trying to do is prevent calling methods on null.

from coffeescript.

liamoc avatar liamoc commented on July 23, 2024

He means that functions that should throw exceptions tend not to.

Using exception handling for anything other than genuine exception handling sounds horrible to me, I vote leaving the syntax as is.

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

Yeah, I don't think I want to promote this feature with a language construct. Most places where it occurs in Ruby have hidden bugs where real exceptions (the ones that you aren't expecting) get swallowed by overzealous rescues. If you're just checking for null, then this:

result: try obj.method() catch e then null

Isn't the right way to do it in CoffeeScript either, this is better (and far, far faster):

result: if obj? then obj.method() else null

Closing the ticket.

from coffeescript.

weepy avatar weepy commented on July 23, 2024

Yep - I agree. In fact you can leave out the 'else':

result: if obj? then obj.method()

or even the 'then'

result: obj.method() if obj?

from coffeescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.