Thursday, 8 January 2015

C# 6

Been digging in to some of the new C#6 features, a few examples/thoughts

Auto Property Initializers

Auto property initializers allow you to set the initial value without having  to do it in a constructor

 public string TestString { get; set; } = "Hello";  

This can also be used for get only properties

 public string TestString { get; } = "Hello";  

Seems like a non-static field cannot access a method to set the value:
 Cannot access non-static method 'GetString' in static context  

When changing the method to static it appears to work, interesting...
 public string TestMethodReturn { get; } = GetString();  
   
 public static string GetString()  
 {  
   return "Method";  
 }  

This appears to be because the property initializers are called before the constructor.

Think I will be using this stuff, especially setting the get only property. Nice little bits of syntactic sugar.

Constructor Assignment to get only Auto Properties

Does what it says on the tin,
 public string TestString { get; }  
 public Program()  
 {  
   TestString = "Hello";  
 }  

Also seems reasonably useful, though not sure if I will favour this or setting it on the property.

Static Using

Pretty simple, just imports the static methods for use without their class name.
 using System.Console;  
   
 WriteLine("Hello");  

Not sure if using this stuff will be a good idea as it may reduce readability, but then maybe there will be cases where this would be useful. Advise using with caution.

Dictionary Initializers

There is a new dictionary initialzer format, the old one like the following still works
 var dictionaryOld = new Dictionary<string, string>  
 {  
   {"first", "First"},  
   {"Second", "Second"},  
   {"Third", "Third"},  
 };  

The new format is as follows
 var dictionaryNew = new Dictionary<string, string>  
 {  
   ["First"] = "First",  
   ["Second"] = "Second",  
   ["Third"] = "Third",  
 };  

So why the new format, well the only reason that I can see is this allows you to use the new format initializer just by implementing a this property on your own class:
 public class MyCollection  
 {  
   private readonly Dictionary<string, string> _store = new Dictionary<string, string>();  
   
   public string this[string key]  
   {  
     get { return ""; }  
     set { _store[key] = value; }  
   }  
 }  

You can then use the new initializer when creating instances
 var custom = new MyCollection  
 {  
   ["First"] = "First",  
   ["Second"] = "Second",  
   ["Third"] = "Third",  
 };  

Think this stuff will be useful for people making custom collections, good to know it exists so that I can use it when necessary. Though i'm now not sure which initializer I should be using when initializing dictionaries...

Wednesday, 7 January 2015

New Toys

Looks like there has been a load of new stuff for Visual Studio and .NET Recently. So I thought I would have a play. Link.

Visual Studio Ultimate 2015 Preview
Download and installation pretty standard, havent noticed anything exceptionally different, lambda debugging looks like it will be pretty cool. Will spend a bit more time playing with the features after I have played with C#6.
List of features

Roslyn (The new C# compiler platform)
Pretty easy to install in to a project just put open a console and type:
Install-Package Microsoft.CodeAnalysis -Pre
Roslyn Website

R#
New platform installer for 9.1 picked up vs2015 and installed, works fine. Looks like some of the c# 6 support is already added as it suggests adding the ?. (Null Propagation) operator.
Resharper Platform Installer

C# 6
Tried out some of the new features including Null propagation, using Static Members ,auto Property Initializers,Getter only auto properties,Exception Filters. Lots of fun :) hopefully will have time to look at a few of these more in depth.

Here is a list of c#6 features:
















FeatureExampleC#VB
Auto-property initializerspublic int X { get; set; } = x;AddedExists
Getter-only auto-propertiespublic int Y { get; } = y;AddedAdded
Ctor assignment to getter-only autopropsY = 15AddedAdded
Parameterless struct ctorsStructure S : Sub New() : End Sub : End StructureAddedAdded
Using static membersusing System.Console; … Write(4);AddedExists
Dictionary initializernew JObject { ["x"] = 3, ["y"] = 7 }AddedNo
Await in catch/finallytry … catch { await … } finally { await … }AddedNo
Exception filterscatch(E e) if (e.Count > 5) { … }AddedExists
Partial modulesPartial Module M1N/AAdded
Partial interfacesPartial Interface I1ExistsAdded
Multiline string literals"Hello<newline>World"ExistsAdded
Year-first date literalsDim d = #2014-04-03#N/AAdded
Line continuation commentsDim addrs = From c in Customers ' commentN/AAdded
TypeOf IsNotIf TypeOf x IsNot Customer Then …N/AAdded
Expression-bodied memberspublic double Dist => Sqrt(X * X + Y * Y);AddedNo
Null propagationcustomer?.Orders?[5]?.$priceAddedAdded
String interpolation$"{p.First} {p.Last} is {p.Age} years old."Added*Planned
nameof operatorstring s = nameof(Console.Write);Added*Planned
#pragma#Disable Warning BC40008AddedAdded
Smart name resolutionN/AAdded
ReadWrite props can implement ReadOnlyExistsAdded
#region inside methodsExistsAdded
Overloads inferred from OverridesN/AAdded
CObj in attributesExistsAdded
CRef and parameter nameExistsAdded
Extension Add in collection initializersAddedExists
Improved overload resolutionAddedN/A
Source

Xamarin
At first the Xamarin project templates did not appear, but I updated Xamarin studio to v 3.9 (In the alpha/Beta Channel) which features Visual Studio 2015 support and the templates appeared.

Looks like there is lots of fun ahead for .net!

Friday, 12 December 2014

Agile Thoughts

Recently I have found myself looking at our agile process and trying to work out ways of improving things, here are a few thoughts.

Iterations
The idea of iterations seems like a nice thing when you work in an environment that releases completely ad-hoc and your practices don't really support this due to lack of proper testing. But when I look at projects that utilize automated testing and Continuous Integration the prospect of continuous delivery suddenly seems like a nice thing. If my code base is stable and automatically tested then why shouldn't I release at any point? there should never be a check in where the code base doesn't build/work. While I have defiantly argued the case that the business should be able to commit to 2 weeks worth of work without requiring to add additional stories to the iteration. Is it really such a bad thing for the business to be able to change their mind daily to respond quicker to the needs of customers? This seems like a very agile practice, responding to change quickly to give the business the edge of the competition.

Estimation
The developer in me thinks that pretty much all estimation can quite happily be put under the category 'Waste', but I understand that this kind of thing when done properly can be good for the business to estimate how long things are going to take. I guess the main reason I am cynical is I have yet to see it work very well. At the moment I seem to favor planning poker as an estimation method and like the idea of using a T-Shirt Size/Numbered system crossover. The main reason for this being that t-shirt sizes are very easy for people to understand and they are not related to time, I've found that the whole story point can be quite a confusing concept to explain, though the downside of t-shirt sizes over story points is that the relation ship between the sizes can be very unclear (is a medium twice the size of a small.. e.t.c.). So I am thinking of doing a t-shirt size system but with the story point numbers wrote on the cards as well, this should hopefully make is accessible and clear.

Retrospective
I think this is the most valuable thing that agile brings us, the process of Kaizen (Continuous Improvement). While I often struggle with what to do with the items that come up in retrospectives (How to manage/resolve the problems). 

Stand-Ups
Stand ups are quite a good way of keeping track of whats going on between the team but I often find that most of what's said is already known by the people who need to know it. I think a stand up can be a useful tool when people lack time or communication but maybe it is not required by co located teams in an open office where anyone they need to speak to is often no more than a 30 second walk away.