All posts by Rikard Ottosson

About Rikard Ottosson

.NET developer. Father. Couch potato. Old, cynical, willing to learn, but quick to judge. Likes old hard rock. Hope to buy more guitars.

Rebootcamp

I have been saying a bunch of things, repeating what others say, mostly, but never actually internalised what they really meant. After a week with Fred George and Tom Scott I have seen the light in some way. I have seen proof of the efficacy of pair programming, I have seen the value of fast red-green-refactor cycles and most importantly I have learnt just how much I don’t know.

This was an Object Bootcamp developed by Fred George and Deliberate and basically consisted of problem solving in pairs going over various patterns and OO design in general, pointing out various code smells to look out for and how to refactor your way out of trouble. The course packed in as much as the team could take over the course of the week and is highly recommended. Our finest OO developers in the team still learned new things over the week and the rest of us learned even more.

Where to go from here? I use this blog as a way to write down things I learn so I can reference it later. My fanbase tends to stick to my posts about NHibernate and ASP.NET MVC 3 or something from several years ago, so I need not worry about making things fresh and interesting for the readership. The general recommended reading list that came off of this week reads as follows:

So, GoF and Refactoring – no shockers, eh? We have them in our library and I’ve even read them, even though I first read some other derivative book on design patterns back in the day, but obviously there are things that didn’t quite take the first time. I guess I was too young. Things make so much more sense now when you have a catalogue of past mistakes to cross-reference against various patterns.

The thing is, what I hadn’t internalised properly is how evil getters and setters are. I had some separation of concerns in terms of separating database classes from model classes, but still the classes didn’t instantiate good objects, they were basically just bags of data, and mediator classes had business logic, messing with other classes data instead of proper objects churning cleanly.

Encapsulating information in the system is crucial. It is hard to do correctly, but by timeboxing the time from red to green you force yourself to build the next simplest clean thing before you continue. There is no time for gold plating, and boy you veer off and try something clever only to realise that you needed to stop and go back. Small changes. I have written this so many times before, but if you do it properly it really works.  I have seen JB Rainsberger and Greg Young talk about this, and I have nodded and said sure. Testify! “That would be nice to get to do in practice” was my thinking. And then I added getters and setters to my classes. Or at least made them anemic by having a constructor with parameters and then getters, used by demigod classes. The time to make a change is yesterday, not tomorrow.

So, yes. Analysis Patterns is a hard read, said Fred. Well, then. It seems extremely interesting. I think Refactoring to Patterns will be the very next thing I read, but then I will need to take a stab at it.

I need to learn where patterns could get rid of code smells, increase encapsulation and reduce complexity.

There is a handy catalogue of refactorings that I already have a shortcut to in the chrome toolbar. It gets a lot more clicks now, but in general I will not make any grand statements now but rather come back with a post showing results.

Powershell woes

What is the point of powershell?
Trying to orchestrate and automate distributed tests with powershell leads to things such as using remote powershell or psexec to start tests on VMs and to use PSDrives to set up tests and collect logs.
So then you would like some calls to be reliable. Starting WinRM on a remote box, or setting up a PSDrive to get fresh executables to VMs or collecting logs neesd to be reliable.
With a 10% successrate out-of-the-box you wonder how people ever use this stuff, and the truth is of course people don’t, they write reliability layers around it with retries and exception handlers, just for the Happy-path nothing-important-has-failed scenario
Had MS spent the extra five minutes to make the built-in code be less unreliable and weak and perhaps to only return a failure once every means to recover the situation had been exhausted would have spared every single powershell user the work to write their own reliability layer just to get the basics working.
Case in point:  New-PSSession. There is no way in the known universe that should be allowed to fail. OK, if there are HTTPS certificat problems or network connectivity problems, say so, but other than that I deserve an apology. Eloquent and part of the return object.  Problem is 90% of the time New-PSSession will fail, for no reason. No reason whatsover.
I mean I know the test triangle and all that, but for that tip of the triangle I don’t need any help from MS to make the system test EVEN MORE EXPENSIVE to make and maintain.

