robust programmings

本文探讨了如何编写健壮的程序,包括从预期错误到不可恢复错误的处理方式,确保程序的稳定性和可靠性。强调了检查返回值、参数验证及合理错误处理的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近看了一些很烂的代码, 自己也在写这样的代码,对编程有了一些反思。

什么是robust program, 看看大牛的说法

 

can recover from expected error conditions

(intermittent failures, missing configuration files.)

 

can recover from unexpected error condition

(e.g file system full, our of disk quota, name lookup failures,

not enough resuources to draw a window ).

 

when there is an unrecoverable error - notifies the user of

the situation and cleanly shuts down.

 

avoids dependency on robustness of related processes

(a client should not crush because a server sent a

garbled reply and vice versa).

 

always check return values of system calls an library

functions - make this test exhaustive.

 

in any global function that accepts parameters-

check for their sanity(

pointers to data must not be NULL, data should no be out of range).

 

if a function might fail - have it return some error code

to its caller, don't just ignore the pssibility of failure.

 

when an inner function fails - it should return an error string to its caller - not just try

to display it to the user, exception:

the function can overcome the error and hider it from the calling function.

the function then may ignore the error, or issue a warning to the user,

depending on context. )

 

转载于:https://www.cnblogs.com/kwingmei/p/3608717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值