他的看了一天的空间虽然他近日

展开阅读全文

我该怎么搞他?虽然只有3行代码

01-22

和大家讨论一个函数设计的问题,在这方面我是一个门外汉,还希望大家多帮助rnrn我解释一下,这里的代码我用的是java的,但应该对于各位不会存在任何问题的,都是相通的,我也是出于项目的要求才写java的,关键是思路,和具体的语言应该没什么关系.rn有必要解释一下的是:rn对于c++来说通过debug和release来决定是否启用assertrn对于java来说则是通过参数-ea,-da来决定是否启用,其实原理是一样的,可能我下面的说法有些混.rnrn函数很简单,其实就3行代码,但却让我想了一天的时间来考虑该怎么搞他rnrnpublic void joinGroup(Group group) rn if(group == null)rn return;rn rn groups.add(group);rnrnrnrn这里的group势必不能为null,其实这是一种约定,因为传入null没有任何意义啊rn所以传入null应该属于错误,这种类型的错误应该在开发阶段予以发现,其实这里rn改为assert的作用在于帮助这个方法的client发现错误并修复.rnrn=>rnpublic void joinGroup(Group group) rn assert group != null;rn rn group.add(group);rnrnrn但同时需要考虑的是一旦release后,则这个方法处于无保护的状态下,如果遇到rnnull的话,这里会将null add至groups,但一定会在后面的某个地方产生NullPointerrn的异常.所以仅仅是assert还是不够的,因为assert只是在debug阶段帮助你一下,rn但绝对不可能就能保证在release以后就不会再发生传入null的可能性,这取决于rn测试的完整性.这种完整性只是理论上的,相对的.rnrn那我们又该如何处理最终产品中的这个问题呢?rn有几种可选的方法,一种就是if null then return,另一种则是exceptionrn把持不包庇错误的原则,我们应该还是首选exceptionrnrn=>rnpublic void joinGroup(Group group) rn assert group != null;rnrn if (group == null)rn throw new NullPointerException();rnrn group.add(group);rnrnrnrn这样一来,assert还有什么意义呢?rn而且也违反了exception的初衷rn开始郁闷rnrn于是google =>rn任何一个失败了的Assertion都应该当作Bug来处理。rn需要注意的是,不要将Assert和Exception的处理(Try…Catch…Finally)混淆,rnAssert处理的是一些不应该发生的错误情况,而Exception处理的是由于各种原因rn(如网络环境,服务器Down机,数据库Crash等等)产生的异常。rn:http://www.microsoft.com/china/community/program/originalarticles/techdoc/debuggingInNET.mspx rnrnGood!rn这样的说法故然没错,可是...rn一定能确保在release之前debug出所有的bugrnmission impossible,一来是时间问题,二来就是测试成本的问题,三还有环境的问题rn我们是不可能对所有用户的环境进行测试的,不是吗?rn那么万一没有在debug阶段把这个bug消灭之前就release(这也是常有的事了),然后呢?rn又该怎么办?rnrn没办法吧,google again...吃饭先rnBTW:其实到了这里问题也变得明晰多了,rn 首先就是要清楚这里是错误还是异常rn 错误一般来说意味着BUG和shutdownrn 而异常只是超出一般正常的情况,相当于需要安排另一条路,可以看成是一个elsern 这里处理异常的话首先排除return,然后就是要遵守不能包庇错误的原则.rn 剩下的就应该是确定异常和assert的时机问题了.rn 慢慢来rnrnrn事情总是会有办法的,虽然我现在还是不知道该怎么办,但我现在至少知道了rnassert+exception一定是不对的.为什么呢?rnrn从蔡学墉的Shit? Happens中看到assert会别编译器当成以下代码来编译:rnif (ea) rn  if( statement ) rn   throw new AssertionError();rn  rnrn换句话说如果我同时写rnassert group != null;rnrnif(group == null)rn throw NullPointerException();rn其实是自欺欺人,多此一举rn变成在打开assert的时候,即debug的时候连写了丢两次异常的代码,rn而release的时候只不过变成了一次异常而已,如果是这样的话,releasern的时候把assert开着不就ok了,或者说就直接写throw,那基本上assertrn就废掉了.总之同时使用两者肯定是不对的啦rnrn过去我对assert的阴影在于报错,如果给用户一个程序用了一会就突然rnassert Failure那会有多黑暗啊,所以赶紧编一个release,至少就不会弹出rn框来,显然我很肤浅...别砸我..rn后来才懂原来是因为效率的原因.rn因为你知道能release的程序一定是assert怎么都不会错的,如果assert还会rn时不时的弹一下说明程序还有bug,不是通过release就万事大吉了.rn换言之,如果说我根本不考虑效率的问题,那么assert放在release中也不会rn产生除任何效率以外其他副作用.rnrn那么我是否能考虑给客户的就是打开assert的版本,因为我不在乎速度rnassert group != 0;rngroups.add(group);rn这样就ok了,在目前的情况达到我所有的目的,因为我现在对速度没有任何要求.rn别砸我,是的,当时就是这样rnrn不过我内心深处知道他依然不完美,怎么可以这样呢?rn事实上,一定很多人这样写rnif(group != null)rn groups.add(group);rn我过去就是这样的,不过这种是最没有效率,最自欺欺人,最要被批的写法rn可能很多人认为if对效率来说可以说是微乎其微,可是却包庇了错误,rn我们的目的在于写出没有bug的代码,但一定会有一个过程,从开发阶段到用户使用rn的阶段,我们都在为这个目标所努力,有些东西想躲是躲不过去的,rn难道有人认为写一句if就万事大吉了吗?我想一定会出问题的,只是时间的问题而已.rn而且严重增加了后期排查错误的难度.rn开发阶段我们应该倚仗assert来帮助我们debug,而交付以后,可能更加需要考虑由rn用户来帮助你来发现问题,解决问题,怎么在这之间建立桥梁,可能光靠assert是不rn够的,这样的体验直接导致下次不会再找你替他开发软件,不行不行.rn该怎么办呢?rn大家能帮忙说说你的思路吗? 论坛