UTF-8

Having grown up in a society evolved beyond the confines of 7-bit ASCII and lived through the nightmare of codepages as well as cursed the illiterate that have so little to say they can manage with 26 letters in their alphabet, I was pleased when I read Joel Spolsky’s tutorial on Unicode. It was a relief – finally somebody understood.

Years passed and I thought I knew now how to do things right. And then I had to do Windows C in anger and was lost in the jungle of wchar_t and TCHAR and didn’t know where to turn.

Finally I found this resource here:

http://utf8everywhere.org/

And the strategies outlined to deal with UTF-8 in Windows are clear:

  • Define UNICODE and _UNICODE
  • Don’t use wchar_t or TCHAR or any of the associated macros. Always assume std::string and char * are UTF-8. Call Wide windows APIs and use boost nowide or similar to widen the characters going in and narrowing them coming out.
  • Never produce any text that isn’t UTF-8.

Do note however that the observations that Windows would not support true UTF-16 are incorrect as this was fixed before Windows 7.

Powershell

I am extending some tests we are running in powershell. It is used to validate that the chef configs we are setting up so that all moving parts are running.

One thing I tried to google that wasn’t  perfectly clear to a n00b like me was how to use static objects in powershell, specifically take byte array and interpret it as a UTF8 string.

Apparently this is how you do it:

$enc = [System.Text.Encoding]::UTF8
$c = $enc.GetString($result.Content)

Where the $result came out of a WebRequest. This means I can test that $c contains an expected value and fail the script, and thus – the build, if the result is unexpected.

TDD setup in VS14

I’ve been trying out VS14 CTP4, doing some code katas and I was thinking if there is a better way I could set up my Visual Studio environment. The NuGet console works well for running tests on the command line or committing code but of course there is an XUnit runner plugin that works well to run the tests from within the IDE. After checking what the gurus say it seems having the test and the production code side by side is the way to go, which is easy when you have one class and one test. Ideally there would be a way to keep the window structure organised in some semi-automated way, but my limited googling has not given me any good answers, although unpinning most windows and keeping the test runner at the bottom and test- and production code in the rest of the screen is quite usable.

I haven’t installed ReSharper on this Visual Studio CTP which gives me a rare opportunity to try the native features before ReSharper saves the day and although the shortcuts are wrong (but you could easily remap Ctrl+. to Alt+Enter and feel less lost immediately) it does have the most basic features you need such as extract method and rename.

Overall it is possible to have a neat workflow as before but you would be hard pressed to let go of ReSharper as the quick create-shortcuts and the easy-to-use refactorings are hard to live without once you have used them in anger.

Posh Git

I have tried to get my head around PowerShell for a while, and I’ve read blogs by Scott Hanselman about PsReadline and PoshGit and decided to finally get on with it and use it properly.

This post is just a repository of my current setup.

Install PsGet if you don’t have it:

(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex

Install PsReadline:

Install-Module PsReadline

Install Git for Windows

http://git-scm.com/download/win

Install posh-git

Install-Module posh-git

Update the profile.ps1 file

Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)

# Load posh-git module from current directory
Import-Module PSReadLine
Import-Module posh-git

# If module is installed in a default location ($env:PSModulePath),
# use this instead (see about_Modules for more information):
# Import-Module posh-git


# Set up a simple prompt, adding the git prompt parts inside git repos
function global:prompt {
 $realLASTEXITCODE = $LASTEXITCODE

 # Reset color, which can be messed up by Enable-GitColors
 $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor

 Write-Host($pwd.ProviderPath) -nonewline

 Write-VcsStatus

 $global:LASTEXITCODE = $realLASTEXITCODE
 return "> "
}

Enable-GitColors

Pop-Location

Start-SshAgent -Quiet

