Archive for the ‘Code and Development’ Category

Experimenting with setting up a new machine every third month

April 3rd, 2019

I am currently experimenting with using a virtual machine for my development. I have earlier done it through OSX/Win by the means of Parallells but now I am trying a Win/Win solution with Hyper-V.

Every 30 days or third month, I have seen both, is for how long the license is valid, I do a Hyper-V Manager->Quick create->Windows 10 dev environment and 30 minutes later, or so, the new VM is up and running.

Microsoft packages and delivers the new download on the very day of expiry so it is not possible to download a VM a day before and prep it.
It means the VM is out of date the moment you have installed the new one.

But there is a work around, open a console in admin and

1
slmgr –rearm
and you have 90(?) new days. This can only be done once per VM.

Also note that the Hyper-V manager gui, as depicted below, does not refresh its “Create virtual-machine”-content so the date of the template might be old. Restart (the host machine?) to refresh it.

The mandatory screen shot.

It comes with Visual studio, Visual studio code, Powershell 6 and some dotnet preinstalled. All I have to do is start VS and tell it to update itself..

Then I need Git, Notepad++, VSCode, Posh-git Keyboard1337 and BecerroInamovible. Ergo I need Chocolatey too.

Here is how I do it, the explained way

Fire up a console with elevated priveges.

Window-button, cmd, ctrl-shift-enter. Enter.


1
Window-button, cmd, ctrl-shift-enter. Enter.

Install Chocolatey.


1
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

Restart console.

I have checked that all chocolatey packages I reference are “trusted package”. I cannot know if they packages remain “trusted package” at the time you are reading this. The choice is yours.


1
2
Exit. Enter.
Window-button, cmd, ctrl-shift-enter. Enter.

Install Git.


1
choco install git.install

1
winget install --id Git.Git -e --source winget

Install Notepad++.


1
choco install notepadplusplus.install

Install Posh-Git


1
2
PowerShellGet\Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force
Add-PoshGitToProfile -AllHosts

To make it even cooler one can install more stuff, like oh-my-posh. For it to work one needs a new font “Cascadia Code”, and Windows terminal has to be updated to use it.

1
"fontFace":  "Cascadia Code PL"

1
Import-Module oh-my-posh

1
Set-ThemeParadox

I like to have a clear separator by every promp. So I might add a newline before the prompt


1
$GitPromptSettings.DefaultPromptBeforeSuffix.Text = '`n'

Install Powershell core


1
choco install powershell-core

or


1
winget install --name PowerShell --exact

Install Keyboard1337.

I believe Keyboard1337 is a zip if you git clone BecarroInamovible. If so, you don’t have to download Keyboard1337.

Otherwise download

1
Keyboad 1337.zip
. Unblock. Unpack. Install.
Run Keyboard 1337\l337\setup.exe (yupp. there’s a typo there)
Language preferences -> Keep English(Sweden) and English(United States). The latter is Keyboard1337

BecerroInamovible

It has a dependency on Autohotkey.


1
choco install autohotkey

Copy contents of

1
https://raw.githubusercontent.com/LosManos/keyboard1337/master/BecerroInamovible.ahk
to
1
C:\DATA\PROJEKT\Keyboard1337

As an alternative do ”

1
git clone https://github.com/LosManos/keyboard1337.git

Selfelected1337

Not mentioned above, and not, yet, public I use the world’s first non-fixed programmer font called Selfelected1337.

Get it and install it.

Vscode

To make it work in VSCode. Open settings.json (ctrl-shift-p)settings.json

Update it with

“editor.fontFamily”: “Selfelected1337, Consolas, ‘Courier New’, monospace”,

To make the active tab stand out. Update settings.json with

