错误检查的不同语法 [英] A different syntax for error checking

查看:64
本文介绍了错误检查的不同语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在comp.lang.c ++上,我们讨论了以下的替换:


if(expr)Func();


with:


expr&& Func();


我自己最初的反应是,如果我在代码中看到后者,我会说

程序员在玩游戏用时髦的功能打动人心。我b / b
表示if在语言中有这个确切的目的,它应该是

用于此目的。


然后其中一个贡献者回复说他们使用if ;对于

正常的代码运行,然后使用&&和||用于错误检查代码。

有点像:


FILE * pf;


if(输入7 )

{

pf = fopen(" monkey"," r");


pf ||退出(EXIT_FAILURE);

}


说实话,我非常喜欢这个想法。明确的错误检查代码

从正常代码中脱颖而出。代码。


-
$ b $bTomásóhéilidhe

解决方案

" ????= ISO-8859-1 q汤姆= E1s_ = D3_h = C9ilidhe = QUOT?; < to *@lavabit.comwrote:


在comp.lang.c ++上,我们讨论了替换:


if(expr)Func();


with:


expr&& Func();


我自己最初的反应是,如果我在代码中看到后者,我会说

程序员在玩游戏用时髦的功能打动人心。



我也是。如果他想要Perl,他知道在哪里找到它。


if(b)输入7)

{

pf = fopen(" monkey"," r");


pf ||退出(EXIT_FAILURE);

}



* Brrrrrr *


Richard


我不喜欢它。

但每个都是他们自己的。


他当然不会得分如果我必须维护他的

代码,请与我联系。


Tomásóhéilidhe写道:


在comp.lang.c ++上,我们讨论了替换:


if(expr)Func();


with:


expr&& Func();


我自己最初的反应是,如果我在代码中看到后者,我会说

程序员在玩游戏用时髦的功能打动人心。我b / b
表示if在语言中有这个确切的目的,它应该是

用于此目的。


然后其中一个贡献者回复说他们使用if ;对于

正常的代码运行,然后使用&&和||用于错误检查代码。

有点像:


FILE * pf;


if(输入7 )

{

pf = fopen(" monkey"," r");


pf ||退出(EXIT_FAILURE);

}


说实话,我非常喜欢这个想法。明确的错误检查代码

从正常代码中脱颖而出。码。



这在C中是一个糟糕的主意,原因有两个:


1)这不常见的做法

2)它没有逻辑上的阅读。


在Perl中(这个构造*是常见的练习和惯用语),

函数通常会返回一个真正的成功值,所以行:


dosomething(...)或die无法打开文件;


使用或死字样就像英语使用它们一样。在C中,

函数通常会返回0表示成功,因此C中的表单将是:


dosomething(...)&& myerrorfunc(无法打开文件\ n);


其中的单词and和与英语完全相反的方式使用。

fopen()返回成功的有效指针和失败的NULL,所以

这个成语恰好适用于fopen() ,但fclose()返回0(false)

表示成功,EOF(true)表示失败,因此这项技术永远不会一直使用



Over on comp.lang.c++, we were dicussing the replacment of:

if (expr) Func();

with:

expr && Func();

My own initial reaction was that, if I saw the latter in code, I''d say
the programmer is playing games trying to impress with funky features. I
stated that "if" has this exact purpose in the language, and it should be
used for this.

But then one of the contributors replied saying that they use "if" for
the normal run of code, but then use && and || for error-checking code.
Sort of like:

FILE *pf;

if (input 7)
{
pf = fopen("monkey","r");

pf || exit(EXIT_FAILURE);
}

To be honest, I quite like the idea. The error-checking code clearly
stands out from the "normal" code.

--
Tomás ó héilidhe

解决方案

"=?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=" <to*@lavabit.comwrote:

Over on comp.lang.c++, we were dicussing the replacment of:

if (expr) Func();

with:

expr && Func();

My own initial reaction was that, if I saw the latter in code, I''d say
the programmer is playing games trying to impress with funky features.

So would I. If he wants Perl, he knows where to find it.

if (input 7)
{
pf = fopen("monkey","r");

pf || exit(EXIT_FAILURE);
}

*Brrrrrr*

Richard


I don''t like it.
But each to their own.

He certainly does not score a point with me if I have to maintain his
code.


Tomás ó héilidhe wrote:

Over on comp.lang.c++, we were dicussing the replacment of:

if (expr) Func();

with:

expr && Func();

My own initial reaction was that, if I saw the latter in code, I''d say
the programmer is playing games trying to impress with funky features. I
stated that "if" has this exact purpose in the language, and it should be
used for this.

But then one of the contributors replied saying that they use "if" for
the normal run of code, but then use && and || for error-checking code.
Sort of like:

FILE *pf;

if (input 7)
{
pf = fopen("monkey","r");

pf || exit(EXIT_FAILURE);
}

To be honest, I quite like the idea. The error-checking code clearly
stands out from the "normal" code.

This is a terrible idea in C, for two reasons:

1) It''s not common practise
2) It doesn''t read logically.

In Perl (where this construct *is* common practise and idiomatic),
functions generally return a true value for success, so the line:

dosomething(...) or die "Couldn''t open file";

uses the words "or die" in the same way that English uses them. In C,
functions generally return 0 for success, so the form in C would be:

dosomething(...) && myerrorfunc("Couldn''t open file\n");

where the word "and" is used in precisely the opposite way from English.
fopen() returns a valid pointer for success and NULL for failure, so
this idiom happens to work for fopen(), but fclose() returns 0 (false)
for success and EOF (true) for failure, so this technique could never be
used consistently.


这篇关于错误检查的不同语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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