{"action":"created","issue":{"url":"https://api.github.com/repos/cozy/cozy-guidelines/issues/9","labels_url":"https://api.github.com/repos/cozy/cozy-guidelines/issues/9/labels{/name}","comments_url":"https://api.github.com/repos/cozy/cozy-guidelines/issues/9/comments","events_url":"https://api.github.com/repos/cozy/cozy-guidelines/issues/9/events","html_url":"https://github.com/cozy/cozy-guidelines/issues/9","id":55894007,"number":9,"title":"fast return","user":{"login":"aenario","id":1580287,"avatar_url":"https://avatars.githubusercontent.com/u/1580287?v=3","gravatar_id":"","url":"https://api.github.com/users/aenario","html_url":"https://github.com/aenario","followers_url":"https://api.github.com/users/aenario/followers","following_url":"https://api.github.com/users/aenario/following{/other_user}","gists_url":"https://api.github.com/users/aenario/gists{/gist_id}","starred_url":"https://api.github.com/users/aenario/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aenario/subscriptions","organizations_url":"https://api.github.com/users/aenario/orgs","repos_url":"https://api.github.com/users/aenario/repos","events_url":"https://api.github.com/users/aenario/events{/privacy}","received_events_url":"https://api.github.com/users/aenario/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-01-29T12:40:13Z","updated_at":"2015-01-29T16:21:28Z","closed_at":null,"body":"# Problem\r\n\r\n```coffee\r\nmyFunction = (callback) ->\r\n    doStuff1 (err ) ->\r\n        return callback err if err\r\n        doStuff2 (err, result) ->\r\n            return callback err if err\r\n            callback null, result\r\n```\r\nvs\r\n```coffee\r\nmyFunction = (callback) ->\r\n    doStuff1 (err ) ->\r\n        if err\r\n            callback err\r\n        else\r\n            doStuff2 (err, result) ->\r\n                if err\r\n                    callback err\r\n                else\r\n                    callback null, result\r\n```\r\n\r\n\r\nI prefer and always use the return fast approach. @frankrousseau prefer the if/else approach and we have both ~50% in our codebase\r\n\r\n\r\n# Returns\r\n- GOOD : smaller / cleanear amha\r\n- GOOD : more readble amha : this line means if there is an error, pass it above and dont think about it in the rest of the function\r\n- BAD : easy to forget (coffee-jshint to the rescue)\r\n\r\n# if/else\r\n- GOOD : more explicit, expose complexity (force smaller functions)\r\n- GOOD : easier to expand to handle more error case [1]\r\n- BAD : amha, need some braintick to see if we handle err, transform it, ignore it\r\n\r\n\r\n```coffee\r\n# [1]\r\ndoStuff (err, body) ->\r\n    if err and err.code is 'YOLO'\r\n        callback new Error 'yolo'\r\n    else if body.error is 'not_found'\r\n        callback NotFoundError 'that stuff'\r\n    else if err\r\n        callback err\r\n   else\r\n         callback null, body.stuff\r\n```\r\n\r\n \r\n"},"comment":{"url":"https://api.github.com/repos/cozy/cozy-guidelines/issues/comments/72053843","html_url":"https://github.com/cozy/cozy-guidelines/issues/9#issuecomment-72053843","issue_url":"https://api.github.com/repos/cozy/cozy-guidelines/issues/9","id":72053843,"user":{"login":"aenario","id":1580287,"avatar_url":"https://avatars.githubusercontent.com/u/1580287?v=3","gravatar_id":"","url":"https://api.github.com/users/aenario","html_url":"https://github.com/aenario","followers_url":"https://api.github.com/users/aenario/followers","following_url":"https://api.github.com/users/aenario/following{/other_user}","gists_url":"https://api.github.com/users/aenario/gists{/gist_id}","starred_url":"https://api.github.com/users/aenario/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aenario/subscriptions","organizations_url":"https://api.github.com/users/aenario/orgs","repos_url":"https://api.github.com/users/aenario/repos","events_url":"https://api.github.com/users/aenario/events{/privacy}","received_events_url":"https://api.github.com/users/aenario/received_events","type":"User","site_admin":false},"created_at":"2015-01-29T16:21:27Z","updated_at":"2015-01-29T16:21:27Z","body":"Optimization : these two solutions are equivalent at least in node/v8 (https://gist.github.com/aenario/551724be0de2c9600645)\r\n\r\nThis is purely a style choice. \r\n\r\n------------\r\n\r\n\"First case often leads to put returns in the middle of the method which makes it hard to debug.\"\r\n\r\nSorry, I dont see what you mean.\r\n\r\n-----------\r\n\r\n\"First case looks to be the trend (newcomers to the problematic only sees direct advantages and don't see side effects).\"\r\n\r\nMost established projects use them too.\r\n\r\nhttps://github.com/npm/npm/blob/master/lib/install.js#L37\r\nhttps://github.com/joyent/node/blob/885f72105bf0b18007100550f131c85ca56536e0/lib/_debugger.js#L281\r\nhttps://github.com/strongloop/express/blob/9434ad39e114ea810859cd9c6fef624d4acfbe18/lib/response.js#L398"}}