{"action":"created","issue":{"url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/issues/1798","labels_url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/issues/1798/labels{/name}","comments_url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/issues/1798/comments","events_url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/issues/1798/events","html_url":"https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1798","id":118152393,"number":1798,"title":"Possible rule proposal: Disallow multiple returns from method","user":{"login":"GregReddick","id":3107352,"avatar_url":"https://avatars.githubusercontent.com/u/3107352?v=3","gravatar_id":"","url":"https://api.github.com/users/GregReddick","html_url":"https://github.com/GregReddick","followers_url":"https://api.github.com/users/GregReddick/followers","following_url":"https://api.github.com/users/GregReddick/following{/other_user}","gists_url":"https://api.github.com/users/GregReddick/gists{/gist_id}","starred_url":"https://api.github.com/users/GregReddick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GregReddick/subscriptions","organizations_url":"https://api.github.com/users/GregReddick/orgs","repos_url":"https://api.github.com/users/GregReddick/repos","events_url":"https://api.github.com/users/GregReddick/events{/privacy}","received_events_url":"https://api.github.com/users/GregReddick/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/labels/needs%20discussion","name":"needs discussion","color":"f7c6c7"},{"url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/labels/new%20rule","name":"new rule","color":"207de5"},{"url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/labels/proposal","name":"proposal","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-11-20T23:56:11Z","updated_at":"2015-11-28T06:00:25Z","closed_at":null,"body":"What do people think about multiple returns inside a method? I've been back and forth on whether I think they should be allowed. I prefer having only one return inside a method in most cases (it's been suggested it makes provably better code), thus this is wrong:\r\n```\r\npublic bool Foo(int x)\r\n{\r\n    bool result = false;\r\n    if (x == 2)\r\n    {\r\n        return result;\r\n    {\r\n\r\n    // other code\r\n    result = true;\r\n    return result;\r\n}\r\n```\r\nIt could be rewritten to have only one exit point.\r\n```\r\npublic bool Foo(int x)\r\n{\r\n    bool result = false;\r\n    if (x != 2)\r\n    {\r\n        // other code\r\n        result = true;\r\n    }\r\n\r\n    return result;\r\n}\r\n```\r\nOn the other hand, there are times that disallowing multiple returns creates awkward code, especially when there are checks of parameters at the beginning of the code that immediately returns when they are invalid. This code has an implicit return at the end, and thus three exit points.\r\n```\r\npublic void Foo(int x, int y)\r\n{\r\n    if (x > 2)\r\n    {\r\n        return;\r\n    }\r\n\r\n    if (y > 14)\r\n    {\r\n        return;\r\n    }\r\n\r\n    // other code\r\n}\r\n```\r\nCoding this with only one return can be awkward, forcing deep nesting, particularly if the expressions being tested are more complex. (Sure, you can do an && on the two expressions and reduce the indents some, but that's beside the point.)\r\n```\r\npublic void Foo(int x, int y)\r\n{\r\n    if (x <= 2)\r\n    {\r\n        if (y <= 14)\r\n        {\r\n           // other code\r\n       }\r\n    }\r\n}\r\n```\r\nI don't think you can easily do a code fix on this as it requires some deep thought about the code.\r\n\r\nI am weakly proposing this as a rule, but mostly wanting to gather people's opinion about multiple exit points in code."},"comment":{"url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/issues/comments/160251412","html_url":"https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1798#issuecomment-160251412","issue_url":"https://api.github.com/repos/DotNetAnalyzers/StyleCopAnalyzers/issues/1798","id":160251412,"user":{"login":"nbarbettini","id":7525482,"avatar_url":"https://avatars.githubusercontent.com/u/7525482?v=3","gravatar_id":"","url":"https://api.github.com/users/nbarbettini","html_url":"https://github.com/nbarbettini","followers_url":"https://api.github.com/users/nbarbettini/followers","following_url":"https://api.github.com/users/nbarbettini/following{/other_user}","gists_url":"https://api.github.com/users/nbarbettini/gists{/gist_id}","starred_url":"https://api.github.com/users/nbarbettini/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nbarbettini/subscriptions","organizations_url":"https://api.github.com/users/nbarbettini/orgs","repos_url":"https://api.github.com/users/nbarbettini/repos","events_url":"https://api.github.com/users/nbarbettini/events{/privacy}","received_events_url":"https://api.github.com/users/nbarbettini/received_events","type":"User","site_admin":false},"created_at":"2015-11-28T06:00:25Z","updated_at":"2015-11-28T06:00:25Z","body":"I'm a big fan of the \"fail fast and early\" school of thought, so I'm :thumbsdown: on this rule. I have a lot of this in my code:\r\n\r\n```\r\npublic int Foo(int bar)\r\n{\r\n    if (bar < 0)\r\n        return 0;\r\n\r\n    // do other things\r\n}\r\n```"}}