`
sean_gao
  • 浏览: 227726 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

作为开发者,我们必须要学会defensive programming,尤其是对要求高可靠性和无人职守的企业级应用中,需要特别留意我们的设计和编码,必须尽可能做到足够defensive。

什么是defensive programming?举个大家都看过的例子:

String str = ...
if ("".equals(str)) {}

在这里我们不写str.equals("")而是反过来,就是为了防止出现不必要的NPE – NullPointerException。

运行期异常是最最需要特别关照的一种非正常状况,除了像上面这类要求我们采用相对较好的编码习惯之外,为了减少运行期异常的发生,通常也需要使用try-catch代码块来把我们相对脆弱,或者需要格外保护的逻辑包起来,对于外部传进来的参数,一定要assert它们的合法性,即assert它们是否能够安全的被后面的逻辑所使用。

通常意义上,defensive programming主要cover的是避免不必要的运行期异常发生。我们可以更进一步,更广义的运用defensive programming的核心思想:在企业应用中,除了运行期异常,对于有些看似严重的极端的错误,如网络超时,连接丢失,数据库提交失败等情况,需要我们具体问题具体分析,并非所有checked exception都一定需要我们去一一catch然后处理。更多的时候,尤其当开发无人职守的后台程序,我们可以采取重试、报告、修改外部数据等方式处理,能够自行解决的,就不要动不动就报错,或等待用户确认,不能自行解决的,则要及时报告并停止运行,避免更大的错误发生。

举个相对具体的例子,两个异构的系统,通过一个中间层的消息平台相互发送消息,通信协议采用最基本的socket方式,这三个系统随时都可能出现宕机或链接中断的情况。为了保证数据的完整性,我们拿其中一个需要发送和接收消息的系统来细说:

一个可能的实现方式是:该系统所有要发送的消息保存到数据库,给它一个初始状态;另一个独立进程从数据库按照时间先后拿出消息,更新拿出的这条消息的状态为处理中,并尝试发送消息;成功后根据需要,更新消息状态为成功发送或者直接删除,如果遇到失败或异常,消息恢复为初始状态,线程sleep一段时间,然后再次尝试,多次尝试或者尝试跨度超过一定时间范围,则停止处理,向管理员汇报(通过邮件、短信等途径)。对于接收到的消息,同样是先存入数据库,然后再由后续的进程用类似的方式取出并处理。如果程序崩溃,可以自动重新启动(应用或整个服务器)。这样不管哪一段通信线路出现故障或阻塞,或者宕机,系统都可以一步一个脚印,确保任务主动而自动的执行,并且忠实记录下有价值的状态信息,出现问题时管理员可以很直观的看到在哪个环节出现故障,从而快速找到问题关键并有效解决。

Defensive programming可以让我们的应用更健壮,在保证数据正确性、完整性的前提下,面对困难也能更加独立自主。和defensive programming相关的话题我想大家如果感兴趣,可以展开更多更深入的探讨,这里只是给大家做个介绍性的铺垫,能抛砖引玉当然更好。




大胃 2007-01-25 00:03 发表评论
分享到:
评论

相关推荐

    Defensive Security Handbook: Best Practices for Securing Infrastructure

    Defensive Security Handbook: Best Practices for Securing Infrastructure by Lee Brotherston English | 3 Apr. 2017 | ASIN: B06Y18XC5Y | 268 Pages | AZW3 | 3.88 MB Despite the increase of high-profile ...

    Defensive Security Handbook(O'Reilly,2017) (三种格式pdf,azw3,epub)

    profile hacks, record-breaking data leaks, and ransomware attacks, many organizations don’t have the budget to establish or outsource an information security (InfoSec) program, forcing them to learn ...

    Defensive Security Handbook(O'Reilly,2017)

    profile hacks, record-breaking data leaks, and ransomware attacks, many organizations don’t have the budget to establish or outsource an information security (InfoSec) program, forcing them to learn ...

    Cyber Operations(Apress,2015)

    Learn to set up, defend, and attack computer networks. This book focuses on networks and real attacks, offers extensive coverage of offensive and defensive techniques, and is supported by a rich ...

    难得的经典——《Defensive Programming for RedHat Enterprise Linux》

    ### 难得的经典——《Defensive Programming for Red Hat Enterprise Linux》 #### 摘要与背景 在软件开发领域,安全性和稳定性是至关重要的。随着互联网的发展,网络安全威胁日益严重,对软件的安全性提出了更高...

    防御式编程 Defensive Programming.PPT完整版(精品课件)

    防御式编程 Defensive Programming.PPT完整版(精品课件) 大纲: 保护程序免遭非法输入数据的破坏 断言 错误处理技术 异常 隔离程序 辅助调试代码

    Personal.Cybersecurity.epub

    This book teaches you the defensive best practices and state-of-the-art tools available to you to repel each kind of threat. Personal Cybersecurity addresses the needs of individual users at work ...

    Military Avionics Systems

    Avionics is a word coined in the late 1930s to provide a generic name for the ... It is the military avionics of an aircraft that allow it to perform defensive, offensive and surveillance missions.

    counter hack reloaded

    other curious folks who want to learn how attackers work and techniques for defending their own systems against attacks can benefit. The book includes practical recommendations for people who have to ...

    Enterprise.Cybersecurity.1430260823

    Given sufficient time, an intelligent attacker can eventually defeat defensive measures protecting an enterprise’s computer systems and IT networks. To prevail, an enterprise cybersecurity program ...

    Java Defensive Programming-开源

    Java防御性编程是一种编程策略,旨在提前预防程序中可能的错误和异常,从而提高软件的稳定性和可靠性。在Java中,防御性编程通常通过注释、异常处理、数据验证和健壮的错误处理来实现。开源社区提供了许多工具和库来...

    Security with Go

    Learn to write secure software, monitor your systems, secure your data, attack systems, and extract information. Defensive topics include cryptography, forensics, packet capturing, and building ...

    defensive database programming

    本书《Defensive Database Programming with SQL Server》由Alex Kuznetsov撰写,深入探讨了如何在SQL Server环境中实施防御性编程策略。 ### 一、基本防御性数据库编程技术 #### 1. 减少代码漏洞 - **定义假设**...

    nvxdsync.exe

    This LICENSE shall be deemed to have been made in, and shall be construed pursuant to, the laws of the State of Delaware. The United Nations Convention on Contracts for the International Sale of Goods...

Global site tag (gtag.js) - Google Analytics