Table of Contents for
Your Code as a Crime Scene

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Your Code as a Crime Scene by Adam Tornhill Published by Pragmatic Bookshelf, 2015
  1. Title Page
  2. Your Code as a Crime Scene
  3. Your Code as a Crime Scene
  4. For the Best Reading Experience...
  5. Table of Contents
  6. Early praise for Your Code as a Crime Scene
  7. Foreword by Michael Feathers
  8. Acknowledgments
  9. Chapter 1: Welcome!
  10. About This Book
  11. Optimize for Understanding
  12. How to Read This Book
  13. Toward a New Approach
  14. Get Your Investigative Tools
  15. Part 1: Evolving Software
  16. Chapter 2: Code as a Crime Scene
  17. Meet the Problems of Scale
  18. Get a Crash Course in Offender Profiling
  19. Profiling the Ripper
  20. Apply Geographical Offender Profiling to Code
  21. Learn from the Spatial Movement of Programmers
  22. Find Your Own Hotspots
  23. Chapter 3: Creating an Offender Profile
  24. Mining Evolutionary Data
  25. Automated Mining with Code Maat
  26. Add the Complexity Dimension
  27. Merge Complexity and Effort
  28. Limitations of the Hotspot Criteria
  29. Use Hotspots as a Guide
  30. Dig Deeper
  31. Chapter 4: Analyze Hotspots in Large-Scale Systems
  32. Analyze a Large Codebase
  33. Visualize Hotspots
  34. Explore the Visualization
  35. Study the Distribution of Hotspots
  36. Differentiate Between True Problems and False Positives
  37. Chapter 5: Judge Hotspots with the Power of Names
  38. Know the Cognitive Advantages of Good Names
  39. Investigate a Hotspot by Its Name
  40. Understand the Limitations of Heuristics
  41. Chapter 6: Calculate Complexity Trends from Your Code’s Shape
  42. Complexity by the Visual Shape of Programs
  43. Learn About the Negative Space in Code
  44. Analyze Complexity Trends in Hotspots
  45. Evaluate the Growth Patterns
  46. From Individual Hotspots to Architectures
  47. Part 2: Dissect Your Architecture
  48. Chapter 7: Treat Your Code As a Cooperative Witness
  49. Know How Your Brain Deceives You
  50. Learn the Modus Operandi of a Code Change
  51. Use Temporal Coupling to Reduce Bias
  52. Prepare to Analyze Temporal Coupling
  53. Chapter 8: Detect Architectural Decay
  54. Support Your Redesigns with Data
  55. Analyze Temporal Coupling
  56. Catch Architectural Decay
  57. React to Structural Trends
  58. Scale to System Architectures
  59. Chapter 9: Build a Safety Net for Your Architecture
  60. Know What’s in an Architecture
  61. Analyze the Evolution on a System Level
  62. Differentiate Between the Level of Tests
  63. Create a Safety Net for Your Automated Tests
  64. Know the Costs of Automation Gone Wrong
  65. Chapter 10: Use Beauty as a Guiding Principle
  66. Learn Why Attractiveness Matters
  67. Write Beautiful Code
  68. Avoid Surprises in Your Architecture
  69. Analyze Layered Architectures
  70. Find Surprising Change Patterns
  71. Expand Your Analyses
  72. Part 3: Master the Social Aspects of Code
  73. Chapter 11: Norms, Groups, and False Serial Killers
  74. Learn Why the Right People Don’t Speak Up
  75. Understand Pluralistic Ignorance
  76. Witness Groupthink in Action
  77. Discover Your Team’s Modus Operandi
  78. Mine Organizational Metrics from Code
  79. Chapter 12: Discover Organizational Metrics in Your Codebase
  80. Let’s Work in the Communication Business
  81. Find the Social Problems of Scale
  82. Measure Temporal Coupling over Organizational Boundaries
  83. Evaluate Communication Costs
  84. Take It Step by Step
  85. Chapter 13: Build a Knowledge Map of Your System
  86. Know Your Knowledge Distribution
  87. Grow Your Mental Maps
  88. Investigate Knowledge in the Scala Repository
  89. Visualize Knowledge Loss
  90. Get More Details with Code Churn
  91. Chapter 14: Dive Deeper with Code Churn
  92. Cure the Disease, Not the Symptoms
  93. Discover Your Process Loss from Code
  94. Investigate the Disposal Sites of Killers and Code
  95. Predict Defects
  96. Time to Move On
  97. Chapter 15: Toward the Future
  98. Let Your Questions Guide Your Analysis
  99. Take Other Approaches
  100. Let’s Look into the Future
  101. Write to Evolve
  102. Appendix 1: Refactoring Hotspots
  103. Refactor Guided by Names
  104. Bibliography
  105. You May Be Interested In…

Find Surprising Change Patterns

Start by cloning the nopCommerce git repository:

 
git clone https://git01.codeplex.com/nopcommerce

Move into your repository and generate a version-control log. Let’s look at all changes to the code from the start of 2014 until the present day (defined as 2014-09-25—the day I’m writing this):

 
prompt>​ git log --pretty=format:'[%h] %an %ad %s' --date=short --numstat \
 
--after 2014-01-01 --before 2014-09-25 > nop_evo_2014.log

Your git log is now stored in the file nop_evo_2014.log. Let’s define a transformation to go with it.

