Writing, Debugging, and Testing Functions

C# - Writing, Debugging, and Testing Functions , ANGULAR WITH CORE .NET AND MVC RAZOR ENGINE

Introduction

This chapter is about writing functions to reuse code, debugging logic errors during
development, logging exceptions during runtime, and unit testing your code to
remove bugs and ensure stability and reliability.

This chapter covers the following topics:

  • Writing functions
  • Debugging during development
  • Logging during runtime
  • Unit testing

Writing functions

A fundamental principle of programming is Don't Repeat Yourself (DRY).
While programming, if you find yourself writing the same statements over and over
again, then turn those statements into a function. Functions are like tiny programs
that complete one small task. For example, you might write a function to calculate
sales tax and then reuse that function in many places in a financial application.
Like programs, functions usually have inputs and outputs. They are sometimes
described as black boxes, where you feed some raw materials in one end and
a manufactured item emerges at the other. Once created, you don't need to think
about how they work.
You previously learned about the for statement earlier in this book, so you know
that for can be used to generate repeated lines of output when there is a regular
pattern, like the 12 times table, as shown in the following code:
            for (int row = 0; row <= 12; row++)
            {
                Console.WriteLine($"{row} * {12} = {row * 12}");
            }
            Console.ReadLine();
            /*Output
             * 0 * 12 = 0
                1 * 12 = 12
                2 * 12 = 24
                3 * 12 = 36
                4 * 12 = 48
                5 * 12 = 60
                6 * 12 = 72
                7 * 12 = 84
                8 * 12 = 96
                9 * 12 = 108
                10 * 12 = 120
                11 * 12 = 132
                12 * 12 = 144
             * 
             * */
However, instead of outputting the 12 times table, we want to make this more
flexible, so it could output the times table for any number. We can do this by creating
a function.
using System;
namespace Day_3
{
    public class Program
    {
        public void Table(int number)
        {
            for (int row = 1; row <= 12; row++)
            {
                Console.WriteLine($"{row} * {number} = {row * number}");
            }
        }
        public static void Main(string[] args)
        {
            Program program = new Program();
            program.Table(3); 
            Console.ReadLine();
            /* Ouput
             *  1 * 3 = 3
                2 * 3 = 6
                3 * 3 = 9
                4 * 3 = 12
                5 * 3 = 15
                6 * 3 = 18
                7 * 3 = 21
                8 * 3 = 24
                9 * 3 = 27
                10 * 3 = 30
                11 * 3 = 33
                12 * 3 = 36
             */
        }
    }
}

Writing functions that return a value


The previous function performed actions (looping and writing to the console), but it

did not return a value. Let's say you need to calculate some values based on  input and action 


 public class Program
    {
        public decimal calculate(int number1, int number2)
        {
            return number1 + number2;
        }

        public static void Main(string[] args)
        {
            Program program = new Program();
            decimal result = program.calculate(65, 10);             
            Console.WriteLine(result);
            // Output
            // 75
        }

    }


Calculating factorial with recursion


The factorial of 5 is 120, because factorials are calculated by multiplying the starting
number by one less than itself, and then by one less again, and so on, until the
number is reduced to 1. An example can be seen in: 5 x 4 x 3 x 2 x 1 = 120.
We will write a function named Factorial; this will calculate the factorial for an
int passed to it as a parameter. We will use a clever technique called recursion,
which means a function that calls itself within its implementation, either directly
or indirectly.
 public class Program
    {
        public void factorial(int factorialNumber)
        {
            int i,  fact;            
            fact = factorialNumber;
            for (i = factorialNumber - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine(fact);
        }
        public static void Main(string[] args)
        {
            Program program = new Program();
            program.factorial(6);
            Console.ReadLine();
             
        }
    }

Documenting functions with XML comments

By default, when calling a function like factorial, Visual Studio Code
will show a tooltip with basic information, as shown in the following screenshot:
type three forward
slashes, and note the extension expands this into an XML comment and
recognizes that it has a single parameter named number.

Debugging during Development

In this section, you will learn how to debug problems at development time.

Setting a breakpoint


Breakpoints allow us to mark a line of code that we want to pause at to inspect the program state and find bugs.

  • You can press F9 to set a breakpoint


Stepping through code

After setting a debugger you can use F11 & F10 for stepin and stepout.


Comments

Post a Comment

Popular posts from this blog

How to disable table row in Angular

How to create XML file dynamically in c#.net?

How to get Array value in Angular ?