眼见他起高楼,眼见他宴宾客,眼见他楼塌了。

07-05

我很反感这种强制结帖的做法。rn1.如果没有人给出满意的解答,那么把分给谁呢?rn2.如果没有一个回帖,又如何给分呢?难道自己 gz 一下,然后把分给自己?rn我更加反感这种禁止发帖的处罚,如果一个家伙度蜜月回来发现被禁言了,该作何感想?rnrn站在管理者的角度,论坛作为一个非赢利机构,其最大的价值就是人气。首先是人,要以人为本;其次是气,这个东东是易散难聚的。论坛不是军营,不需要铁的纪律,更不需要流水的兵。rnrnrnv主  题: 论坛解贴通知(2002.7) rn作  者: hcat1999 (阿猫) rn等  级: rn信 誉 值: 100 rn所属论坛: 建议和意见 rn问题点数: 0 rn回复次数: 66 rn发表时间: 2002-7-4 11:56:58 rnrn1、历史帖子的处理rnrn配合本次改版,CSDN论坛将清理历史帖子,请大家将自己2002年1月1日前发出的帖子在7月30前结帐给分rn如果实在无法解决的问题,请平均给分,然后重新提问。rnrn如果不能按时解贴的我们将用程序自动结贴,对于2002年1月1日前的帖子结贴率<80% 或者大于5个未结的rn用户将列入论坛禁止发贴用户。rnrn2、结贴率控制rn本次改版结贴率成为信誉分改动的一个重点,包括,移贴不在扣信誉分rn一定时期内结贴率下降的用户将扣除信誉分(具体方案还需要讨论)rn以后结贴统计以一个月为一个周期,3个月前的帖子必须结帐。rnrn当然这些我们都会配有短消息或者email通知rnrn 论坛

看了一天也没找到问题

11-27

