Local stack quick setup on my Windows 10 machine

Prerequisite:

Steps:

  • Create a file with the name docker-compose.yml.
  • Copy the following content and paste it into a docker-compose.yml.
version: "3.1"
services:
  localstack:
    image: localstack/localstack:latest
    container_name: localstack_demo
    ports:
      - "4563-4599:4563-4599"
      - "8000:8080"
    environment:
      - SERVICES=${SERVICES- }
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "./.localstack:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

  • If there is any problem due to indentation, you can download the original file from here.
  • Open PowerShell from the folder where above docker-compose.yml is kept.
  • Run docker-compose up command.
  • Builds, (re)creates, starts, and attaches to containers for all LocalStack services.
  • Now you can work with Localstack services like S3.


How to make PowerShell shorten the long path of the current directory.

Are you getting less space on the PowerShell due to the long path of the current directory?

You can increase the signal to noise ratio by hiding that long path. How?

Follow the steps:

  • Navigate to PowerShell and type $profile.
  • It will give you a path to the PowerShell profile file.
  • Navigate to the directory of that PowerShell profile file.
    It was “C:\Users\DELL\Documents\WindowsPowerShell” for me.
  • Check if the file “Microsoft.PowerShell_profile.ps1” exists on the disk.
  • If NOT then create one “Microsoft.PowerShell_profile.ps1”.
  • Open “Microsoft.PowerShell_profile.ps1” file.
  • Paste the following code in it and save.
function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}
  • Restart PowerShell.
  • It will show the current folder name only.

Collect human-readable code coverage report for .net core apps

In this post, I will note down steps to collect human-readable code coverage for .net core/standard apps on the developer machine.

Big Picture of Code coverage collection.

I have created a simple .NET standard class library and .net core test project (xUnit) project here.

Project structure: .NET standard class library and xUnit test project


Prerequisites

You will require the following NuGet packages to be installed on the xUnit tests project (BeingCraftsman.CoverageDemo.Tests).

  • Microsoft.NET.Test.Sdk (Preinstalled as a part of xUnit tests project)
  • xunit (Preinstalled as a part of xUnit tests project)
  • xunit.runner.visualstudio (Preinstalled as a part of xUnit tests project)
  • coverlet.msbuild
    Install it using the command:
    dotnet add package coverlet.msbuild
  • ReportGenerator
    Install it using the following two commands:
    dotnet new tool-manifest
    dotnet tool install dotnet-reportgenerator-globaltool


Steps:

  • Open Powershell and navigate to the tests project directory (In my case, BeingCraftsman.CoverageDemo.Tests).
  • Check if the command dotnet test is running all tests.
  • Collect the coverage in a folder (./CodeCoverage in my case) and mention the format opencover.
    WHY opencover?
    ReportGenerator will generate a human-readable report but for that, it requires raw code coverage file in OpenCover format.
    dotnet test /p:CollectCoverage=true /p:CoverletOutput="./CodeCoverage/" /p:CoverletOutputFormat="opencover"
  • Convert opencover format to web pages (Human readable one).
    dotnet reportgenerator "-reports:.\CodeCoverage/coverage.opencover.xml" "-targetdir:.\CodeCoverage\Web"
  • Open generated report
    start .\CodeCoverage\Web\index.htm

Human readable code coverage report giving insights about class, methods etc…


Getting tired of running all these commands one by one.
Next time, onwards, just combine them with “;” separated and execute.

dotnet test /p:CollectCoverage=true /p:CoverletOutput="./CodeCoverage/" /p:CoverletOutputFormat="opencover"; dotnet reportgenerator "-reports:.\CodeCoverage/coverage.opencover.xml" "-targetdir:.\CodeCoverage\Web"; start .\CodeCoverage\Web\index.htm



Brief description of tools/commands:

dotnet test

The dotnet test command is used to execute unit tests in a given solution. The dotnet test command builds the solution and runs a test host application for each test project in the solution.

More details: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test


coverlet.msbuild

Coverlet is Cross platform code coverage for .NET. How it works.

Coverlet also integrates with the build system to run code coverage after tests. In our case, MSBuild is the build system and hence we are using this package. For other build systems, you would require another package.

More details: https://github.com/coverlet-coverage/coverlet


ReportGenerator

ReportGenerator converts coverage reports generated by OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, gcov or lcov into human readable reports in various formats.

More details: https://github.com/danielpalme/ReportGenerator


Consistent C# coding styles in .NET Core and VS 2019

Are you a part of champion C# team who has developers from Different background (JavaScript, Python, Java, Ruby etc.)?

Some of them may prefer their own ways of indentations, placing curly brace locations (JavaScriptish), even not putting them at all (Python-ish).

Curly Brace Conventions by the JavaScript community.
But, C# community prefers that the opening or closing brace within a C# statement, element, or expression should be placed on its own line.


Members from your team have different perspectives over C# coding styles however your team want everyone to follow agreed guideline as if code is written by a single developer and it’s readable?

