Skip to content

Exercises related to Robust Development Methodologies - Part 2

Cyclomatic Complexity

Exercice 1

What is the cyclomatic complexity of a switch case?

   void a_case (){
     char operation = '';

     switch (operation) {
        case "+":
           printf("operation: %c\n");
           break;
        case "-":
           printf("operation: %c\n");
           break;
        case "=":
           printf("operation: %c\n");
           break;
        default:
           printf("operation: none\n");
           break;
     } 
   }
Solution

It is close to that of a switch with the difference that there are many (3 in this case) decision points. As a result, \(CC= 4\)

It is worth noting that the default case in the switch statement will not create a new independent path, as it will only be executed if none of the other case statements is executed. In other words, the default case is not an independent path of execution, but rather a fallback option that is only executed if the other paths fail.

Exercice 2

Given the function below, calculate the cyclomatic complexity

   void foo1 () {
     int array[] = {1, 7, 4, 9, 12, 7, -1, 13};
     const int kArraySize = 8;
     int min = array[0];
     for(int i = 0; i < kArraySize; i++){
       if( array[i] < min){
          min = array[i];
       }
      }
     printf("Array minimum = %d\n", min);
   }
Solution

\(CC= E - N + 2*P\)
The cyclomatic complexity is thus 3.

  • e:the number of edges in the control graph = 11
  • n: the number of nodes in the control graph = 10
  • p: the number of connected components in the control graph = 1
  • (d: the number of decisions in the control graph = 2)

Note

Did you know that Scitool Understand will generate a control flow diagram from software? Here a simple example:

Critical: How to Write git Commit Messages

Exercice 3

Read the article Write Better Commit Messages and the respond to the following questions:

  1. What is the structure of a commit message?
  2. What types of commits are there?
  3. What does one put in the footer section usually? What is its purpose?
  4. What are the git commit best practices?
Solution
  1. It is:

       type(scope): subject
       <body>
       <footer>
    

  2. They are:

      feat:     The new feature being added to a particular application
      fix:      A bug fix (this correlates with PATCH in SemVer)
      style:    Feature and updates related to styling
      refactor: Refactoring a specific section of the codebase
      test:     Everything related to testing
      docs:     Everything related to documentation
      chore:    Regular code maintenance
    
    Very valuable examples to be found under Conventional Commits.

  3. References are added to this section (footer). This serves to link the commit to its origin (e.g. issue ID).

  4. They are:

    1. Make Small, Single-Purpose Commits
      For proper source code management and better housekeeping a commit should be a wrapper for related changes. For example, fixing two different bugs should produce two separate commits. Small commits make it easier for other developers to a) understand the changes b) roll them back if something went wrong.

    2. Commit Early & Often
      Committing early and often keeps your commits small and helps you commit only related changes. Moreover, it allows you to share your code more frequently with others and avoid having merge conflicts.

    3. Test Your Code Before You Commit
      Resist the temptation to commit something that you «think» is completed. Test it thoroughly to make sure it really is completed and has no side effects (as far as one can tell).

    4. Agree on A Workflow
      Git lets you pick from a lot of different workflows: long-running branches, feature branches, merge or rebase, git-flow, etc. Which one you choose depends on a couple of factors: your project, your overall development and deployment workflows, etc. However you choose to work, just make sure to agree on a common workflow that everyone follows.