<feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://www.riviti.org" xml:lang="en">
	<generator uri="http://zotonic.com/">Zotonic - Atom Feed Module</generator>
	<updated>2012-08-21T17:55:14+02:00</updated>
	<logo />
	<link rel="self" type="application/atom+xml" href="http://www.riviti.org/en/feed/article" />
	<id>http://www.riviti.org/en/feed/article</id>
	<title>Refactoring stuff. - Article</title>

    
	
		
	
		<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xml:lang="en">
			<id>http://www.riviti.org/en/id/327</id>
			<updated>2012-08-21T17:55:14+02:00</updated>
			<published>2012-08-21T17:46:00+02:00</published>
			<author>
				
				<name>Simon Lindgren</name>
				<uri>http://www.riviti.org/en/id/315</uri>
				
			</author>
			
			<link rel="alternate" type="text/html" href="http://www.riviti.org/en/article/327/gsoc-2012-rounding-up"/>
			
			
			
			

			<title>GSoC 2012: Rounding up</title>
			
				<summary>As it turns out, it was a little hard to keep up the blogging, but I thought I&#39;d at least end with a post, now that the coding period is officially over.</summary>
				<content type="html">&#60;p&#62;My project was about making Code Action providers and Code Issue inspectors for the NRefactory library. This work will ultimately benefit the &#60;a title=&#34;The MonoDevelop project site&#34; href=&#34;http://monodevelop.com&#34;&#62;MonoDevelop IDE&#60;/a&#62; and the &#60;a title=&#34;The SharpDevelop project site&#34; href=&#34;http://www.icsharpcode.net/opensource/sd/&#34;&#62;SharpDevelop IDE&#60;/a&#62;.&#60;/p&#62;&#60;p&#62;&#60;a title=&#34;Converting to and from object and collection initializers&#34; href=&#34;/en/article/326/converting-to-and-from-object-and-collection-initializers&#34;&#62;Conversion to and from initializers&#60;/a&#62; which I talked about previously, is done, as are several other action providers and issue inspectors.&#60;/p&#62;&#60;p&#62;Code Action providers:&#60;/p&#62;&#60;ul&#62;&#60;li&#62;Convert initializer to explicit initializations&#60;/li&#62;&#60;li&#62;Convert statements to initializer&#60;/li&#62;&#60;li&#62;Add catch type&#60;/li&#62;&#60;li&#62;Convert anonymous delegate to lambda&#60;/li&#62;&#60;li&#62;Convert lambda to anonymous delegate&#60;/li&#62;&#60;li&#62;Convert extension method invocation to static method invocation&#60;/li&#62;&#60;li&#62;Convert static method invocation to extension method invocation&#60;/li&#62;&#60;li&#62;Iterate via foreach&#60;/li&#62;&#60;li&#62;Move declaration to outer scope&#60;/li&#62;&#60;/ul&#62;&#60;p&#62;Code Issue inspectors:&#60;/p&#62;&#60;ul&#62;&#60;li&#62;Format string syntax error checker&#60;/li&#62;&#60;li&#62;Parameter can be demoted to base type&#60;/li&#62;&#60;li&#62;Call to base.Equals resolves to Object equals which is reference equality&#60;/li&#62;&#60;li&#62;Call to virtual member from constructor&#60;/li&#62;&#60;li&#62;Exceptions should be rethrown using empty &#38;quot;throw;&#38;quot;&#60;/li&#62;&#60;li&#62;Call to base.GetHashCode() resolves to Object.GetHashCode() which is reference based&#60;/li&#62;&#60;li&#62;Incorrect ordering of parameters to exceptions with inconsistent parameter ordering&#60;/li&#62;&#60;li&#62;Optional parameter is passed the default value and can be skipped&#60;/li&#62;&#60;li&#62;Redundant catch clause&#60;/li&#62;&#60;li&#62;Redundant .ToString()&#60;/li&#62;&#60;li&#62;Reference to static member via derived type&#60;/li&#62;&#60;li&#62;Static field in generic type&#60;/li&#62;&#60;li&#62;[ThreadStatic] on instance field&#60;/li&#62;&#60;li&#62;&#38;#39;value&#38;#39; parameter unused&#60;/li&#62;&#60;li&#62;Variable can be moved to nested scope&#60;/li&#62;&#60;/ul&#62;&#60;p&#62;Granted, those are very terse descriptions, but with some imagination I think you&#38;#39;ll understand what most of them do. In the end, I&#38;#39;m glad to have completed GSoC, even though I had hoped to complete more Code Actions and Code Issues.&#60;/p&#62;&#60;p&#62;I will likely continue improving this code and implement more Code Actions &#38;amp; Code Issues when I have time, so if you have any ideas, feel free to post them in the comments, make a feature request in the &#60;a title=&#34;NRefactory bug tracker&#34; href=&#34;https://github.com/icsharpcode/NRefactory/issues?state=open&#34;&#62;NRefactory issue tracker&#60;/a&#62;, or &#60;a title=&#34;About&#34; href=&#34;http://www.riviti.org/en/page/318/about&#34;&#62;contact me&#60;/a&#62; directly.&#60;/p&#62;&#60;p&#62;Lastly, I&#38;#39;d like to thank the mono project for a great summer of code. :-)&#60;/p&#62;</content>
			
			
			
			
				
			<category term="article" scheme="http://zotonic.com/id/category" />
		</entry>
	


	
		
	
		<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xml:lang="en">
			<id>http://www.riviti.org/en/id/326</id>
			<updated>2012-08-19T16:28:58+02:00</updated>
			<published>2012-06-19T13:57:00+02:00</published>
			<author>
				
				<name>Simon Lindgren</name>
				<uri>http://www.riviti.org/en/id/315</uri>
				
			</author>
			
			<link rel="alternate" type="text/html" href="http://www.riviti.org/en/article/326/converting-to-and-from-object-and-collection-initializers"/>
			
			
			
			

			<title>Converting to and from object and collection initializers</title>
			
				<summary>In another &quot;most complicated yet&quot; code action, I feel the need to explain it to myself. So here you go too.</summary>
				<content type="html">&#60;p&#62;Initializers are useful for two reasons: They offer a compact way to initialize an object in a compact way and they do so using a single atomic assignment to the variable holding the object after construction. They also offer a way to initialize fields of an object without giving it a name, since the initializer is usable as an expression.&#60;/p&#62;&#60;p&#62;The goal of this weeks GSoC work is to be able to convert between initializers and normal assignments and Add() calls (on Collections).&#60;/p&#62;&#60;p&#62;Analyzing this properly is a little difficult, because the initializer construct is fairly flexible. An object initializer can for an object with properties or fields initialize those with a nested initializer, which can either be an instance creation followed by an initializer, or just an initializer. Taken from the &#60;a title=&#34;The C# Version 4.0 Specification&#34; href=&#34;http://go.microsoft.com/fwlink/?LinkId=199552&#34;&#62;C# Specification, Version 4&#60;/a&#62;:&#60;/p&#62;&#60;blockquote&#62;&#60;p&#62;The following class represents a point with two coordinates:&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; public class Point&#60;br /&#62; {&#60;br /&#62;    int x, y;&#60;/pre&#62;&#60;pre class=&#34;Code&#34;&#62;    public int X { get { return x; } set { x = value; } }&#60;br /&#62;    public int Y { get { return y; } set { y = value; } }&#60;br /&#62; }&#60;/pre&#62;&#60;p&#62;An instance of Point can be created and initialized as follows:&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; Point a = new Point { X = 0, Y = 1 };&#60;/pre&#62;&#60;p&#62;which has the same effect as&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; Point __a = new Point();&#60;br /&#62; __a.X = 0;&#60;br /&#62; __a.Y = 1; &#60;br /&#62; Point a = __a;&#60;/pre&#62;&#60;p&#62;where __a is an otherwise invisible and inaccessible temporary variable. The following class represents a rectangle created from two points:&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; public class Rectangle&#60;br /&#62; {&#60;br /&#62;    Point p1, p2;&#60;/pre&#62;&#60;pre class=&#34;Code&#34;&#62;    public Point P1 { get { return p1; } set { p1 = value; } }&#60;br /&#62;    public Point P2 { get { return p2; } set { p2 = value; } }&#60;br /&#62; }&#60;/pre&#62;&#60;p&#62;An instance of Rectangle can be created and initialized as follows:&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; Rectangle r = new Rectangle {&#60;br /&#62;    P1 = new Point { X = 0, Y = 1 },&#60;br /&#62;    P2 = new Point { X = 2, Y = 3 }&#60;br /&#62; };&#60;/pre&#62;&#60;p&#62;which has the same effect as&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; Rectangle __r = new Rectangle();&#60;br /&#62; Point __p1 = new Point();&#60;br /&#62; __p1.X = 0;&#60;br /&#62; __p1.Y = 1;&#60;br /&#62; __r.P1 = __p1;&#60;br /&#62; Point __p2 = new Point();&#60;br /&#62; __p2.X = 2;&#60;br /&#62; __p2.Y = 3;&#60;br /&#62; __r.P2 = __p2; &#60;br /&#62; Rectangle r = __r;&#60;/pre&#62;&#60;p&#62;where __r, __p1 and __p2 are temporary variables that are otherwise invisible and inaccessible.&#60;/p&#62;&#60;p&#62;If Rectangle’s constructor allocates the two embedded Point instances&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; public class Rectangle&#60;br /&#62; {&#60;br /&#62;    Point p1 = new Point();&#60;br /&#62;    Point p2 = new Point();&#60;/pre&#62;&#60;pre class=&#34;Code&#34;&#62;    public Point P1 { get { return p1; } }&#60;br /&#62;    public Point P2 { get { return p2; } }&#60;br /&#62; }&#60;/pre&#62;&#60;p&#62;the following construct can be used to initialize the embedded Point instances instead of assigning new instances:&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; Rectangle r = new Rectangle {&#60;br /&#62;    P1 = { X = 0, Y = 1 },&#60;br /&#62;    P2 = { X = 2, Y = 3 }&#60;br /&#62; };&#60;/pre&#62;&#60;p&#62;which has the same effect as&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; Rectangle __r = new Rectangle();&#60;br /&#62; __r.P1.X = 0;&#60;br /&#62; __r.P1.Y = 1;&#60;br /&#62; __r.P2.X = 2;&#60;br /&#62; __r.P2.Y = 3;&#60;br /&#62; Rectangle r = __r;&#60;/pre&#62;&#60;/blockquote&#62;&#60;p&#62;Now, that is not how I would expect the code to look, more likely something like:&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; Rectangle r = new Rectangle();&#60;br /&#62; r.P1.X = 0;&#60;br /&#62; r.P1.Y = 1;&#60;br /&#62; r.P2.X = 2;&#60;br /&#62; r.P2.Y = 3;&#60;/pre&#62;&#60;p&#62;However, both variants need to be supported, because when converting from an initializer to a set of assignments, we need to generate the strictly equivalent code (or we risk introducing bugs, in this case probably race conditions due to loosing atomicity). We then want to be able to convert that back to the original form.&#60;/p&#62;&#60;p&#62;Another interesting case is this:&#60;/p&#62;&#60;pre class=&#34;Code&#34;&#62; Rectangle r = new Rectangle();&#60;br /&#62; r.P1.X = 0;&#60;br /&#62; r.P1 = new Point() { X = 2, Y = 6 }&#60;br /&#62; r.P1.Y = 1;&#60;br /&#62; r.P2.X = 2;&#60;br /&#62; r.P2.Y = 3;&#60;/pre&#62;&#60;p&#62;Which admittedly doesn&#38;#39;t really make sense, but still needs to be handled in some way (probably by ignoring the entire initialization and not consider it convertible or by stopping after the second line).&#60;/p&#62;&#60;p&#62;Add to this the fact that to not generate incorrect code (or show the action where it &#60;em&#62;cannot&#60;/em&#62; generate correct code) dependencies between statements need to be taken into account, the fact that initializers can be nested and the fact that individual statements initializing properties can themselves be initializers, you get to a point where there are a lot of cases to keep track of.&#60;/p&#62;&#60;p&#62;Alright, on to coding. More details once it&#38;#39;s done :)&#60;/p&#62;</content>
			
			
			
			
				
			<category term="article" scheme="http://zotonic.com/id/category" />
		</entry>
	


	
		
	
		<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xml:lang="en">
			<id>http://www.riviti.org/en/id/323</id>
			<updated>2012-08-19T16:28:28+02:00</updated>
			<published>2012-06-11T09:47:00+02:00</published>
			<author>
				
				<name>Simon Lindgren</name>
				<uri>http://www.riviti.org/en/id/315</uri>
				
			</author>
			
			<link rel="alternate" type="text/html" href="http://www.riviti.org/en/article/323/iterate-via-foreach"/>
			
			
			
			

			<title>Iterate via foreach</title>
			
				<summary>One part of my GSoC 2012 projects is to implement the &quot;Iterate via foreach&quot; code action. It will be the most complicated code action I have made so far, because there are some details in how it should work that are not obvious which I need to figure out.</summary>
				<content type="html">&#60;p&#62;The foreach statement can be used to iterate over any type which implements the IEnumerable interface, which in .NET it pretty much any container type as well as results from language integrated queries and from member functions classes.&#60;/p&#62;&#60;p&#62;Any expression to be iterated needs result in an object implementing IEnumerable so the places where the action should work depends on the places where such an expression can be extracted. Below are some examples of these cases (locations where the action should work is marked in &#60;strong&#62;bold&#60;/strong&#62;):&#60;/p&#62;&#60;pre&#62;using System.Collections.Generic;&#60;br /&#62;class A&#60;br /&#62;{&#60;br /&#62;    public static IEnumerable&#38;lt;int&#38;gt; F ()&#60;br /&#62;    {&#60;br /&#62;        var&#60;strong&#62; list&#60;/strong&#62; =&#60;strong&#62; new List&#38;lt;int&#38;gt; ();&#60;/strong&#62;&#60;br /&#62;        return list;&#60;br /&#62;    }&#60;br /&#62;&#60;br /&#62;    public IList&#38;lt;int&#38;gt; GetInts()&#60;br /&#62;    {&#60;br /&#62;        return new List&#38;lt;int&#38;gt;();&#60;br /&#62;    }&#60;br /&#62;}&#60;br /&#62;&#60;br /&#62;class B&#60;br /&#62;{&#60;br /&#62;    void F ()&#60;br /&#62;    {&#60;br /&#62;        var &#60;strong&#62;list&#60;/strong&#62; =&#60;strong&#62; A.F ()&#60;/strong&#62;;&#60;br /&#62;        var &#60;strong&#62;zeroes&#60;/strong&#62; =&#60;br /&#62;            &#60;strong&#62;from item in list&#60;/strong&#62;&#60;br /&#62;&#60;strong&#62;            where item == 0&#60;/strong&#62;&#60;br /&#62;&#60;strong&#62;            select item&#60;/strong&#62;;&#60;br /&#62;    }&#60;br /&#62;}&#60;br /&#62;&#60;br /&#62;class C&#60;br /&#62;{&#60;br /&#62;    void F ()&#60;br /&#62;    {&#60;br /&#62;        &#60;strong&#62;&#60;/strong&#62;&#60;strong&#62;A.F ()&#60;/strong&#62;;&#60;br /&#62;        var a = new A();&#60;br /&#62;    }&#60;br /&#62;}&#60;/pre&#62;&#60;p&#62;At this point I am not sure about the behaviour of the action in some of the cases. For example, when doing the action on a variable, it is reasonable to iterate over the variable. But what about iteration over expressions? Enumeration can be slow, so double enumerations should be avoided. Two examples of how it should look after executing the action (for classes B and C):&#60;/p&#62;&#60;pre&#62;class B&#60;br /&#62;{&#60;br /&#62;    void F ()&#60;br /&#62;    {&#60;br /&#62;        var list = A.F ();&#60;br /&#62;        foreach (var item in list) {&#60;br /&#62;&#60;br /&#62;        }&#60;br /&#62;        var &#60;strong&#62;zeroes&#60;/strong&#62; =&#60;br /&#62;            &#60;strong&#62;from item in list&#60;/strong&#62;&#60;br /&#62;&#60;strong&#62;            where item == 0&#60;/strong&#62;&#60;br /&#62;&#60;strong&#62;            select item&#60;/strong&#62;;&#60;br /&#62;    }&#60;br /&#62;}&#60;br /&#62;&#60;br /&#62;class C&#60;br /&#62;{&#60;br /&#62;    void F ()&#60;br /&#62;    {&#60;br /&#62;        foreach (var item in A.F()) {&#60;br /&#62;&#60;br /&#62;        }&#60;br /&#62;        var a = new A ();&#60;br /&#62;    }&#60;br /&#62;}&#60;/pre&#62;&#60;p&#62;How I think it should work may not be obvious just from these examples, but they nevertheless represent my current thinking of how it should behave and are subject to change. This should be implemented by the end of the week, suggestions welcome :)&#60;/p&#62;</content>
			
			
			
			
				
			<category term="article" scheme="http://zotonic.com/id/category" />
		</entry>
	


	
		
	
		<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xml:lang="en">
			<id>http://www.riviti.org/en/id/316</id>
			<updated>2012-06-19T15:59:46+02:00</updated>
			<published>2012-04-30T00:51:00+02:00</published>
			<author>
				
				<name>Simon Lindgren</name>
				<uri>http://www.riviti.org/en/id/315</uri>
				
			</author>
			
			<link rel="alternate" type="text/html" href="http://www.riviti.org/en/article/316/gsoc-2012"/>
			
			
			
			

			<title>GSoC 2012</title>
			
				<summary>I got accepted into Google Summer of Code 2012, with a code analysis project for MonoDevelop and NRefactory. Since there is a rumour that a lot of open source people have blogs, I have decided I&#39;m going to try that myself.</summary>
				<content type="html">&#60;p&#62;The project is conceptually simple: I am going to implement a set of detections for code issues and related (and unrelated) code actions to fix them. To help me I have my mentor &#60;a title=&#34;Mike Krüger&#38;#39;s blog&#34; href=&#34;http://mikemdblog.blogspot.se&#34;&#62;Mike Krüger&#60;/a&#62; and the &#60;a title=&#34;The NRefactory code repository&#34; href=&#34;https://github.com/riviti/NRefactory&#34;&#62;NRefactory&#60;/a&#62; library which was created for this very purpose.&#60;/p&#62;&#60;p&#62;The NRefactory library is shared between both &#60;a title=&#34;The MonoDevelop project site&#34; href=&#34;http://monodevelop.com&#34;&#62;MonoDevelop&#60;/a&#62; and &#60;a title=&#34;The SharpDevelop project site&#34; href=&#34;http://www.icsharpcode.net/opensource/sd/&#34;&#62;SharpDevelop&#60;/a&#62;, so any refactoring operations using the library are usable in both applications. I will only use MonoDevelop during development, since SharpDevelop does not run on Linux.&#60;/p&#62;&#60;p&#62;Next up is looking through the APIs for refactoring in NRefactory. It is a large library though the parts that are directly necessary to know are just a part of it. Have some tea or something while I figure it out, and I&#38;#39;ll post again when I have. ;)&#60;/p&#62;&#60;p&#62;More information about my project can be seen at the &#60;a title=&#34;Code issue detection and code actions for MonoDevelop and NRefactory&#34; href=&#34;http://www.google-melange.com/gsoc/project/google/gsoc2012/simonlindgren/19002&#34;&#62;Google Melange project page&#60;/a&#62;.&#60;/p&#62;</content>
			
			
			
			
				
			<category term="article" scheme="http://zotonic.com/id/category" />
		</entry>
	


	
	
</feed>