set-alias -name np -value "C:\Program Files\Sublime Text 2\sublime_text.exe"

function vsh() {
 Write-Output "Opening first solution..."
 $sln = (dir -in *.sln -r | Select -first 1)
 Write-Output "Found $($sln.FullName)"
 Invoke-Item $sln.FullName
}
 
function title($str) {
 (get-host).ui.rawui.windowtitle = $str
}
 
function ga() {
 Write-Output "Staging all changes..."
 git add -A
 git status
}

function gs() {
 git status
}

function gsd() {
 Write-Output "Pushing changes to subversion..."
 git svn dcommit
}

function gsf() {
 Write-Output "Checking subversion for recent changes..."
 git svn fetch
}
 
function gco() {
 param([switch]$amend, [switch]$a)
 
 $argstr = $args -join ' '
 $message = '"', $argstr, '"' -join ''
 
 if ($amend -or $a) {
 Write-Output "Amending previous commit with message: $message"
 git commit -m $message --amend
 } else {
 Write-Output "Committing with message: $args"
 git commit -m $message
 }
}
 
function gca() {
 $argstr = $args -join ' '
 $message = '"', $argstr, '"' -join ''
 
 Write-Output "Amending previous commit with message: $message"
 git commit -m $message --amend
}

Links

Windows 10 tech preview

So, later than everybody else I installed Win10 tech preview.

I am not agreeing with the versioning strategy and feel like Windows 8.2 had been a more fitting identifier given the limited changes, but the ill-deserved but yet pervasive stigma of the new-fangled Windows 8 had to go away, so I see why going with a major version increase might have made sense.

Multiple desktops is easier to use than Sysinternals Desktops, with the isolation being less complete, so you can Windows Tab between all apps irrespective of desktops and easily move windows between them.

The semi transparent console window thingy is pretty neat but I’m surptised by the limited font choices. Copy / Paste and proper selection just worksTM and I suspect that just like after Ctrl-S arrived in Notepad on Windows 2000 it will probably be inconceivable that it ever worked differently. I installed the operating system on VMWare player and threw in VS14 which diesn’t compile the ASP.NET vNext, but I have spent literally 0 seconds figuring out why and I suspect I haven’t configured something right. The MS Twitter app doesn’t work on my VM, neither does OneGet specifically because Chocolatey is not a provider and no methods of adding providers worked.

I resorted to install Git like a barbarian, downloading the installer from git-scm.com/downloads, but posh-git and psreadline worked as expected. Finally I had a very neat git environment set up in a few moments. I like.

The start menu feels a bit weird, but does make more sense with a desktop OS and I will probably find it more useful as time goes by.

So anyway, if you have an extra computer laying around, get Win10 and try it out. I’ve reported my gripes to the feedback tool, I suggest you do the same. Don’t believe they hype, it’s just a really nice Windows 8.1 Update 2/ Windows 8.2, not  a revolution by any means, just good small improvements overall.

MOVSXD

I have relocated to the Southeastern outskirts of London with my family since I last posted. After 5 years with Jayway it was strange to leave, but a new adventure in a new country awaited. I started working at Cloudhouse Technologies that are developing application virtualisation- and data access virtualisation products. A whole heap of very clever developers work here and we are looking for more such people. We provide ways for companies to leverage new technologies to run applications developed for earlier platforms and network topologies and the work ranges from analysing network logs, designing distributed systems, playing with various low-level API:s and sometimes smithing some websites. I couldn’t believe the job spec when I saw it, it’s like a kinder surprise with all fun things rolled into one. Anyway, so that’s what I have been doing and what has kept me busy.

Microsoft, Apple and the future

I have seen a bunch of uninformed blogs on LinkedIn about the state of large corporations with conclusions based only on hardware preferences of the author, loose speculation and nothing else. I thought why don’t I blog uninformedly on the same subject? I am going to take some of my opinions and state them as fact because let’s face it, I’m right.