“workbench.colorCustomizations”: {        “tab.activeBackground”: “#00aaff”    }

As short as possible:

@”%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe” -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command “iex ((New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))” && SET “PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin”

choco install git.install -y & choco install notepadplusplus.install -y & choco install autohotkey -yt
choco install kubernetes-cli
winget install Microsoft.PowerShell
winget install “visual studio code”
winget install -e –id Microsoft.AzureCLI

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Start a powershell terminal:

PowerShellGet\Install-Module posh-git -Scope CurrentUser ; Add-PoshGitToProfile -AllHosts

notepad $profile

And in the resulting file input

$GitPromptSettings.DefaultPromptPrefix.Text = ‘*** $(Get-Date -f “yyyy-MM-dd HH:mm:ss”) `n’

Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete

In a powershell terminal:

mkdir c:\DATA ; mkdir c:\DATA\PROJEKT ; pushd c:\DATA\PROJEKT ; git clone https://github.com/LosManos/keyboard1337.git ;

& ‘C:\Program Files\AutoHotkey\AutoHotkeyU64.exe’ C:\DATA\PROJEKT\keyboard1337\BecerroInamovible.ahk ;

Expand-Archive ‘C:\DATA\PROJEKT\keyboard1337\Keyboard 1337.zip’ -DestinationPath C:\DATA\PROJEKT\keyboard1337\ ; C:\DATA\PROJEKT\keyboard1337\l337\setup.exe

The three reasons to use an O/RM

December 20th, 2018
  1. Type security
  2. Data copying Entity/Recordset
  3. Faster turn around with a moving schema

If these 3 issues aren’t a problem in you case, be reluctant to use an O/RM as an O/RM brings a bunch of other problems.

Type security

When you get the Customer.Verified out of a database the code already knows it is a boolean. No explicit type casting or declaring is needed as it is done elswhere, once and for all.

Now this isn’t totally true but depends on whether LINQ or HQL (or for another O/RM) you are using. But basically it is true, because of solid Right-left copying

Right-left data copying

Copying data from a recordset to the Entity is done by the OR/M and configured once and for all. Unlike a programmer it won’t forget to copy a field in an obscure corner in your code base.

It isn’t hard to write a data copying function for this simple case and dragging a whole OR/M into the project is a lot of dependencies for little use.

Turn around

When the scheme is changing fast, like in the beginning of a project, or when prototyping, an O/RM is of great help to change in one place and let the coding tools and compiler do the rest of the changes in the project.

Schemes tend to solidify after a while and only change in small increments so speeding up the first 3 months for having a rucksack the coming 6 years might not be the best solution.

Powershell special characters and tokens

August 22nd, 2018

It is hard to web search for Powershell’s interpretation of $?, |, –% or whatnot but Neolisk has created a nice list.

Read here: http://www.neolisk.com/techblog/powershell-specialcharactersandtokens

Classes

Concise: https://xainey.github.io/2016/powershell-classes-and-concepts/

Watch Your code become intermediary C# before becoming CLR

August 5th, 2018

Go to https://sharplab.io and start typing away in the left screen. The internediary code or IL is presented in the right.

The code you write can be in C#8, F# or VBnet. The output can be the input (try `using` to see how it is just a shorthand for `try…catch`).

The output is C#, F# and Vbnet; but you can also choose IL, JITAsm, the syntax tree and some more.

Use it to get a better understanding of what is produced by your code.

It is all open source https://github.com/ashmind/SharpLabhttps://github.com/ashmind/SharpLab.

Why I try to not use the debugger

June 25th, 2018

Readable logs

In production we usually rely on logs to track down an error.
Even worse; in production we have more data and more logging to sift through than in development. The trees can be lost in the woods. If we are more unlucky we also have a time restraint.

To learn how to read a log file we must train. This means reading log files. And to read the we must first write to them; in a readable way.

So I write log files and read them to find my errors already in development. Training for production.

Learn to write

Too little information is bad. This is the usual case. Too much information is bad too as we cannot see the flow of the program due to uninteresting information. Writing the exact amount of information for every time is probably not doable but by training I learn and get better at it.

Readable code

If we develop the application through setting breakpoints and inspecting variables we are in a bad spot when production error happens as we seldom can do that.
All we have is a log with a stack and possibly a rough idea of the input.

If I instead learn to read the code and trace the execution path manually, already in the development phase, I am in a much better position when production hits the proverbial fan.

Startup time

Connecting the debugger takes time. Just starting the program is faster.

I only connect the debugger when I have to.

Powershell pitfalls

June 21st, 2018
1
2
3
function T ($b){Write-Host "[$b]"; return $b}

if( T($true) -and T($true)){1}else{0}

I would expect the above to return

1
2
3
[True]
[True]
1

but no. Instead

1
2
[True]
1

is returned.

Why?

It’s because a method call in an if statement has to be surrounded with paranthesises like so:

1
if( ( T($true)) -and (T($true)) ){1}else{0}

Now the output is

1
2
3
[True]
[True]
1

 

Powershell for devs, part 2

April 28th, 2018

Continuing from Part1.

New to Powershell? Let me jot down a few good-to-know things. Some you already know. Some might save you hours of googling.

Below is a simple module. I will explain it row(s) by row(s).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Set-StrictMode -Version 2.0

< #
.SYNOPSIS
Short description.

.DESCRIPTION
Long description.

.PARAMETER name
This does not show with Get-Help.

.PARAMETER birthDate
This does not show with Get-Help.

.EXAMPLE
An example...

.NOTES
General notes.
#>
function Get-PersonData
{
    param(
        [parameter(
            Mandatory=$true,
            HelpMessage='The name of the culprit.'
        )]
        [string] $name,
        [datetime] $birthDate = (Get-Date)
    )
    $startTime = Get-Date -Format 't'
    Write-Verbose "Start:$startTime"
   
    "Name=$name, Date=$($birthDate.ToString('yyyyMMdd'))."

    # Call method | Filter | Sort | Return.
    $foundPerson = GetPeople `
        | where {$_.name -eq $name } `
        | Sort-Object born `
        | Select-Object -First 1

    Write-Host "foundPerson:[$foundPerson]"
   
    Write-Verbose "Stop:$(Get-Date -Format 't')"

    return $foundPerson
}