Define Each Layer as an Architectural Boundary

Just as we did for our Pipes and Filters analysis, we map each architectural part to a logical name. Here’s the transformation for nopCommerce:

 
src/Presentation/Nop.Web/Administration/Models => Admin Models
 
src/Presentation/Nop.Web/Administration/Views => Admin Views
 
src/Presentation/Nop.Web/Administration/Controllers => Admin Controllers
 
src/Libraries/Nop.Services => Services
 
src/Libraries/Nop.Core => Core
 
src/Libraries/Nop.Data => Data Access
 
src/Libraries/Nop.Services => Business Access Layer
 
src/Presentation/Nop.Web/Models => Models
 
src/Presentation/Nop.Web/Views => Views
 
src/Presentation/Nop.Web/Controllers => Controllers

I derived the transformation from the nopCommerce documentation.[29] I also had a look at the source code to identify the Model-View-Controller layers that you see below the src/Presentation/Nop.Web and src/Presentation/Nop.Web/Administration folders. (When you analyze your own system, you’re probably already familiar with its high-level design.)

Before we run the analysis, note that nopCommerce consists of two applications: one administration application and one application for the actual store. We specify both in our transformation, since they’re logical parts of the same system and have to be maintained together.

Now, store the transformation specification in the file arch_boundaries.txt, and you’re set for the analysis:

 
prompt>​ maat -c git -l nop_evo_2014.log -g arch_boundaries.txt -a coupling
 
entity,coupled,degree,average-revs
 
Admin Models,Admin Views,75,74
 
Admin Controllers,Admin Models,68,73
 
Admin Controllers,Admin Views,66,89
 
Admin Models,Core,54,76
 
Core,Services,46,130
 
Models,Views,46,47
 
Admin Views,Core,44,92
 
Admin Controllers,Core,39,91
 
Controllers,Models,38,60
 
Admin Controllers,Services,35,128
 
Admin Models,Services,35,113
 
Admin Views,Services,34,129
 
Controllers,Views,34,83
 
Controllers,Services,33,132
 
Admin Controllers,Controllers,31,92

The results reveal several cases of temporal coupling. The Admin parts of the system exhibit the strongest coupling. Let’s focus on them.

Identify Expensive Change Patterns

Remember that one idea behind the MVC pattern is to allow us to swap in different views. Since the Views change together with the Models in 75 percent of all cases, that idea is probably not fulfilled; if we do add a different set of views, those will have to change frequently as well, which will slow us down.

The following figure also shows that all components in the MVC part have a temporal dependency upon the Core and Services layers. Let’s get support from a hotspot analysis to find out how serious that is.

images/Chp10_nopCommerceCoupling.png

Use Hotspots to Assess the Severity

Instead of identifying individual modules as hotspots, we’ll reuse the transformation. That allows you to find hotspots on the level of your architecture. That is, a hotspot in this analysis corresponds to a whole layer:

 
prompt>​ maat -c git -l nop_evo_2014.log -g arch_boundaries.txt -a revisions
 
entity,n-revs
 
Services,393
 
Views,388
 
Admin Controllers,257
 
Admin Views,253
 
Controllers,181
 
Core,169
 
Data Access,122
 
Admin Models,76
 
Models,36

As you see, the Services layer has the most volatile code. That means any temporal coupling that involves Services is, on average, a more serious concern than the others. This is information we use to reason about the cost of changes—for example, when exploring design alternatives.

However, a temporal coupling analysis can’t tell us the direction of the change dependency; we don’t know whether changes to the Services lead to predictable changes in the MVC parts or whether it is the other way around. But we do know there’s a 35 percent risk that our change will affect multiple layers.

Finally, note the strange change dependency between the Admin Controllers and the Controllers that we see at the bottom of the preceding figure. The controllers in two different packages change together 31 percent of the time.

When we find cross-cutting temporal coupling like that, we should investigate the cause. Often, coupled components share a common abstraction, such as a base class that accumulates too many responsibilities. In other cases, we’ll find the opposite problem, and we have a shared abstraction waiting to be discovered. To find it, we want to apply a temporal coupling analysis, as we did back in Chapter 8, Detect Architectural Decay.

Treat Patterns as Helpful Friends

Before we move on, note that these results aren’t presented to show that design patterns don’t work—quite to the contrary. Patterns are context-dependent and do not, by some work of magic, provide universally good designs. You can’t take the human out of the design loop. That said, patterns have a lot to offer:

  • Patterns are a guide. Our architectural principles are likely to evolve together with our system. Remember, problem-solving is an iterative process. Agreeing on the right set of initial principles is challenging, and this is where the knowledge captured in patterns helps.

  • Patterns share knowledge. Patterns come from existing solutions and experiences. Since few designs are really novel, we’ll often find patterns that apply to our new problem as well.

  • Patterns have social value. As the architect Christopher Alexander formalized patterns, the intent was to enable collaborative construction using a shared vocabulary. As such, patterns are more of a communication tool than a technical solution.

  • Patterns are reasoning tools. You learned about chunking back in Names Make the Code Fit Your Head. Patterns are a sophisticated form of chunking. Their names serve as handles to knowledge stored in our long-term memory. Patterns optimize our working memory and guide us as we evolve mental models of the problem and solution space.