Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --> operator for non-returning functions #3209

Closed
wants to merge 2 commits into from
Closed

Conversation

L8D
Copy link

@L8D L8D commented Oct 20, 2013

If you don't want this feature, I totally understand and you can close this PR. I'm just doing this since I've already written the code for personal use.

@bjmiller
Copy link

What's the use case for this? I'm not sure why I would need to make a function "non-returning". In JS, every function returns something - undefined if you don't use a return statement. If you really want that behavior, just make the last line of a function void 0.

@L8D
Copy link
Author

L8D commented Oct 20, 2013

In a lot of my JavaScript projects most of my functions aren't supposed to return anything. A lot of the time CoffeeScript will end up writing extraneous code in order for the last line of a function to return something. By having this operator I can make it a lot easier to distinguish between functions that aren't needing to return something and functions that do. This is very common when it comes to callbacks in Node.js or when working with libraries like Angular.js or Ember.js.

For example, let's say I have a function that calls another function for a specified number of times:

doTimes = (x, f) ->
  for _ in [0..x]
    f()

Well, if you're not careful, CoffeeScript will end up generating this:

var doTimes;

doTimes = function(x, f) {
  var _, _i, _results;
  _results = [];
  for (_ = _i = 0; 0 <= x ? _i <= x : _i >= x; _ = 0 <= x ? ++_i : --_i) {
    _results.push(f());
  }
  return _results;
};

Which you really don't need. The only “proper” way to do this is by explicitly returning at the end of a function like so:

doTimes = (x, f) ->
  for _ in [0..x]
    f()
  return

That can start to make your code look really hairy after nesting callbacks or having non-returning methods on classes.

I made this adjustment to the CoffeeScript library used on a few of my projects to keep my code cleaner and to prevent unexpected behavior when forgetting to add a return at the end of every function.

So instead of writing an additional line, I could just add an additional hyphen.

doTimes = (x, f) -->
  for _ in [0..x]
    f()

And BTW, using an empty return is better than using void 0 or undefined, as CoffeeScript won't realize that's what the function will return anyways, and will actually add return void 0; at the end of the function calls.

Which, I don't really see why CoffeeScript generates this:

var doTimes;

doTimes = function(x, f) {
  var _, _i;
  for (_ = _i = 0; 0 <= x ? _i <= x : _i >= x; _ = 0 <= x ? ++_i : --_i) {
    f();
  }
};

–when all it really needs to do is:

var doTimes = function(x, f) {
  for(var i = ; i < x; i++) {
    f()
  }
};

@L8D
Copy link
Author

L8D commented Oct 20, 2013

Step -->
  fs.readFile __filename, this

, (err, text) ->
  throw err if err
  do text.toUpperCase

, (err, newText) -->
  throw err if err
  console.log newText

As opposed to:

Step ->
  fs.readFile __filename, this
  return
, (err, text) ->
  throw err if err
  do text.toUpperCase
, (err, newText) ->
  throw err if err
  console.log newText
  return

@vendethiel
Copy link
Collaborator

Thanks for your pull request, but I'm afraid this has been turned down many times already. I guess #899 is the oldest, but the opinion around hasn't changed since I think (issues like this get created regularly and get closed for the same reasons). Closing for now.

@vendethiel vendethiel closed this Oct 20, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants