📄 tij318.htm
字号:
<p><a name="Index2125"></a>Testing has traditionally been relegated to the last part of a project, after you’ve “gotten everything working, but just to be sure.” It has implicitly had a low priority, and people who specialize in it have not been given a lot of status and have often even been cordoned off in a basement, away from the “real programmers.” Test teams have responded in kind, going so far as to wear black clothing and cackling with glee whenever they break something (to be honest, I’ve had this feeling myself when breaking compilers). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_316" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>XP completely revolutionizes the concept of testing by giving it equal (or even greater) priority than the code. In fact, you write the tests <i>before</i> you write the code that will be tested, and the tests stay with the code forever. The tests must be executed successfully every time you do a build of the project (which is often, sometimes more than once a day). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_317" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Writing tests first has two extremely important effects. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_318" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>First, it forces a clear definition of the interface of a class. I’ve often suggested that people “imagine the perfect class to solve a particular problem” as a tool when trying to design the system. The XP testing strategy goes further than that—it specifies exactly what the class must look like, to the consumer of that class, and exactly how the class must behave. In no uncertain terms. You can write all the prose, or create all the diagrams you want, describing how a class should behave and what it looks like, but nothing is as real as a set of tests. The former is a wish list, but the tests are a contract that is enforced by the compiler and the test framework. It’s hard to imagine a more concrete description of a class than the tests. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_319" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index2126"></a><a name="Index2127"></a>While creating the tests, you are forced to completely think out the class and will often discover needed functionality that might be missed during the thought experiments of UML diagrams, CRC cards, use cases, etc. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_320" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index2128"></a>The second important effect of writing the tests first comes from running the tests every time you do a build of your software. This activity gives you the other half of the testing that’s performed by the compiler. If you look at the evolution of programming languages from this perspective, you’ll see that the real improvements in the technology have actually revolved around testing. Assembly language checked only for syntax, but C imposed some semantic restrictions, and these prevented you from making certain types of mistakes. OOP languages impose even more semantic restrictions, which if you think about it are actually forms of testing. “Is this data type being used properly?” and “Is this method being called properly?” are the kinds of tests that are being performed by the compiler or run-time system. We’ve seen the results of having these tests built into the language: People have been able to write more complex systems, and get them to work, with much less time and effort. I’ve puzzled over why this is, but now I realize it’s the tests: You do something wrong, and the safety net of the built-in tests tells you there’s a problem and points you to where it is. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_321" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>But the built-in testing afforded by the design of the language can only go so far. At some point, <i>you</i> must step in and add the rest of the tests that produce a full suite (in cooperation with the compiler and run-time system) that verifies all of your program. And, just like having a compiler watching over your shoulder, wouldn’t you want these tests helping you right from the beginning? That’s why you write them first, and run them automatically with every build of your system. Your tests become an extension of the safety net provided by the language. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_322" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>One of the things that I’ve discovered about the use of more and more powerful programming languages is that I am emboldened to try more brazen experiments, because I know that the language will keep me from wasting my time chasing bugs. The XP test scheme does the same thing for your entire project. Because you know your tests will always catch any problems that you introduce (and you regularly add any new tests as you think of them), you can make big changes when you need to without worrying that you’ll throw the whole project into complete disarray. This is incredibly powerful. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_323" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In this third edition of this book, I realized that testing was so important that it must also be applied to the examples in the book itself. With the help of the Crested Butte Summer 2002 Interns, we developed the testing system that you will see used throughout this book. The code and description is in Chapter 15. This system has increased the robustness of the code examples in this book immeasurably. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0443" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p class="heading 3,H3"><br></p>
<h3>
<a name="Index2129"></a><a name="Index2130"></a><a name="_Toc472654701"></a><a name="_Toc24775983"></a><a name="Heading24451"></a>Pair
programming</h3>
<p>Pair programming goes against the rugged individualism that we’ve been indoctrinated into from the beginning, through school (where we succeed or fail on our own, and working with our neighbors is considered “cheating”), and media, especially Hollywood movies in which the hero is usually fighting against mindless conformity.<sup><a name="fnB114" href="#fn114">[114]</a></sup> Programmers, too, are considered paragons of individuality—“cowboy coders,” as Larry Constantine likes to say. And yet XP, which is itself battling against conventional thinking, says that code should be written with two people per workstation. And that this should be done in an area with a group of workstations, without the barriers that the facilities-design people are so fond of. In fact, Beck says that the first task of converting to XP is to arrive with screwdrivers and Allen wrenches and take apart everything that gets in the way.<sup><a name="fnB115" href="#fn115">[115]</a></sup> (This will require a manager who can deflect the ire of the facilities department.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_324" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The value of pair programming is that one person is actually doing the coding while the other is thinking about it. The thinker keeps the big picture in mind—not only the picture of the problem at hand, but the guidelines of XP. If two people are working, it’s less likely that one of them will get away with saying, “I don’t want to write the tests first,” for example. And if the coder gets stuck, they can swap places. If both of them get stuck, their musings may be overheard by someone else in the work area who can contribute. Working in pairs keeps things flowing and on track. Probably more important, it makes programming a lot more social and fun. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_325" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>I’ve begun using pair programming during the exercise periods in some of my seminars, and it seems to significantly improve everyone’s experience. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_326" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc312373815"></a><a name="_Toc472654711"></a><a name="_Toc24775984"></a><a name="Heading24457"></a>Strategies
for transition<br></h2>
<p><a name="Index2131"></a>If you buy into OOP, your next question is probably, “How can I get my manager/colleagues/department/peers to start using objects?” Think about how you—one independent programmer—would go about learning to use a new language and a new programming paradigm. You’ve done it before. First comes education and examples; then comes a trial project to give you a feel for the basics without doing anything too confusing. Then comes a “real world” project that actually does something useful. Throughout your first projects you continue your education by reading, asking questions of experts, and trading hints with friends. This is the approach many experienced programmers suggest for the switch to Java. Switching an entire company will of course introduce certain group dynamics, but it will help at each step to remember how one person would do it. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_334" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc472654712"></a><a name="_Toc24775985"></a><a name="Heading24459"></a>Guidelines</h3>
<p>Here are some guidelines to consider when making the transition to OOP and Java: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_335" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading24461"></a>1. Training</h4>
<p><a name="Index2132"></a>The first step is some form of education. Remember the company’s investment in code, and try not to throw everything into disarray for six to nine months while everyone puzzles over unfamiliar features. Pick a small group for indoctrination, preferably one composed of people who are curious, work well together, and can function as their own support network while they’re learning Java. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0106" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>An alternative approach is the education of all company levels at once, including overview courses for strategic managers as well as design and programming courses for project builders. This is especially good for smaller companies making fundamental shifts in the way they do things, or at the division level of larger companies. Because the cost is higher, however, some may choose to start with project-level training, do a pilot project (possibly with an outside mentor), and let the project team become the teachers for the rest of the company. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_336" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading24464"></a>2. Low-risk project</h4>
<p>Try a low-risk project first and allow for mistakes. Once you’ve gained some experience, you can either seed other projects from members of this first team or use the team members as an OOP technical support staff. This first project may not work right the first time, so it should not be mission-critical for the company. It should be simple, self-contained, and instructive; this means that it should involve creating classes that will be meaningful to the other programmers in the company when they get their turn to learn Java. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_337" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading24466"></a>3. Model from success</h4>
<p>Seek out examples of good object-oriented design before starting from scratch. There’s a good probability that someone has solved your problem already, and if they haven’t solved it exactly you can probably apply what you’ve learned about abstraction to modify an existing design to fit your needs. This is the general concept of <i>design patterns, </i>covered in <i>Thinking in Patterns (with Java)</i> at <i>www.BruceEckel.com</i>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_338" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Index2133"></a><a name="Index2134"></a><a name="Heading24468"></a>4.
Use existing class libraries</h4>
<p><a name="Index2135"></a>An important economic motivation for switching to OOP is the easy use of existing code in the form of class libraries (in particular, the Standard Java libraries, which are covered throughout this book). The shortest application development cycle will result when you can create and use objects from off-the-shelf libraries. However, some new programmers don’t understand this, are unaware of existing class libraries, or, through fascination with the language, desire to write classes that may already exist. Your success with OOP and Java will be optimized if you make an effort to seek out and reuse other people’s code early in the transition process. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_339" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading24470"></a>5. Don’t rewrite existing code in Java</h4>
<p>It is not usually the best use of your time to take existing, functional code and rewrite it in Java. If you must turn it into objects, you can interface to the C or C++ code using the Java Native Interface or <i>Extensible Markup Language </i>(XML). There are incremental benefits, especially if the code is slated for reuse. But chances are you aren’t going to see the dramatic increases in productivity that you hope for in your first few projects unless that project is a new one. Java and OOP shine best when taking a project from concept to reality. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_340" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p class="heading 3,H3"><br></p>
<h3>
<a name="_Toc312373817"></a><a name="Index2136"></a><a name="Index2137"></a><a name="_Toc472654713"></a><a name="_Toc24775986"></a><a name="Heading24472"></a>Management
obstacles</h3>
<p>If you’re a manager, your job is to acquire resources for your team, to overcome barriers to your team’s success, and in general to try to provide the most productive and enjoyable environment so your team is most likely to perform those miracles that are always being asked of you. Moving to Java falls
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -