Archive for the ‘Code and Development’ Category
June 2nd, 2017
I am presently using TFS’s code reviewing tool and IMHO it lacks visualisation of which comments are important and which are not.
So I came up with the idea of prefixing my comments.
PRAISE: Good refactoring, I like how you remove the negation.
FIX: A null check is missing.
SUGGESTION: Split line.
CHECK: Is it the right enum used here?
EXPLAIN: Is this comparison really correct? or seems to overflow to me.
If there are many FIX but only one or a few are critical I prefix the critical once with RED, like RED FIX.
This gives the opportunity to “while you already are updating the module, do these things too”.
When code reviewing do not forget to give appraisal too
Update:
This is what I wrote at Patricia Aas’ show at Dotnetrocks:
I have learned that some don’t like code review because it is a critique of the code written. When failing a code review the task is halted for a rewrite and everyone loses.
I love it because to me it is a discussion about code and the system *we* are building *together*.
The code involved is a talking piece.
I prepend my comments with “PRAISE:”, “FIX:”, “SUGGESTION:”, “EXPLAIN:” and “CHECK:”.
FIX is the only one failing a code review.
Depending on the amount of, and severity, EXPLAIN and CHECK might give a red flag too.
The rest are for discussion; to learn and spread knowledge.
PRAISE is important. Only giving negative critique wears a relation. It might also be a part of the reason some don’t like code reviews. It can be hard to recognise good code as, as with many things, it steps out of the way so one does not reflect upon it. Good naming of a method, remember to remove old usings, good test data, …
SUGGESTION is how *I* would like to write the code. Change if you like, don’t if you don’t. I’d be happy to talk about it but there is no need to involve me in a decision.
EXPLAIN on the other hand requires my input. I don’t understand what you are doing – please explain it to me. If it is easy to explain then I should probably have understood; but if you have a hard time explaining it, then there is a good chance the code is too complex. (I have found a bug this way.)
CHECK is more serious. I am not sure you are doing the right thing and would like for you (or anyone else, including me) to double check it.
Typically there will be quite some SUGGESTION followed by a healthy amount of CHECK and EXPLAIN.
Update:
I have used this method for some projects now and only received positive or neutral feedback.
In one project one colleague took every SUGGESTION and implemented them while another ignored anything that wasn’t FIX. Both were skilled and made good choices. We talked a lot about code.
In another project my colleague liked to start with only looking at FIX to get a tally and then read through the rest. S/he is/was junior and we’ve had good discussions about code.
Update:
Someone else has come to the same conclusion: https://conventionalcomments.org/#labels
April 28th, 2017
I have started developing a lib for creating pseudo randomised data. (yes, I know all randomised data on a computer is pseudo randomised, but in my case I want to control the seeding)
The lib, basically, is the ordinary dotnet random class with constructor with some customised constructors to make it easier to add a known seed. It also contains methods for randomising strings with a prefix, a certain length, int, long, decimal, guid, enum and other stuff that is often used.
The seeding is taken from the test method’s name.
This will give the same values for every test run of that method.
February 12th, 2017
Prerequisite
See Compile Aspnet core 1.1 outside Docker container but run within
Create a controller
Create a file TodoController.cs file in the same folder as HomeController.cs file. In a real application we should probably put it in a folder “api” or similar.
Fill it with the text below (which is a refinement of https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api ):
[Update:There is now a
or similar so you don’t have to copypaste the code below. I plan to write about it as soon as I can.]
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
| using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication.Controllers
{
// Setting the Route attribute is important.
// Otherwise we don't get the restish behaviour we're looking for.
// I cannot say exactly why this is so.
[Route("api/[controller]")]
public class TodoController : Controller
{
private static List _data =
new List{
new Item{
Id = 1,
Name = "One"
},
new Item{
Id = 2,
Name = "Two"
},
};
[HttpGet]
public IEnumerable<item> GetAll()
{
return _data;
}
[HttpGet("{id}", Name = "GetTodo")]
public string Get(int id)
{
return _data.Single(d => d.Id == id).Name;
}
[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] string name)
{
GetItem(id).Name = name;
return new NoContentResult();
}
[HttpPost]
public IActionResult Post(string name)
{
var item = new Item { Id = _data.Count() + 1, Name = name };
_data.Add(item);
return CreatedAtRoute("GetTodo", new { id = item.Id }, item);
}
private Item GetItem(int id)
{
return _data.Single(d => d.Id == id);
}
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
}
</item> |
Compile:
And start the web server with the application:
1
| dotnet bin/Debug/netcoreapp1.1/MyWs.dll |
Call the controller
Start another terminal.
First check what we have.
1
| curl localhost:5000/api/Todo |
Then check one item.
1
| curl localhost:5000/api/Todo/2 |
Create a new.
1 2
| curl --data "Name=Three" localhost:5000/api/Todo/
curl localhost:5000/api/Todo/3 |
Update an existing according to Stack overflow.
1 2 3 4 5
| curl -H "Content-Type: application/json" \
-X PUT \
--data '"Tva"' \
localhost:5000/api/todo/2
curl localhost:5000/api/Todo/2 |
February 6th, 2017
There is no native image processing lib for dotnet core but someone at Microsoft has been kind enough to list 6 alternatives here, namely CoreCompat.System.Drawing,ImageSharp,Magick.NET, SkiaSharp, FreeImage-dotnet-core and MagicScaler.
January 14th, 2017
Prerequisite
OSX
Docker
Dotnet core 1.1
A folder named (e.g.) /Users/yourname/Docker/Dandelion
and a folder named (e.g.) /Users/yourname/Docker/Dandelion/MyWeb
Create the web
In MyWeb run
1 2 3 4
| dotnet new -t web
dotnet restore
dotnet build
dotnet publish |
Now we have a web site compiled and ready to run with [dotnet run].
You can run the app and then [curl localhost] to find out if it is runnable; but if you got no error message earlier all should be ok.
Create an image
Go to the Dandelion folder.
Create a file Dockerfile and paste into it:
1 2 3
| FROM microsoft/dotnet
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80" |
Then, to create the image run
1
| docker build -t yourname/dandelion . |
Note that the name must be all small caps.
Don’t miss the trailing period.
If you want to check that the image is created just execute [docker images] which should show the new image at the top of the list.
Start the container
Go to the Dandelion folder. (you are probably already standing there)
Execute
1 2 3 4 5
| docker run -p 80:80 \
-ti --rm \
-v /Users/yourname/Documents/Docker/Dandelion/MyWeb/bin/Debug/netcoreapp1.1/publish:/MyWeb \
yourname/dandelion \
/bin/bash -c 'cd /MyWeb; dotnet MyWeb.dll' |
and your web should start.
Verify result
Either open another console and execute
or go to localhost in your web browser.
January 10th, 2017
When trying to
a dotnet core 1.1 project you might receive
1 2
| Publishing TheApp for .NETCoreApp,Version=v1.1
No executable found matching command "npm" |
The remedy is to install NPM.
If you are running OSX you probably got Homebrew with the installation.
So just
Then you might get
No executable found matching command “bower”
which means that Bower is missing. According to https://github.com/dotnet/cli/issues/3293 you should install it through npm like so:
1 2
| npm install -g bower
npm install -g gulp |
January 7th, 2017
Prerequisite:
Docker and Dotnet Core 1.1 should be installed on the host (=OSX).
You have a folder somewhere, for instance /User/yourname/Documents/Docker/mywebapp.
Create the image
In your folder, create the dockerfile
1 2 3 4 5 6 7 8
| FROM microsoft/dotnet
# VOLUME /Documents/Docker/mywebapp
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80"
RUN mkdir app
WORKDIR /app
RUN dotnet new -t web
RUN dotnet restore |
Then execute
1
| docker build -t mynick/myimage . |
in a terminal in said folder.
If you now execute
you can se the new image as mynick/myimage.
Create the container and start the web server
In the terminal execute
1 2 3 4
| docker run \
-p 80:80 \
-ti --rm mynick/myimage \
/bin/bash -c 'cd /app; dotnet run;' |
Behold result
In a terminal execute
or in a web browser go to
January 3rd, 2017
Prerequisite
Install docker.
Install Dotnet Core 1.1.
Create web application
Create a directory “theapp”.
Open a terminal and, in the directory, execute:
1 2 3
| dotnet new -t web
dotnet restore
dotnet run |
Check it works.
Execute, in a terminal:
Or is it
?
Create container
In a terminal execute:
(after you have have updated “YourRootedPathAndFolder” appropriately.
1 2 3 4 5 6
| docker run -p 80:80 \
-e "ASPNETCORE_URLS=http://+:80" \
-ti --rm \
-v /YourRootedPathAndFolder/TheApp:/theapp \
microft/dotnet \
/bin/bash -c 'cd /theapp; dotnet restore; dotnet run' |
and when it is finished churning through the long list of modules open a new terminal and execute
on your host to receive a smaller waterfall of HTML.
That should be it.
Troubleshooting
If you forget to do the dotnet restore
1 2 3 4
| docker run -p 80:80 \
-ti -v /Users/ola/Documents/Docker/loose/TheApp:/theapp \
microsoft/dotnet \
/bin/bash -c 'cd /theapp; dotnet run' |
You get something like:
Project theapp (.NETCoreApp,Version=v1.1) was previously compiled. Skipping compilation.
Error: assembly specified in the dependencies manifest was not found — package: ‘Microsoft.AspNetCore.Antiforgery’, version: ‘1.0.1’, path: ‘lib/netstandard1.3/Microsoft.AspNetCore.Antiforgery.dll’
Attach and request
Run
in a terminal. Note the Container ID.
Then execute, after the b5a…f5 is updated appropriately.
1
| docker exec -it b5a8ccd5b1f5 bash |
Now you have a shell inside the container and should be able to get a result from:
December 31st, 2016
Install Dotnet core à la https://www.microsoft.com/net/core#macos
Create a folder and to to it.
Open a terminal (one cannot reuse any terminal from before dotnet was installed since the path is updated) and execute:
1 2 3
| dotnet new -t web
dotnet restore
dotnet run |
Open another terminal and execute:
A pile of HTML should scroll into view.
In the first terminal you can see the reaction.
December 29th, 2016
Prerequisite
Install Docker on OSX.
Create Dockerfile
Create a file named Dockerfile in a new folder.
Its contents are:
1 2 3 4 5 6 7 8
| FROM microsoft/dotnet
# VOLUME /Documents/Docker/dnc
EXPOSE 80
ENV "ASPNETCORE_URLS=http://+:80"
RUN mkdir app
WORKDIR /app
RUN dotnet new -t web
RUN dotnet restore |
Create image
1
| docker build -t yournick/yourimagename . |
Run image
1 2 3
| docker run -p 80:80 \
-ti --rm yournick/yourimagename \
/bin/bash -c 'dotnet run' |
See output
On the host:
and you should se a whole dab of HTMl. You can also watch it in your browser.