function GetPeople(){
    # Create array of key-value pairs.
    $people =
        @{name='ola';born=[DateTime]'1970-10-13';children=1},
        @{name='anders';born=[DateTime]'2011-01-01'}

    return $people
}

Export-ModuleMember Get-PersonData
# Export-ModuleMember GetPeople
1
Set-StrictMode

Use Set-StrictMode. See part1 of this blog series.

1
# .SYNOPSIS... #

This type of comment right before a method is recognised by Powershell and ends up in Get-Help.  Adhering to explaining the intention for your methods is considered good practice.

Inside the method is

1
2
3
4
5
6
7
8
param(
    [parameter(
        Mandatory=$true,
        HelpMessage='The name of the culprit.'
    )]
    [string]$name,
    [datetime]$birthDate=(Get-Date)
)

This is what the parameters look like. One can set if a parameter is mandatory, some help text to be picked up by your favourite text editor, the [type] and default value.

1
$startTime=Get-Date-Format 't'

A variable is set to a string. Other scripting languages send strings around. Powershell sends proper objects. The Get-Date-Format converts the DateTime value to a string.

1
Write-Verbose "Start$startTime"

Built into Powershell is the possibility to call (almost) anything with a -Verbose flag. Only then is the Write-Verbose called. Like a simple logging level.

1
"Name=$name, Date=$($birthDate.ToString('yyyyMMdd'))."

Nothing strange here at first sight. Until you exeute in a console. Then you realise this string is outputed; because it is not inputed into something else, like a variable.

Also “$($variable.Method)” is the way to call methods inside a string.

