Welcome to gamedevs

Week 8

This week we did some metrics on our code. Metrics are used to give better insight into how good your code is. Otherwise we would refactor all day while stepping in the dark without knowing in what direction to put our efforts into. Conveniently, Visual Studio already offers code metrics out of the box.

It looks something like this:

Code Metrics Example

As you can see it will show metrics for namespaces, classes and methods. The metrics that it can show are:

  • Maintainablility Index
  • Cyclomatic Complexity
  • Depth of Inheritance
  • Class Coupling
  • Lines of Code

We decided to use Class Coupling and Depth of Inheritance. Class Coupling shows how many classes a class uses and therefore how coupled it is with those classes. Generally, you want to keep the amount of Couplings low to avoid changes in classes affecting too many other classes. Depth of Inheritance shows how long the chain of inheritance is. The goal is to avoid long inheritance chains where a change in the upper hiarchie will ripple down to the inheritors.

Code Metrics Example

As you can see from the image, there tends to be low Class Coupling except on specific classes where it’s hard to avoid. In contrast to that, Depth of Inheritance seems rather high at first, but that’s because Unity offers premade classes that already have a depth of 3. Therefore a 4 is a 1 for us. Big Mathz.

Overall, we can say that metrics are a very reasuring tool to make sure you move in the right direction.