Thursday 12 November 2015

Types of errors in C#

Normally without error correction software, if you ever made a mistake when typing your code, even a tiny one, when it's all compiled up and the game is played it just wouldn't work as intended! Potentially, finding and dealing with the problem without any sort of guide as to what's causing it would be a nightmare. This is where debugging tools come in, which help users identify the issues in code once they start to have varying levels of complexity inside with all sorts of variables and methods! First I will now explain, thanks to (Csharp.net-tutorials, 2015), the easiest way of debugging in C#:

Debugging is essential to every programmer, and as such is compulsory for anyone to learn. Print Debugging is a type of Debugging of the most simple form, that even though beginners will take to the most, veterans still use years later. Essentially all it does is make your code print your variables, numbers, text statements, etc when it starts, which helps the user see how far through the code has been run before an error occurred and broke it. The method to use this in Unity/C# is simply 'Console.Write()' and it allows you to post all of your inner-workings of other methods and variables straight to your console window for easy analysis later on!

One more method of debugging is using a Try-Catch Block. A quote from (Msdn.microsoft e, 2015), "The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions." They work really well for looking for catch statements when an exception appears, and if the method that's currently running doesn't have a catch block present, it will go up the hierarchy to find a parent method that does. Eventually if there isn't a catch block, then there will be an error message listing an 'unhandled exception' and the software will cease to function. On the other hand the 'Try' section of of the 'Try-Catch Block' holds the defended code that could start the exception message, and the block will be continually run until an exception appears.

Here's an example from the above source, used for declaring variables with a Try-Catch Block:

static void Main() 
{
    int n;
    try 
    {
        // Do not initialize this variable here.
        n = 123;
    }
    catch
    {
    }
    // Error: Use of unassigned local variable 'n'.
    Console.Write(n);
}

There are various types of errors in Unity, and I will list them now with a description too! (All sourced reliably from the official Microsoft website (Msdn.microsoft d, 2015)) Firstly we have Syntax Errors, which as quoted by Microsoft "Syntax errors are those that appear while you write code.", which means that anything invalid you type down whilst coding will appear in real time. These can even be as small as grammar errors (like not using a capital letter for an instance name, since it could be referring to an different object entirely.) Syntax errors are the most common type, and are completely due to human error and making a mistake. Software like Visual Basic can check your code in real-time and alert you to any errors present.

Next-up we have Run-Time Errors! These only arise once you've started playing a game, and can be considered as a bug or glitch at the Syntax Error system never detected. It's essentially an unexpected occurrence in your game that doesn't function like you coded when a game is booting. There are tonnes of examples, like maybe an enemy not appearing, or the score showing a fault line of text when the game boots up. There's no solve-all fix for this, and involves a more trial and error process of finding the culprit code and amending it accordingly.

Finally there are Logic Errors. These have a clue in the name, but a Logic Error as a problem or glitch in the game as it's being played that the programmer never accounted for, and thus the game behaves unexpectedly to the programmer, even though the code could have been correctly programmed to do the 'wrong' task. These range from the main character being able to jump in the air continuously forever if they hold down the jump key, or maybe when the player finishes the level they end up spawning at another level, rather than the next one in the order they're supposed to be in. Again like run-time errors they're tough to fix, but in this case they're even tougher since they could happen any time in a game under a huge number of possible scenarios, without a clear reason why they're happening (unlike Run-Time Errors which are guaranteed to happen at the start).

There is also white box testing (something I will create for my own game prototype), which involves creating a test-log (inside a chart) for various Scripts and Functions in your code, as you test each game mechanic of your game for bugs and functionality. (An example will be the one I create for my project.)

No comments:

Post a Comment