1
2
3
4
$foundPerson= GetPeople `
    | where {$_.name-eq$name } `
    | Sort-Object born `
    | Select-Object-First 1

Powershell is said to be able to use Linq. This is technically true but the syntax is so weird that I have never used it. This code though has (almost) the same behaviour and is easy to read.

GetPeople is a method call. Backtick concatenate lines and circumvents that Powershell has automatic statement ending with a line end. | pipes object and not strings. where is an alias for Where-Object. The rest is… Linqish.

1
Write-Host "foundPerson:[$foundPerson]"

Write-Host an object like $foundPerson output the contents of the object. Not just the type as in C#.
Write-Verbose “Stop:$(Get-Date-Format ‘t’)”
This string is outputed only if the method call is made with -Verbose. Plus an exmple on how to write a method call in a string.

1
return $foundPerson

Finally nothing surprising. Except that return can be left out to make the code harder to read.

1
2
3
$people=
@{name='ola';born=[DateTime]'1970-10-13';children=1},
@{name='anders';born=[DateTime]'2011-01-01'}

@ tells Powershell to create a list of key-value pairs. Also often called a hash list.

Note the comma character. That makes $people an array of key-value pairs.

1
Export-ModuleMember Get-PersonData

Only exported mehods are visible outside the module. It is like making them public.

Powershell for devs, part 1

April 28th, 2018

New to Powershell? Let me jot down a few good-to-know things. Some you already know. Some might save you hours of googling.

Use Set-StrictMode -Version 2.0.

Powershell 5.1 is the last Windows Powershell. From 6 it runs on Dotnet core and more platforms.

Use Pester for automatic testing. It runs tests and can mock. Note that mocking works differently in Powershell than C# as they load code in different ways.

Don’t patch together your Powershell scripts. Use the systems thinking you usally do and create a sturdy, thought out, solution with bricks of the right size. Just like you would any other solution.

For caveats let me explain the file below, row by row.

File JustASimpleScript.ps1

1
Set-StrictMode -Version 2.0

$temp

$temp = ‘LocalVariable’

function LocalFunction( $foo, $bar ){
Write-Host “Local variable is $temp.”
$foo    # The last executed row is returned.
}

LocalFunction ‘MyParameter’

Enter the above in your favourite text editor. Save it as JustASimpleScript.ps1. Open a console and navigate to the proper folder. Execute Powershell to get Powershell started. Then execute .\JustASimpleScript.ps1′. The result shows both an exception and some more proper output.

1
Set-StrictMode -Version 2.0

Set-StrictMode is like `option explicit` in old VB6, it throws an error if you try to evaluate a variable that has not been set, that parenthesises are not used when calling functions and called methods do exist.
No code example at Stackoverflow shows it and almost no blog article.

1
$temp

Variables are recognised by the leading dollar sign.

As we used Set-StrictMode above this row throws an exception. But… the program continues to run!

1
$temp = 'LocalVariable'

Strings are delimited with apostrophes. Quotation works but is over kill, see below.

1
function LocalFunction( $foo, $bar ){

Nothing special about declaring a function like this. Declare a function with parenthesises but call it without; it is very easy to get this wrong. Also; the parameters are optional by default.

1
Write-Host "Local variable is $temp."

Write-Host is the normal way of outputting text in the console. If it is the correct way is another discussion I won’t dive into without more knowledge.

Also note the quotation marks. They mean that anything that looks like a variable inside should be evaluated. Just like PHP. Just like C# by prefixing a string with $. Many online examples use quotation marks for every string. By the time of writing I consider that less good.

1
$foo    # The last executed row is returned.

To fool newbies, Powershell implicitly returns the last executed row with an output. I suggest to make it more readable, prefix with return like so:

1
return $foo    # The last executed row is returned.

Comments starts with a # sign.

1
{...}

Stuff within curly brackets are a code block. A code block is not only the contents of a method or an if statement but can also be passed around, like a() => {…} lambda in C#.

1
LocalFunction 'MyParameter'

This ia a method call. As long as it is Powershell leave out the parenthesises. Otherwise you have converted your list of parameters to a single parameter call and the single parameters is a list. This rule is not hard to remember but reading code that calls method with lists as parameters is hard to grasp for a newbie. Adding insult to injury, calling a C# method from Powershell might change the rule.

Using a file like this, ending in ps1 is typically done by “dot sourcing”. It is quick, dirty and pollutes the global name space. Things you would never to in your “regular language”.

A call like below makes the contents live only just in the call.

1
.\JustASimpleScript.ps1

If you want to make the $temp variable and the LocalFunction live on you start it with yet a period and a space like so:

1
. .\JustASimpleScript.ps1

But probably you want to make a module instead. Which you find in part 2.

Finally, don’t forget:

  • Think System.
  • Use automatic tests.
  • Use Set-StrictMode -Version 2.0.

When you are testing, you are not testing code; you are testing intention

April 4th, 2018

I just had to get that off my chest.

How I tested *every* authorisation, authorised or not

March 10th, 2018

Well… I didn’t, not down to bit level. But I got closer than any time before as every business case was tested.

Don’t believe my rudimentary text below will simply answer how to do it; it will just give a raw landscape. It took me several nights and 3 iterations before I solved all the tidbits.

More important than to test that every Role could get to its authorised Product was to see that it could not get to unauthorised ditto.
So I had to test every combination.

Testing every permutation of User, Role and Product was not  feasible. Each entity has several properties and they are, mostly, not related to authorisation. Then we have 2^64 kinds of userId where most of them are not interesting and not even in use. To continue a test on “Role of type X or Y” is really a “Role is HiredInProductsCompany”.
So I sat down and extracted the if statements from the code. They were like “if loggedOn” and “if in Role x or y”. Every such statement was extracted as a method and moved into a (temporary) helper lib.

When I was sure, with visual inspection, I had caught everything, I put some business logic into thought and manipulated and rearranged the methods. They became fewer and matchable to business requirements. Gone was “if user.Role == Administrator and user.Company = product.Company” but instead “if user.IsAdministratorAtProductsCompany(product)”.

Note that during this process I have not changed any logic and, testing besides, present state could be shipped all the time.

Now I had to get rid of any technical remains. On the outside it looked ok as the method names where very descriptive in business lingua but inside the authorisation method was “if user.Id == 0” or “if challengedUser.Id == persistedUser.ID”. It was not usable since Id as integer is a technical (often a persistance layer construction) solution for recognising an entity. In business terms it is more like “user.IsPersisted” and “if challengedUser.SameAs(persistedUser)”. I continued redusing the problem space to what I really wanted to test when authorising.
This way I seriously minimised the permutations, as an authorisable User did not care about “Id” or “Name” or “BusinessPartner” but only “IsLoggedOn” and “Role”. With 6 roles that means 12 permutations. With Project I came to, say, 32 permutations and user 4. This gives in all 12*32*4=1500 variants. Not a problem to test every combination now if I just put some (business) intelligence into creating the tests. #win

Let’s say 240 of them were positive (autorised) and the rest negative.

I started with creating a simple lib for permutating every possible input and them through One Test to green light “not authorised”. Everything red should be authorised. Already here I might have found combinations that was authorised when they should not have been.

Then I, manually, created a list of every permutation allowing authorisation. Well… manually for a programmer is reducing to loops and ifs so one method could create every authorised combination of type A and one of type B. Alltogether that is, say, 10 different methods and some manual ones. They were concatenated to a list and I made the Test assert authorised and not-authorised according to this list.

So now I had a test for every kind of authorisation check testing both authorised and not authorised and tests looks like business logic.