#include "currency.h"rn#include rnrnCCurrency::CCurrency(sign s, unsigned long d, unsigned int c)rnrn if (c > 99) rn cerr << "Cents should be < 100" << endl;rn exit(1);rn rnrn sgn = s;rn dollars = d;rn cents = c;rnrnrnbool CCurrency::Set(sign s, unsigned long d, unsigned int c)rnrn if (c > 99) rn return false;rn rnrn sgn = s; rn dollars = d;rn cents = c;rnrn return true;rnrnrnbool CCurrency::Set(float a)rnrn if (a < 0) rn sgn = minus;rn a = -a;rn rn else rn sgn = plus;rn rnrn dollars = a;rn cents = (a + 0.005 - dollars) * 100;rnrn return true;rnrnrnCCurrency CCurrency::Add(const CCurrency& x) constrnrn long a1, a2, a3;rn CCurrency ans;rnrn a1 = dollars * 100 + cents;rnrn if (sgn == minus) rn a1 = -a1;rn rnrn a2 = x.dollars * 100 + x.cents;rnrn if (x.sgn == minus) rn a2 = -a2;rn rnrn a3 = a1 + a2;rnrn if (a3 < 0) rn ans.sgn = minus;rn a3 = -a3;rn rn else rn ans.sgn = plus;rn rnrn ans.dollars = a3 / 100;rnrn ans.cents = a3 - ans.dollars * 100;rnrn return ans;rnrnrnCCurrency &CCurrency::Increment(const CCurrency &x)rnrn *this = Add(x);rnrn return *this;rnrnrnCCurrency CCurrency::operator +(const CCurrency &x) constrnrn CCurrency y;rn y.amount = amount + x.amount;rnrn return y;rnrnrnvoid CCurrency::Output(ostream &out) constrnrn long a = amount;rnrn if (a < 0) rn out << '-';rn a = -a;rn rnrn long d = a / 100;rn out << '$' << d << '.';rn int c = a - d * 100;rnrn if (c < 10) rn out << "0";rn out << c;rn rnrn ostream &operator<<(ostream &out, const CCurrency &x)rn x.Output(out);return out;rnrnrnrn问题如下:rnompiling...rnCurrency.cpprnx:\currency\currency.h(6) : error C2629: unexpected 'class CCurrency ('rnx:\currency\currency.h(6) : error C2238: unexpected token(s) preceding ';'rnx:\currency\currency.h(8) : error C2061: syntax error : identifier 'sign'rnx:\currency\currency.h(10) : error C2146: syntax error : missing ';' before identifier 'Sign'rnx:\currency\currency.h(10) : error C2501: 'sign' : missing storage-class or type specifiersrnx:\currency\currency.h(12) : warning C4183: 'Sign': member function definition looks like a ctor, but name does not match enclosing classrnx:\currency\currency.h(29) : error C2146: syntax error : missing ';' before identifier 'sgn'rnx:\currency\currency.h(29) : error C2501: 'sign' : missing storage-class or type specifiersrnx:\currency\currency.h(29) : error C2501: 'sgn' : missing storage-class or type specifiersrnx:\currency\currency.cpp(4) : error C2065: 'sign' : undeclared identifierrnx:\currency\currency.cpp(4) : error C2146: syntax error : missing ')' before identifier 's'rnx:\currency\currency.cpp(4) : error C2350: 'CCurrency::CCurrency::CCurrency' is not a static memberrnx:\currency\currency.cpp(4) : error C2059: syntax error : ')'rnx:\currency\currency.cpp(5) : error C2143: syntax error : missing ';' before ''rnx:\currency\currency.cpp(5) : error C2447: missing function header (old-style formal list?)rnx:\currency\currency.cpp(16) : error C2146: syntax error : missing ')' before identifier 's'rnx:\currency\currency.cpp(16) : error C2350: 'CCurrency::Set' is not a static memberrnx:\currency\currency.cpp(16) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)rnx:\currency\currency.cpp(16) : error C2059: syntax error : ')'rnx:\currency\currency.cpp(17) : error C2143: syntax error : missing ';' before ''rnx:\currency\currency.cpp(17) : error C2447: missing function header (old-style formal list?)rnx:\currency\currency.cpp(32) : error C2065: 'sgn' : undeclared identifierrnx:\currency\currency.cpp(32) : error C2065: 'minus' : undeclared identifierrnx:\currency\currency.cpp(36) : error C2065: 'plus' : undeclared identifierrnx:\currency\currency.cpp(39) : warning C4244: '=' : conversion from 'float' to 'unsigned long', possible loss of datarnx:\currency\currency.cpp(40) : warning C4244: '=' : conversion from 'double' to 'unsigned int', possible loss of datarnx:\currency\currency.cpp(58) : error C2039: 'sgn' : is not a member of 'CCurrency'rn x:\currency\currency.h(4) : see declaration of 'CCurrency'rnx:\currency\currency.cpp(65) : error C2039: 'sgn' : is not a member of 'CCurrency'rn x:\currency\currency.h(4) : see declaration of 'CCurrency'rnx:\currency\currency.cpp(69) : error C2039: 'sgn' : is not a member of 'CCurrency'rn x:\currency\currency.h(4) : see declaration of 'CCurrency'rnx:\currency\currency.cpp(87) : error C2556: 'class CCurrency __thiscall CCurrency::operator +(const class CCurrency &) const' : overloaded function differs only by return type from 'class CCurrency &__thiscall CCurrency::operator +(const class CCurrrnency &) const'rn x:\currency\currency.h(22) : see declaration of '+'rnx:\currency\currency.cpp(87) : error C2040: '+' : 'class CCurrency (const class CCurrency &) const' differs in levels of indirection from 'class CCurrency &(const class CCurrency &) const'rnx:\currency\currency.cpp(89) : error C2039: 'amount' : is not a member of 'CCurrency'rn x:\currency\currency.h(4) : see declaration of 'CCurrency'rnx:\currency\currency.cpp(89) : error C2039: 'amount' : is not a member of 'CCurrency'rn x:\currency\currency.h(4) : see declaration of 'CCurrency'rnx:\currency\currency.cpp(95) : error C2511: 'Output' : overloaded member function 'void (class ostream &) const' not found in 'CCurrency'rn x:\currency\currency.h(4) : see declaration of 'CCurrency'rnmain.cpprnx:\currency\currency.h(6) : error C2629: unexpected 'class CCurrency ('rnx:\currency\currency.h(6) : error C2238: unexpected token(s) preceding ';'rnx:\currency\currency.h(8) : error C2061: syntax error : identifier 'sign'rnx:\currency\currency.h(10) : error C2146: syntax error : missing ';' before identifier 'Sign'rnx:\currency\currency.h(10) : error C2501: 'sign' : missing storage-class or type specifiersrnx:\currency\currency.h(12) : warning C4183: 'Sign': member function definition looks like a ctor, but name does not match enclosing classrnx:\currency\currency.h(29) : error C2146: syntax error : missing ';' before identifier 'sgn'rnx:\currency\currency.h(29) : error C2501: 'sign' : missing storage-class or type specifiersrnx:\currency\currency.h(29) : error C2501: 'sgn' : missing storage-class or type specifiersrnx:\currency\main.cpp(6) : error C2065: 'plus' : undeclared identifierrnx:\currency\main.cpp(7) : error C2065: 'minus' : undeclared identifierrnx:\currency\main.cpp(8) : warning C4305: 'argument' : truncation from 'const double' to 'float'rnx:\currency\main.cpp(10) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CCurrency' (or there is no acceptable conversion)rnx:\currency\main.cpp(12) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CCurrency' (or there is no acceptable conversion)rnx:\currency\main.cpp(14) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CCurrency' (or there is no acceptable conversion)rnx:\currency\main.cpp(16) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CCurrency' (or there is no acceptable conversion)rnx:\currency\main.cpp(17) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CCurrency' (or there is no acceptable conversion)rnError executing cl.exe.rnrnCurrency.exe - 45 error(s), 6 warning(s) 论坛

没有更多推荐了,返回首页