Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 365 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
365
Dung lượng
6,06 MB
Nội dung
www.it-ebooks.info
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
www.it-ebooks.info
iv
Contents at a Glance
About the Author xiii
About the Technical Reviewer xiv
Acknowledgments xv
Introduction xvi
Chapter 1: Ruthlessly Helpful 1
Chapter 2: NET Practice Areas 15
Chapter 3: Achieving Desired Results 37
Chapter 4: Quantifying Value 55
Chapter 5: Strategy 71
Chapter 6: NET Rules and Regulations 85
Chapter 7: Powerful C# Constructs 107
Chapter 8: Automated Testing 129
Chapter 9: Build Automation 173
Chapter 10: Continuous Integration 201
Chapter 11: Code Analysis 231
Chapter 12: Test Frameworks 265
Chapter 13: Aversions and Biases 313
Index 343
www.it-ebooks.info
xvi
Introduction
Pro .NETBestPractices is a book that was primarily written for professional software developers who
want to bring new and different .NETpractices to their teams and organization. The phrase best practice
is a handy way to describe widely-used practices and generally-accepted standards that many .NET
developers in many organizations have found to be good and effective practices. Because bestpractices
cannot work in every situation, the book encourages you to sort out what may or may not work well for
your situation.
The adoption of new and different practices is a central theme of this book. Consider the four
levels at which bestpractices are embraced:
• Individual: You or any individual adopts better practices to increase personal and
professional productivity, quality, thoroughness, and overall effectiveness.
• Group: The team adopts better practices to be more industrious together, avoid
problems, achieve desired results, improve interpersonal relationships, and work
more effectively with other teams.
• Organization: Your company, agency, or firm adopts better practices to bring
more positive outcomes to the organization, attract and retain employees, satisfy
end-user expectations, and make stakeholders happy.
• Profession: Better practices are widely adopted and become generally-accepted
standards, patterns, and principles that bring alignment to software development
and benefit to all that follow them.
In an ideal world, bestpractices are quickly adopted at all four levels. However, this is not
realistic for many of the reasons discussed in this book’s early chapters. Pragmatists know that they
usually cannot control all four levels within their current circumstances and can only influence a few.
And so they work within their span of control and spheres of influence. As you read this book, think
about adopting better practices within those areas you can control and positively shaping those areas
that you do influence.
Pro .NETBestPractices is a practical reference on the bestpractices that you can apply to your
.NET projects today. You will learn standards, techniques, and conventions that are realistic and helpful
to achieving results. The book covers a broad range of practices and principles with an emphasis on
tools and technologies for
• Automated testing
• Build automation
• Continuous integration
• Code analysis
www.it-ebooks.info
INTRODUCTION
xvii
To get warmed up, you can turn to the sample .NETbestpractices scorecard in Appendix B. If
you take the time to score your current project, what might the score tell you about where things
currently stand? Hopefully, completing the scorecard prompts these kinds of questions:
• What are the bestpractices you want to see adopted? Why are they important to
you? Do you foresee any obstacles or barriers?
• What practice on the scorecard should you, your team, or your organization focus
on first? Do you prioritize practices that offer greater efficiencies or help to
prevent problems?
• How would you revise the scorecard to make it fully apply to your current
situation? What practices would you add to the list? What practices would you
remove?
• Once you have the scorecard that is right for you, how often would you reassess it?
Is yearly too infrequent? Is weekly too intrusive and demotivating?
Adopting bestpractices is an initiative that is guided by having relevant objectives and a way to
track progress. Put together the right scorecard, based on your situation. Track it regularly and use the
overall score as a gauge that indicates and validates your continuous improvement.
If I find 10,000 ways something won’t work, I haven’t failed. I am not
discouraged, because every wrong attempt discarded is another step
forward.
Thomas Edison
The complex problems in life require iterative solutions. Edison knew that patience and
perseverance are an inventor’s competitive advantage. The same is true for best practices. It is better to
take an iterative and incremental approach to adopting new and different practices. Be patient with it,
stick to it, and have fun with it.
Who This Book Is For
This book is for anyone who has a stake in bringing better practices to software development.
Developers
As a developer, you have personal practices that make you an effective software developer. Are there
new and different practices that could make you a more effective developer? This book offers realistic,
practicable, and truly helpful best practices. The early chapters focus on the benefits, relevance, and
purpose behind adopting best practices. The later chapters focus on technologies, tools, and techniques
that bring greater effectiveness. The final chapter examines the influence of biases and aversions and
how you can overcome these obstacles to adopting better practices.
www.it-ebooks.info
INTRODUCTION
xviii
Team Leaders
As a team leader, you see the team develop software through their current practices. Could the team
perform better and achieve better outcomes with new and different practices? You can expect that this
book will offer you pragmatic advice on how to implement suitable bestpractices likely to be followed by
project members. You learn how to get bestpractices started and keep them from falling out of practice.
You learn how to gain support for following bestpractices from all stakeholders by recognizing and
overcoming biases, highlighting the gap between current results and desired results, and demonstrating
the value of following best practices.
How This Book Is Structured
Pro .NETBestPractices presents the topics to provide reasonable breath and go into depth on key
practices. For example, the chapter on code analysis looks at both static and dynamic analysis, and it
goes into depth with FxCop and StyleCop. The goal is to strike the balance between covering all the
topics, discussing the widely-used tools and technologies, and having a reasonable chapter length.
Chapters 1 through 5 are focused on the context of new and different practices. Since adopting
better practices is an initiative, it is important to know what practices to prioritize and where to uncover
better practices within your organization and current circumstances.
• Chapter 1 shows how to choose new and different practices that are better
practices for you, your team, and your organization.
• Chapter 2 draws out ways to uncover better practices in the areas of .NET and
general software development that provide an opportunity to discover or learn
and apply better practices.
• Chapter 3 presents practical advice on how to get team members to collaborate
with each other and work toward a common purpose.
• Chapter 4 describes specific practices to help with quantifying the value of
adopting better development practices.
• Chapter 5 provides you with practices to help you focus on strategy and the
strategic implications of current practices.
Chapters 6 through 9 are focused on a developer’s individual practices. These chapters discuss
guidelines and conventions to follow, effective approaches, and tips and tricks that are worth knowing.
The overarching theme is that each developer helps the whole team succeed by being a more effective
developer.
• Chapter 6 helps sort out the generalized statements, principles, practices, and
procedures that best serve as.NET rules and regulations that support effective and
innovative development.
• Chapter 7 is an informal review of the C# language’s power both to harness its own
strengths and to recognize that effective development is a key part of following
.NET practices.
• Chapter 8 describes many specific practices to improve test code, consistent with
the principles behind effective development and automated testing.
www.it-ebooks.info
INTRODUCTION
xix
• Chapter 9 discusses using build automation to remove error-prone steps, to
establish repeatability and consistency, and to improve the build and deployment
processes.
Chapters 10 through 12 are focused on supporting tools, products, and technologies. These
chapters describe the purpose of various tool sets and present some recommendations on applications
and products worth evaluating.
• Chapter 10 presents the continuous integration lifecycle with a description of the
steps involved within each of the processes. Through effective continuous
integration practices, the project can save time, improve team effectiveness, and
provide early detection of problems.
• Chapter 11 provides an overview of many static and dynamic tools, technologies,
and approaches with an emphasis on improvements that provide continuous,
automated monitoring.
• Chapter 12 is a comprehensive list of testing frameworks and tools with a blend of
commercial and open-source alternatives.
The final chapter, Chapter 13, is about the aversions and biases that keep many individuals,
teams, and organizations from adopting better practices. You may face someone’s reluctance to accept
or acknowledge a new or different practice as potentially better. You may struggle against another’s
tendency to hold a particular view of a new or different practice that undercuts and weakens its
potential. Many people resist change even if it is for the better. This chapter helps you understand how
aversions and biases impact change so that you can identify them, cope with them, and hopefully
manage them.
Appendix A provides a list of resources organized by topic. Many of the resources are either
referenced throughout the book or are good sources for further reading. Either way, once you are ready
to tackle a .NET practice area, this appendix is a good way to delve into the details.
Appendix B provides a scorecard for you to use while evaluating and tracking best practices. As
discussed earlier in the Introduction, this scorecard is a starting point for your ongoing initiative to bring
better practices to .NET development.
Prerequisites
To follow along with the examples in this book, you need the following:
• A good working knowledge of the .NET Framework 4.0 and the C# language
• Microsoft Visual Studio 2010. Many examples in the text refer to features of the
Professional, Premium, or Ultimate editions of Visual Studio 2010. A few code
samples work with Visual Studio 2010 Express.
• Many samples use Microsoft SQL Server Express, but other versions should work
fine. Some use the database that comes with the MVC Music Store tutorial,
available at mvcmusicstore.codeplex.com.
• NUnit version 2.5.10 or later, available at nunit.org.
• Moq version 4.0.0 or later, available at code.google.com/p/moq.
www.it-ebooks.info
INTRODUCTION
xx
• FxCop version 10.0 or later. I wrote a blog post that describes how to extract the
FxCop 10.0 setup program from the Microsoft Windows SDK for Windows 7 and
.NET Framework 4. With the FxCopSetup.exe file, you can run the installer on your
development machine or build server. See
http://ruthlesslyhelpful.net/2011/06/09/liberate-fxcop-10-0/.
• StyleCop version 4.5.25 or later, available at stylecop.codeplex.com.
Many other tools and technologies are presented and discussed throughout the book.
Information on obtaining those specific applications is provided at the time the topic is discussed.
Downloading the Code
This book includes source code that is available for download. The source code illustrates the practices
described in the book. It is not production code. You can download the code at www.apress.com by
searching for and going to the detail page of Pro.NETBest Practices. On the book’s detail page is a link to
the sample code compressed into a ZIP file. You can use a utility like 7-Zip (7-zip.org) or WinZip to
uncompress the code.
For more detailed information, go to http://www.apress.com/source-code/.
Contacting the Author
Stephen can be reached through
• Excella Consulting: www.excella.com
• E-mail: stephen.ritchie@excella.com
• LinkedIn: www.linkedin.com/in/sritchie
• Blog site: ruthlesslyhelpful.net
www.it-ebooks.info
C H A P T E R 1
1
Ruthlessly Helpful
The phrase “best practices” is sometimes difficult to accept. “Best practices” is really a concept that has
become a part of the software development vocabulary; however, the phrase can be troublesome
because not every best practice is clearly a better practice for all situations. In fact, a practice that
improves one situation might worsen another situation. For that reason, this book avoids the phrase
“best practices” and favors “ruthlessly helpful practices.” That phrase embodies the idea that a ruthlessly
helpful practice for you might not be right for others, which is fine. It embodies an attitude of healthy
skepticism, situational relevance, and judgment. In this chapter, you learn just what that phrase means,
how it relates to selecting practices, and how to apply that attitude to those areas that you feel need
improvement.
The word ruthless serves as a contrast to the passion and bias in the word best. Best is a superlative;
there is nothing better than the best. That word is often used to press the point that no other practice
needs to be considered. Some use it to shut down discussion and debate. In reality, every new and
different practice needs to be carefully considered. To be ruthless, you must discount the biased
arguments and zealous opinions. You want to select practices that are right for you and your team.
The word helpful tries to put the focus on results and positive outcomes. In the end, a new and
different practice represents a change that must show results. The results could be in fewer problems or
faster problem resolution. The results could be improvements in delivery, quality, and relationships. The
results could be in greater job satisfaction. You want to select practices that get results for you and your
team.
The most important takeaway of this chapter is that this entire book is about how to choose new
and different practices that are better practices for you, your team, and your organization. Feel free to
call them bestpractices or ruthlessly helpful practices but, in the end, you ought to see them as the
practices that are entirely appropriate to you.
COMMENTARY
Selecting the best practice from among the many good practices is not easy. In fact, making hard
decisions and dealing with dilemmas is a challenge for many projects. One technique I have learned to
avoid is listing out the pros and cons. I rarely find that this approach gets to the heart of the decision. The
list does not present a clear choice or direction. When selecting a new or different practice I focus on two
questions:
• Is the practice not good enough to apply in this
situation?
• Is the practice too good to pass up in this situation?
www.it-ebooks.info
CHAPTER 1 RUTHLESSLY HELPFUL
2
The first question attempts to exclude the practice because the situation is not right, the risks are too
great, or the benefits are too marginal. The second question attempts to include the practice because it
addresses current problems, has few risks, or the likely benefits are significant. This approach seems to
make the decision less difficult and resolves the dilemmas that many practices present. The right choice
becomes clear and better practices are adopted.
As a developer on a large, multiyear software development project, I observed a curious phenomenon.
Every couple of weeks, a consultant would spend the afternoon talking to the developers about the
problems we were experiencing. This consultant came to be known as “the professor” and the meetings
were known as the “drive-by” meetings. Most of the professor’s advice left the team confused and
uncertain. We were rarely able to put the advice into practice. It all seemed very theoretical and not very
appropriate to our situation. Some of what we did try either did not work or seemed to make things worse.
What struck me as curious was that our team was in trouble, yet we spent a lot of time listening to and
considering these unrealistic and impractical suggestions. Today I know that better practices prove
themselves through application and results, and I insist on seeing both.
On an earlier project, I had a very challenging programming assignment. I was given a set of detailed
specifications and tasked with implementing them. The document was very difficult to read and the
information extremely complex. I went to the business analyst who wrote the specifications to see if he
would explain them to me. That started out as a big mistake. He was arrogant, condescending, and hostile.
At one point, he said the material was so straightforward that even a five-year-old could understand it. For
some reason, what he said got me to thinking and I said, “I need you to explain it to me like I am a five-
year-old.”
The analyst stopped and thought for a minute. Suddenly, his whole demeanor changed. He went to the
board and started explaining everything from the beginning. He gave me an overview of the requirements.
As he explained things, I took notes and asked relevant questions. Whenever he went too deep or too
technical, I would repeat that I needed him to explain it to me like I’m a five-year-old. Today, I find it a
ruthlessly helpful practice to ask a business analyst to explain requirements that I do not understand in the
most simplistic terms.
Practice Selection
This book presents standards, techniques, and conventions that many professional .NET developers
would agree are very good practices. People with different experiences or expertise might believe there
are better practices. A ruthlessly helpful practice represents a point-of-view and an assertion that
following the given practice is both sensible and beneficial. Common sense dictates that having a set of
sound, helpful practices in place today is more useful than spending a lot of time researching and
selecting the best practice. It is important to have an efficient way to select new and different practices
that focus on improving outcomes.
In the book Rapid Development,
1
Steve McConnell provides a list of 27 best practices. In addition to
that list, the book provides tables of many best practice candidates and a summary of best practice
1
Steve McConnell, Rapid Development (Redmond, WA.: Microsoft Press, 1996).
www.it-ebooks.info
[...]... illustrates the impact of better practices on the productivity in achieving desired results In the early stages of the project, results are produced during each sprint The team believes the productivity can increase with better practices A new or different practice increases productivity and more is accomplished Later on, additional changes to practices continue to improve productivity This is growing... very comprehensive treatment of the topic of best practices For commercial software development organizations looking to adopt best practices across the board this approach is a great way to organize the initiative In Rapid Development, the evaluation of best practices includes five criteria: • Potential reduction from nominal schedule • Improvement in progress visibility • Effect on schedule risk... of better practices on productivity in achieving desired results Delivery A ruthlessly helpful practice takes the current delivery situation and improves it Productivity and efficiency are improved The ability to meet milestones and deadlines is improved The team can accomplish more with the same or fewer resources These are ways that better practices improve delivery, which come from better problem... significant project this constant diet of pressure and haste is unsustainable When new and different practices focus on problem prevention and more effective issue resolution then the team is less overwhelmed by crisis Adopting better practices improves planning, coordination, and productivity Taken together, these improved approaches offer a more sustainable development environment For example, projects... introducing additional better practices further improves the situation Less effort dealing with problems means more time to devote to other important matters This is growing capability through better problem solving 7 www.it-ebooks.info CHAPTER 1 RUTHLESSLY HELPFUL Figure 1-1 The impact of better practices on the total effort spent on problems A ruthlessly helpful practice might show improvement through innovation... preconditions exist, generally-accepted and widely-used practices are useful to those project teams that are ready to benefit from the improved practices The circumstances and preconditions for common practices are better understood and more extensively discussed, which allows you to decide if the practice is appropriate and beneficial to your situation The more projects that have adopted a practice the more... not always easy or welcomed Consider new and different practices as either solving problems or bringing innovation People find it less risky to adopt better practices that solve problems Better practices that innovate frequently offer greater longterm rewards and advantages A ruthlessly helpful practice shows improvement by helping to resolve problems with less total effort For example, a change in... analysis is to find problems as early as possible by testing the limits of the method-under-test The developer anticipates potential problems as part of writing the code The developer makes sure that exceptions are handled properly This practice reveals potential 9 www.it-ebooks.info CHAPTER 1 RUTHLESSLY HELPFUL problems for the developer to address proactively These are practices that improve delivery... result of better practices is a project that is more sustainable over time Greater productivity and efficiency encourage higher levels of involvement and commitment As individuals, each team member brings more creativity and energy to the project These better practices actively improve and encourage sustainable achievement Balance A ruthlessly helpful practice brings a balanced focus on improvement to... isolate, and diagnose a problem faster Figure 1-1 is a conceptual diagram that illustrates the impact of better practices on the total effort spent on problems In the early stages of the project, significant time and effort is devoted to dealing with questions, issues, delays, defects, and other problems After a few sprints, better practices reduce the total effort devoted to dealing with problems Later on, . following best practices.
How This Book Is Structured
Pro .NET Best Practices presents the topics to provide reasonable breath and go into depth on key
practices. .
Introduction
Pro .NET Best Practices is a book that was primarily written for professional software developers who
want to bring new and different .NET practices