What's the point? The original code eliminates blank lines and comments, and rejects null type code as well. You just postpone the work. I think fail-first is much better and I don't like holding comments and nulls around.
-majorhomedepot(马甲后的炮);
2011-2-9(#6501953@0)
The point is to turn blanks, comments and unknowns into normal cases, normal meaning to be handled the same way as other cases. As a result, client code is much simpler. The work isn't postponed. It's handled by CommentStrategy or NullStrategy.
-wincity(戒书的红卫兵);
2011-2-9(#6502128@0)
It's debatable how we should handle an unknown line. I doubt the boss would be happy when he comes to work to find his reports missing because of a questionable line. But if you still want to throw an exception, you can do it in NullStrategy.
-wincity(戒书的红卫兵);
2011-2-9(#6502153@0)
I thought you would reduce complexity rather than increase complexity. What did you achieve by creating 3 more classes and carrying those unused or error objects to downstream?In this example, I think the better way to manage complexity is
1) fail-first, if you don't expect some inputs, reject them and throw exception right away, and do not pass them to downstream.
2) make decision immediately whenever you can. I don't see how client code can be simpler by having to deal with CommentStrategy, BlankStrategy and NullStrategy. They don't have to see them at all.
-majorhomedepot(马甲后的炮);
2011-2-9{394}(#6502231@0)
You misunderstood the Null Object pattern. It should be used when null is a valid value. In your original code, null is invalid. In your case of your boss' report, it is valid. There is a big difference.
-majorhomedepot(马甲后的炮);
2011-2-9(#6502260@0)
I am just wondering who really understand what he was talking about? honestly I dont.
-frankwoo(柳五随风);
2011-2-8(#6497774@0)
I don't neither. But perhaps somebody else do. I don't know much C++ now.
-iwantcar_majia(iwantcar);
2011-2-8(#6497785@0)
In your world it has "exception" too:)
-frankwoo(柳五随风);
2011-2-8(#6497793@0)
You don't need to read the code to understand the article. I don't even know what language the code is written in.
-wincity(戒书的红卫兵);
2011-2-8(#6497851@0)
装嫩。
-wincity(戒书的红卫兵);
2011-2-8(#6497788@0)
honestly I think you are puffing of nothig.
-frankwoo(柳五随风);
2011-2-8(#6497789@0)
good post. 不老不嫩哈。
-y_k_m(顺水行舟);
2011-2-8(#6498087@0)
看不懂那些CODE。 仰慕ING IT GUYS。
-trainee(trainee);
2011-2-8(#6499379@0)
Bravo! The only thing I didn't like was the song.:-) The 中心思想 was a very helpful addition -- would recommend you make it a norm (rather than an exception) from now on.
-cca(不归的如来佛);
2011-2-8(#6499673@0)
java: What Is an Exception? The term exception is shorthand for the phrase "exceptional event." c#: Exception handling features provide a way to deal with any unexpected or exceptional situations that arise while a program is running.
-xmlhttprequest(build5381);
2011-2-9(#6502118@0)
There is no big difference regarding exception in Java and C#.
-majorhomedepot(马甲后的炮);
2011-2-9(#6502134@0)
Exactly, they are all same, that's my point. Exceptions are used to handle 'exceptional situations'. Period.
-xmlhttprequest(build5381);
2011-2-9(#6502166@0)
dictionary.com: an instance or case not conforming to the general rule. "going up", "going down" is the general rule. "going nowhere" comforms to it. "" doesn't. To not return "going nowhere" creates an "exception" for downstream code.
-wincity(戒书的红卫兵);
2011-2-9(#6502176@0)
I think I get your point. You prefer to treat the exceptional cases as normal cases and thought that would reduce complexity. I strongly disagree. It adds burden to the code to handle normal cases and makes themmuch more heavy and unmaintainable. That's the reason why modern languages support the concept of exception handling.
-majorhomedepot(马甲后的炮);
2011-2-9{118}(#6502243@0)
Where do you draw the line for "正常的例外"? The null type code in your example above looks an error/exception. 电梯停着不是例外是状态,但是它该走了还停着就是不正常的例外了。
-majorhomedepot(马甲后的炮);
2011-2-9(#6502161@0)
When elevator arrives at a floor, it can go up, go down, or stay put. Staying put is a decision, not a mal-function. If you write a function to return a string, do you use "" or "stay put" or "go nowhere"? We make many such decisions every day.
-wincity(戒书的红卫兵);
2011-2-9(#6500597@0)
Great design in Windows? Definitely an exception。Programs writing to HKLM or Windows directory? The best example of Windows stupidity.
-wincity(戒书的红卫兵);
2011-2-9(#6501918@0)
What have been done are already done. Running programs with admin privilege is a stupid design, Microsoft leaned it in a hard way, and they paid great effort for fixing it (they have to use kernel filter drivers for file and registry redirection).
-liquid(豆泡松果 之 松果豆泡);
2011-2-9(#6502457@0)
Good topic. 处理exception是最头大的问题. It can easily get out of control.
-dusk(~小桥流水~);
2011-2-9(#6500542@0)
It's amazing how a high-level discussion turned to micro-detailed plumbing level debates.:-) Exception is a relative concept - relative to one's ability to anticipate. It's something out of your "rader" (expectation) FOR NOW.As the scope of your radar expends, the universe of exceptions shrinks.
If the god (or the Buddha:-) is a programmer, he would see no exception!
-cca(不归的如来佛);
2011-2-10{149}(#6502548@0)
The center of the debate is whether expanding the scope of your radar is a good thing. My answer is not in most cases. It is much better and easier to define the scope clearly as early as possible thanleave it undefined and try to expand it in the middle of a project or process. It adds system complexity, communication cost and maintenance cost. And, I can hardly see benefits even in micro level.
-majorhomedepot(马甲后的炮);
2011-2-10{198}(#6503026@0)
Did I hear "It's better to refuse to grow up" -- or "It's better to deny that we have actually grown up"? :-)Sorry if the analogy sounds a bit offending -- but I hope it conveys what I was trying to mean.
-cca(不归的如来佛);
2011-2-10{95}(#6503780@0)
The metaphor has nothing offending, in fact, it shows, without a clear definition (here, of "growing up"), how easily a serious discussion can turn into a game of wording.
-majorhomedepot(马甲后的炮);
2011-2-10(#6503850@0)
Very insightful. Still, I would make one point clear: treating exceptional cases as exceptions does not mean you don't or are not able to handle exceptions. In other words, the scope of radar does not change at all.You just handle the exceptions one way or the other. Here, it is more about when to deal with exceptions and how to deal with exceptions. Usually, it is more a preference in modelling rather than a learning process, unless your point of view is only from the "normal process" instead of the whole system.
-majorhomedepot(马甲后的炮);
2011-2-10{306}(#6504126@0)
I guess the difference is on terminology -- what I meant by "exception" is probably "uncategorized/uncharted exception" in your case.
-cca(不归的如来佛);
2011-2-10(#6504147@0)
You are absolutely right. However, I don't think the "exception" in wincity's post refers to the "uncategorized/uncharted exception".
-majorhomedepot(马甲后的炮);
2011-2-10(#6504309@0)