Better way to code returning the result of a true/false call

Many times, I'm writing a function which will return `true` on success and `false` on error/failure, and essentially wrapping another function which does the same. An example (basically language-independent, but I'll write in ECMAScript for familiarity): function SendEmail(to, from, subject, body) { var eml = new EmailObj(); eml.To = to; eml.From = from; eml.Subject = subject; eml.Body = body; return eml.Send(); // This will return true/false } But, sometimes, I want to do something else depending on the result of the interior function (say log specific information): function SendEmail(to, from, subject, body) { var eml = new EmailObj(); eml.To = to; eml.From = from; eml.Subject = subject; eml.Body = body; if(!eml.Send()) { Log("Error in send: " + eml.Response); return false; } return true; } But, I never feel like I've written good code when I do something like that. Would you write this differently? Am I being paranoid? Or do you think this pattern in and of itself leads to this dilemma and I should look to see if I can change the higher-level approach(es)?

以上就是Better way to code returning the result of a true/false call的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