Apple vs Microsoft. In a recent study of cool companies people want to work for, Apple scored on top and MS dropped out of the top 20. That seems perfectly reasonable. Microsoft got labelled boring and the wrong kind of evil in the nineties and gradually being proven a lot less evil than both Google and Apple doesn’t help them become cool. Their products are good, not cool. Granted not the best, but better than a lot of them out there, but definitely not cool.

Apple meanwhile deliver decently specified hardware with good margins, iffy software with while showing sub par security conscience. Their iWork Office killer is far from MS Office in terms of features, but sadly for Microsoft, that doesn’t quite matter as much as they would hope. People will take iffy build quality of a laptop (yes), a few missing features of an office suite and a not-very-successful cloud solution if they get a cool laptop that has good battery life. I am not being bitter or sarcastic her. I for instance want the dustbin (the Mac Pro) with very little supporting technical data that would make it win over a traditional tower.

Microsoft just let Nokia fork Android. I’ve said this before, but this would have been the ideal thing to do in the first place. Making several operating systems from scratch just because you think you can is maybe not the most profitable solution. Especially since throughout the early life of Windows Phone, the patents around Android were Microsoft’s top revenue source in the mobile space. Instead having went with an Android fork, focussed on providing excellent gateways towards Microsoft services, driving revenue in Bing/Azure/Office365 could have been far more efficient in my not so humble or well grounded opinion.

Microsoft have always done well in providing API:s and documenting them thoroughly, knowing that engaging developers to extend and combine your products really helps you in the long run. Eventually Apple realised the same thing and overcame Jobs’ doubts and created the AppStore for iOS and that was a roaring success obviously.

Yes, Microsoft made API:s and an awesome development environment Visual Studio and they brought forth evangelists to tell people what to make with their products, but Apple and Google were on a roll. Quickly scores of .NET developers, that still felt that they could never go Apple because they detested the brand and all it stands for, instead found that switching to Java was a survivable pain to get to develop for a mobile platform that essentially is Windows XP for mobile, a platform where you can do whatever you want with the phone and put it in the store.

The problem is that Microsoft desperately wanted to provide the entire foundation of the ecosystem. While Steve Jobs even back in 1985 was overseeing work on the Big Mac that was UNIX based – what later became NeXTStep and OSX, Microsoft soldiered on making everything themselves from drivers to OS to apps, and they were big enough for a while to get away with it, but it is easier if you take things that exist and work and build on them.

This is where Satya Nadella comes in. His division of Microsoft were forerunners in Microsoft working with open source, as in opening their own projects as well as adhering to external open source standards (OWIN, for instance). Apple, Google and others have a far bigger legacy in Open Source, of course, but Microsoft has been turning around, slowly but surely, and Nadella’s org as been the driving force behind this development. Perhaps this can be an opening for Microsoft getting a little leaner. focussing on a few key revenue sources allowing other areas of significant spend to be dropped, adopting open standards and simply contributing to existing community projects.

The only reason the security problems Apple have faced have been so limited despite the complete non-action from Apple over the decades is that they based their OS:es on a core that other people kept going for them. They made it easy for developers to harness the power of what I like to call the dark side, the *nix open source community, which is as vast as the universe itself, In a way that Windows doesn’t.

As an example of the MS of old: When MS started dogfooding Windows Server in large scale clusters they realised that they need a secure file transfer mechanism and a scripting environment that had a bit more muscle than CMD.exe. They could have implemented an SSH server and gotten both – in a standard way no less, but instead they made Powershell and MS Deploy. Purists would have argued in favour of a proper Bash shell and a unified file system with mount points instead of the unfashionable disk drive system, but that even today can be implemented or at least simulated for the desperate among us.

Maybe the course that Nadella set out is one that will lead to a turnaround for Microsoft. The shrinking OS license revenue will be a problem for a while, but I think there is a way out on the other side, and I am suspecting Nadella might know it.