检查大量的返回值时,优秀的C风格 [英] good c style when checking lots of return values

查看:78
本文介绍了检查大量的返回值时,优秀的C风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我不得不写code,它做的事情,并检查错误条件之间交替(例如,调用库函数,检查其返回值,继续下去)。这往往导致长期运行的实际工作中的if语句的条件发生,像

Sometimes I have to write code that alternates between doing things and checking for error conditions (e.g., call a library function, check its return value, keep going). This often leads to long runs where the actual work is happening in the conditions of if statements, like

if(! (data = (big_struct *) malloc(sizeof(*data)))){
    //report allocation error
} else if(init_big_struct(data)){
    //handle initialization error
} else ...

你们怎么写这种code的?我检查了几个风格指南,但他们似乎更关心的变量命名和空格。

How do you guys write this kind of code? I've checked a few style guides, but they seem more concerned with variable naming and whitespace.

链接风格指南欢迎。

编辑:如果现在还不清楚,我不满意这种风格的易读性,并寻找更好的东西。

in case it's not clear, I'm dissatisfied with the legibility of this style and looking for something better.

推荐答案

虽然它的痛苦,我说,这可能是永不受欢迎的转到的情况。这里有一个链接,我发现关于这个问题:<一href=\"http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/\">http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/

Though it pains me to say it, this might be a case for the never-popular goto. Here's one link I found on on the subject: http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/

这篇关于检查大量的返回值时,优秀的C风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