The second defensive programming

2010-02-07  来源:本站原创  分类:CPP  人气:158 

1, test program + code is faster than the direct encoding of a simple test case

#include <iostream>
using namespace std;

class MyDate
{
public:
    MyDate(int y,int m,int d)
    {
       year=y;
       month=m;
       date=d;
    }
    int getYear(){ return year;}
    int getMonth(){ return month;}
    int getDate(){ return date;}
private:
    int year,month,date;
};

int nPass=0,nFail=0;
void test(bool t)
{
    if(t) nPass++;
    else nFail++;
}

int main()
{
    MyDate d(1985,12,20);

    // To test the code
    test(d.getYear()==1985);
    test(d.getMonth()==11);
    test(d.getDate()==20);

    // Returns the test results
    cout<<"Passed:"<<nPass<<", Failed:"<<nFail<<endl;

    return 0;
}
相关文章
  • Defensive programming 2009-10-30

    1, defensive programming. Should be considered when necessary to take protective measures to protect copies of private data within the first look at the following example: pubic final class Period ( private final Date start; private final Date end; p

  • Reconstruction of the practice of defensive programming excess 09_ 2010-12-04

    Here are some exception handling code. public interface ExceptionHandler { public boolean aboutToHandle(Exception ex); public void handle(Exception ex); } public class NullPointerExceptionHandler implements ExceptionHandler { @Override public boolean

  • One of defensive programming 2010-02-07

    1, typical assert () implementation. #ifdef NDEBUG #define assert(cond) ((void)0) #else // Guard, the file name and line number void assertImpl(const* char,const* char,long); #define assert(cond) \ ((cond)? (void)0:assertImpl(???)) #endif Mechanism:

  • The second defensive programming 2010-02-07

    1, test program + code is faster than the direct encoding of a simple test case #include <iostream> using namespace std; class MyDate { public: MyDate(int y,int m,int d) { year=y; month=m; date=d; } int getYear(){ return year;} int getMonth(){ retur

  • The third defensive programming 2010-02-07

    1, a common macro for the output of the statement being executed. #define TEACE(ARG) cout<<#ARG<<endl; ARG 2, a follow new procedures for dynamically allocated memory #include <cstdio> #include <cstddef> #include <cstdlib> #i

  • The scalability of software programming and code a little defensive thinking 2010-09-30

    Software is scalable from a programmer's point of view I understand, it should be like this: Code should be scalable, easy to read others, the code is non-violent (non-violent means is that I changed to a place where the other is similar at the same

  • PHP 5 programming habit 2010-11-08

    Original :http://phpe.net/2010/08/php-coding-5goodhabits/ Some people have asked, excellent programmers and Daniel, What is the difference , About 10 to 20 Kinds of it. Because Daniel has a good programming practice and experience , So they are very

  • C + + programming ideas 2011-07-22

    C + + programming ideas Basic information Original title: Thinking in C + + Volume One: Introduction to Standard C + +, & Volume 2: Practical Programming (2nd Edition) Author: (U.S.) Bruce Eckel Chuck Allison Translator: Liuzong Tian Yuan Siu Shan Pa

  • Everyone says "We are what Microsoft's development (English)" 2009-09-25

    This book-related information: http://turingbooks.javaeye.com/admin/blogs/379933 See details "This book is a good balance between the two aspects of management and technology, related to software modeling, security design, such as defensive programmi

  • The world's first comprehensive software giant Microsoft to reveal technical secret of success 2009-09-25

    Media Comments "This book is a good balance between the two aspects of management and technology, related to software modeling, security design, such as defensive programming. The application of best practices provided by the book, you can make devel

  • [Zz] programmers ability to Programmer Competency Matrix Matrix 2010-10-08

    [Translation] programmers ability to Programmer Competency Matrix Matrix Note: Each level of knowledge is increasing, and in the level of n, also contains all you need to understand the levels of n less than knowledge. Computer Science, Computer Scie

  • Model consistency and code quality on the thinking 2010-10-15

    I feel that code quality is important, many programmers are not seriously out of code quality, very often we think that if the function is enough to do it, but in fact is not enough. I feel this way, one is not product-level code, but toy program. We

  • <Code Complete 2> reading notes (d) 2010-10-26

    Today to talk about defensive programming and the preparation of pseudo-code on the system benefits. First of all, the main idea of defensive programming is: subroutine error data should not be passed due to damage, even if it is an error generated b

  • Programmer Competency Matrix you belong to which layer? 2010-11-13

    Original http://static.icybear.net/% 5BCN% 5DProgrammer% 20competency% 20matrix.htm [Translation] programmers ability to Programmer Competency Matrix Matrix Note: Each level of knowledge is increasing, and in the level of n, also contains all you nee

  • Library function on a point design philosophy 2010-12-24

    Many functions in the library design is very clever, so far, I still understand how their design philosophy is kind of like on the LinkedList, and which has a function: public E getFirst() { if (size==0) throw new NoSuchElementException(); return hea

  • On model consistency and code quality of thinking 2010-10-15

    I feel it is important to code quality, a lot of programmers out quality code very seriously, often people think that if the function is enough to do it, but in fact is not enough. I felt like this out of the code is not the product level, but the to

  • Read excerpt 3 --- Oracle sequence of use, restricted session, instance and database difference 2010-10-07

    1 for data items to be increasing problems, the correct solution for each database using the most appropriate code. In Oracle, the code should be as follows (Assuming the table T needs generated by the primary key): create table t (pk number primary

  • 笔记:inside c++ object model 2012-03-09

    chap 1 object lessons we have several coding style: l procedure: for some algorithm, sth general, now these things are in the form of templates from time to time; l abstract data type(ADT): class, public, private-. l object oriented: virtual and inhe

  • Guava学习笔记 第6个记录(Immutable(不可变)集合) 2014-02-13

    不可变集合,顾名思义就是说集合是不可被修改的.集合的数据项是在创建的时候提供,并且在整个生命周期中都不可改变. 为什么要用immutable对象?immutable对象有以下的优点: 1.对不可靠的客户代码库来说,它使用安全,可以在未受信任的类库中安全的使用这些对象 2.线程安全的:immutable对象在多线程下安全,没有竞态条件 3.不需要支持可变性, 可以尽量节省空间和时间的开销. 所有的不可变集合实现都比可变集合更加有效的利用内存 (analysis) 4.可以被使用为一个常量,并且期望

  • Java对象的生命周期 2014-07-31

    在创建一个Java类的对象之前,需要由虚拟机加载该类,然后对该类进行链接和初始化.初始化完成之后,才能创建出该Java类的新的对象实例.对象也有自己的初始化过程,主要通过调用对应Java类的特定构造方法来完成.当不再有引用指向一个对象时,这个对象成为垃圾回收器的候选.对象所占用的内存空间会在合适的时机被垃圾回收器回收.对象终止机制提供了一种方式在对象被回收之前进行清理工作. 当需要复制一个对象时,可以使用clone方法,如果需要将对象状态持久化,可以使用序列化机制来得到一个方便存储的字节流. 1