Wednesday, 12 December 2018

Capturing exceptions with Nunit in .NET

In this tutorial we are going to capture exception that service throw using nUnit test framework. It is quite important writing unit tests or integration tests to be able to specify scenario that will force method to threw an exception so we are prepared for that eventuality.

We are having fruit service that stores info regarding fruit availability in the fridge. 



public class FruitService
  {
    public async Task CheckIfFruitIsInTheFridge(string fruit)
    {
      // To keep it simple call Fridge method. In real environment we would call
      // some external api and we want to create new thread for that.

      var fruitInTheFridge = Fridge(fruit);

      if(fruitInTheFridge == null)
      {
        throw new Exception("Could not find fruit in the fridge."); 
      }

      return await fruitInTheFridge;
    }

    private async Task<string> Fridge(string fruit)
    {
      // Ok beer is not part of the fruit company, but beer must always be in the fridge :)

      List currentfruitsInTheFridge = new List<string> { "Apple", "Pearl", "Beer" };

      return currentfruitsInTheFridge.FirstOrDefault(m => m.Equals(fruit));
    }
  }

Now we are going to run tests.

[TestFixture]
  public class WhenCallingCheckIfFruitIsInTheFridge
  {
    private FruitService _fruitService;

    [SetUp]
    public void SetUp()
    {
      this._fruitService = new FruitService();
    }

    [Test]
    public void ItThrowsAnExceptionWhenfruitIsNotFoundInTheFridge()
    {
      var ex = Assert.ThrowsAsync<exception>(async () => await this._fruitService.CheckIfFruitIsInTheFridge("Grapes"));

      Assert.That(ex.Message, Is.EqualTo("Could not find fruit in the fridge."));
    }
  }

Wednesday, 21 November 2018

Prefix and Postfix in .NET explained in 1 minute

Using prefix in comparison will change the value before expression evaluation, where using postfix will change value after expression evaluation. Look at below example.

public static void Main()
 {
  // Prefix and Postfix example
  
  int x = 5;
  if(++x == 6)
  {
   Console.WriteLine("Using prefix will increment value before expression is evaluated.");
  }
  
  Console.WriteLine(string.Format("Value of x is {0}", x));
  if(x++ == 7)
  {
   // we will never reach this code as expression is evaluated before increment, hence x is still equal to 6
   Console.WriteLine("I am here but you will never see me in console.");
  }
  
  Console.WriteLine(string.Format("Value of x is {0}", x));
 }

You can look into example in dotnetfiddle - https://dotnetfiddle.net/txxcEa

Wednesday, 14 November 2018

Setting up environment for Angular 6

1. Check if you have npm installed by opening command and type npm -v. If you have not installed npm download it from - https://nodejs.org/en/
2. Install @angular-cli. Type npm install -g @angular/cli. -g parameter means that package will be installed globally so you can access angular cli functionality from any location.
Visit -  https://cli.angular.io/ for more info. This will enable you to create new angular project with some basic data that follows good practices.
3.  Next create new angular project. Type ng new ng-project where ng-project is name of new project you want to create. Make sure that in command line you are in folder where you want this project to appear. You can also use optional parameters like --routing or --style=scss (will use scss by default instead of css)
4. Now you open source code by typing code  (make sure you type .) in command line if you have Visual Studio Code installed (https://code.visualstudio.com/) - highly recommended
5. To run app you just create type ng -serve -o to initiate localhost server and open app in a browser. If you see error message "The serve command requires to be run in an Angular project, but a project definition could not be found" make sure you are in your angular project main folder. 


Monday, 29 October 2018

IndexOf keyword in .NET

IndexOf is an extension method (default functionality can be overridden) on string.
Method will return index number of a character position in string.

Take a look in this example

public static void Main()
 {
  var character = "ABS";
  Console.WriteLine(character.IndexOf("B"));
 }

We are expecting to see 1 in Console.

If character does not exist in the string method will return -1.


public static void Main()
 {
  var character = "ABS";
  Console.WriteLine(character.IndexOf("F"));
 }

Above method will return -1.


Tuesday, 16 October 2018

Understanding versioning in npm and bower

If you ever got confused what is the difference between 1.1.2 and 1.1.x, or ^1.1.2 and ~1.1.2 numbers next to your npm package name keep reading this post.

When you have for instance "angular": "~1.6.1" defined in your package.json and version 1.6.2 is available on the server it will pull this version, however if version 1.7.1 is available it will ignore it.

In order to pull 1.7.1 you can change ~ to ^ so "angular": "^1.6.1"
For major update it is recommended to manually update version as major version usually means breaking changes.

~ - patch update
^ - minor update

You can find all available packages and version https://semver.npmjs.com/








Saturday, 24 February 2018

Reference for converting GLSL to HLSL - Unity 3d ShaderToy


  • Replace iGlobalTime shader input (“shader playback time in seconds”) with _Time.y
  • Replace iResolution.xy (“viewport resolution in pixels”) with _ScreenParams.xy
  • Replace vec2 types with float2mat2 with float2x2 etc.
  • Replace vec3(1) shortcut constructors in which all elements have same value with explicit float3(1,1,1)
  • Replace Texture2D with Tex2D
  • Replace atan(x,y) with atan2(y,x) <- Note parameter ordering!
  • Replace mix() with lerp()
  • Replace *= with mul()
  • Replace signed integer vector ive2, ivec3, ivec4 with int2, int3,int4
  • Replace iTime with _Time
  • Remove third (bias) parameter from Texture2D lookups
  • mainImage(out vec4 fragColor, in vec2 fragCoord) is the fragment shader function, equivalent to float4 mainImage(float2 fragCoord : SV_POSITION) : SV_Target
  • UV coordinates in GLSL have 0 at the top and increase downwards, in HLSL 0 is at the bottom and increases upwards, so you may need to use uv.y = 1 – uv.y at some point.
  • Replace fract with frac
  • Replace function dFdx with ddx
  • Replace function dfdy with ddy
  • fragCoord in GLSL
    Default Unity shader naming :

    HLSL - Shader Lab in Unity
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};

In frag method:

fixed4 frag (v2f i) : SV_Target

{

   // GLSL - fragCoord, fragCoord.xy = i.vertex.xy

    i.vertex.position 

}

In GLSL we would have method:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{

}



  • Replace mod with fmod





I took some information from https://alastaira.wordpress.com/2015/08/07/unity-shadertoys-a-k-a-converting-glsl-shaders-to-cghlsl/

Wednesday, 14 February 2018

Principles for fast and good game development


Above video is a John Romero's presentation in WeAreDevelopers 2017. Just in case you are not sure who is John Romero, he was one of the main creators behind FPS legends such as Doom, Wolfenstein 3D, Quake. 

I personally think is the easiest way to improve skills in any field is to listen the experts. I wasted too much time in the past trying to figure out too many things on my own. I summarize principles that John Romero's team was following. There is not a paradigm in those, however it is good to print them and place them on your office wall as a good reminder to your team.


  1. Polish game as you go
  2. Always maintain shippable code
  3. No prototypes for small game/team
  4. Game can always be run by your team
  5. Have defaults for your core methods, so when someone override it with faulty code other team members won't be blocked by it.
  6. Keep code super simple, refactor as you go
  7. Test properly your code before you commit it, so testers do not have to waste too much time creating bug reports etc.
  8. Write code specifically for the game you are working on, do not try to make your code ready for future games as in the future you will be writing better code as you become more experienced
  9. Run your code after writing few lines of code so you can quickly detect problem if occur 
  10. Write the code is it is very easy to understand by other developers


My personal favourite that is related to point 10

Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.