Are you tired of seeing/receiving linting comments in the C# code reviews?

Multiple violations: Space required after if and closing parenthesis, Extra parenthesis


Do you want to automatically find and fix those as a part of C# code self-reviews?

Omission of curly brace introduced a serious bug. return Buzz will gets always executed.

remove unused variables, remove unused methods, remove unused namespaces and MANY MORE.


Answer to these questions are combination of tools:

  • .EditorConfig
  • StyleCop

If you have a single or multiple project make sure you create a .editorconfig at root level (Solution level) so that all the rules are applicable to all the projects under a solution.

Steps:

  • Open your project solution in the visual studio 2019.
  • Right click on your solution file

A solution with multiple projects (For you it could be 10 or 100 projects)


  • Click on New EditorConfig. It will create a file with pre-configured rules.


  • Install StyleCopAnalyzers using the NuGet command line or the NuGet Package Manager in Visual Studio 2019 for all the projects.


  • Right click on solution and Click build. Now it will start showing fixable things as warnings in your IDE’s “Error List” window.
Error List window in the visual studio showing fixable warnings.

Raise the bar


You can even push yourself and set few rules as errors in the .editorconfigs

Option from IDE to set some guideline as Error and other options.
Set obvious suggestions as an error and some as none to reduce the noise.


Do you want to try it before applying it to your project?

Here is the git repo link:

https://github.com/way2datta/.net-practices/tree/master/Linters




Principles – Quick notes

Robustness principle – Postel’s law, after Jon Postel

Programs that send messages to other machines (or to other programs on the same machine) should conform completely to the specifications, but programs that receive messages should accept non-conformant input as long as the meaning is clear.


Rule of least power

In programming, the rule of least power is a design principle that “suggests choosing the least powerful [computer] language suitable for a given purpose”.


Principle of least astonishment

The principle of least astonishment (POLA), also called the principle of least surprise (alternatively a “law” or “rule”)
applies to user interface and software design.
“If a necessary feature has a high astonishment factor, it may be necessary to redesign the feature.”

When two elements of an interface conflict, or are ambiguous, the behavior should be that which will least surprise the user; in particular a programmer should try to think of the behavior that will least surprise someone who uses the program.


Pareto principle

The Pareto principle (also known as the 80/20 rule, the law of the vital few, or the principle of factor sparsity).
It states that, for many events, roughly 80% of the effects come from 20% of the causes.

It was also discovered that in general the 80% of a certain piece of software can be written in 20% of the total allocated time. Conversely, the hardest 20% of the code takes 80% of the time.


Keep it simple, stupid (keep it simple and straightforward)

The KISS principle states that most systems work best if they are kept simple rather than made complicated; therefore, simplicity should be a key goal in design, and unnecessary complexity should be avoided. 


Don’t repeat yourself 

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Don’t repeat yourself (DRY, or sometimes do not repeat yourself) is a principle of software development aimed at reducing repetition of software patterns, replacing it with abstractions or using data normalization to avoid redundancy.


Check the values of all data from external sources

When getting data from a file

  • Verify that if it has valid extensions/file type
  • Verify that it does not exceeds certain file size

When getting data from a a user, the network, or some other external interface

  • Check to be sure that the data falls within the allowable range.
  • Make sure that numeric values are within tolerances
  • Strings are short enough to handle.
  • If a string is intended to represent a restricted range of values.
  • Be sure that the string is valid for its intended purpose;

Otherwise reject it.

Be symmetric while writing methods

When working with class or module sometimes you would need equal and opposite operations. The methods should be symmetric. Please make sure they are not added causally that would add non symmetric verbs.

Not symmetric

  • turnOn()/disable()
  • openConnection()/shutConnection()
  • increaseVolume()/turndownVolume()
  • push/remove

If you are adding symmetric methods, then make sure they feel symmetric one.

Symmetric

  • turnOn()/turnOff()
  • openConnection()/closeConnection()
  • readFile()/writeFile()
  • increaseVolume()/decreaseVolume()
  • activateAlarm()/deactivateAlarm()
  • push()/pop()


Functions in JavaScript are objects

Functions in JavaScript are objects.

How to know in the browser?

Navigate to the console of the browser near you and type this simple function Person shown below and hit enter. Next, type Person followed by dot (.) see the methods on Person function. Pretty simple.

Thus the function is an object and has the methods as other objects in JavaScript.

We can call functions on function. Let’s invoke call function and see the output.

Invoking call method on function Person.

Prototype object:

Objects are keyed collection/associative array that has a hidden link to the prototype object. Try the following code in any browser console and understand it.

Object and prototype object has a linking.

We can access the prototype object using Object.prototype.

We can get an original object back from the prototype object using constructor (Object.prototype.constructor) property.

Pictorial representation of Object and it’s prototype object.

Oh, wait! We were talking about Function.

Like Object, Function is linked to Function.prototype.

Let me show you using Person function, it’s prototype object and their linking in the browser console.

Pictorial representation of Person function and it’s prototype object.