<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users491</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/491" rel="self"/>
  <title>rikkus</title>
  <updated>Sun Dec 13 12:16:06 -0800 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor384916</id>
    <published>2009-12-13T12:16:06-08:00</published>
    <title>[C#] On Line splitting optimization</title>
    <content type="html">&lt;p&gt;How about a more standard word wrapping algorithm? The simple 'minimum length' algorithm mentioned here &lt;a href="http://en.wikipedia.org/wiki/Word_wrap" target="_blank"&gt;http://en.wikipedia.org/wiki/Word_wrap&lt;/a&gt; should suit.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1122-line-splitting-optimization/refactors/384916" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor157304</id>
    <published>2009-05-13T08:49:49-07:00</published>
    <title>[C#] On Generic Lists of Different Types</title>
    <content type="html">&lt;p&gt;Implement Status.Load as you see fit. Obviously you'll want to load User from the &amp;quot;user&amp;quot; element contained within.
&lt;/p&gt;

&lt;pre&gt;public IEnumerable&amp;lt;IStatus&amp;gt; ResponseHandler(string responseText, string unused)
{
    return ResponseHandler(responseText);
}

public IEnumerable&amp;lt;IStatus&amp;gt; ResponseHandler(string responseText)
{
    var element = XElement.Parse(responseText);

    if (element.Name == &amp;quot;statuses&amp;quot;)
    {
        foreach (var statusElement in element.Descendants(&amp;quot;status&amp;quot;))
        {
            yield return Status.Load(statusElement);
        }
    }
    else
    {
        yield return Status.Load(element);
    }
}&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/862-generic-lists-of-different-types/refactors/157304" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor156436</id>
    <published>2009-05-07T15:38:05-07:00</published>
    <title>[C#] On Find random point inside a tree of geometrical object</title>
    <content type="html">&lt;p&gt;The easiest way to choose a random subvolume would probably be as shown. Also note that I don't recognise Random.NextDouble() - In .NET that's not a static method, for good reason: You generally have to initialise a Random object with a seed before using it.&lt;/p&gt;

&lt;pre&gt;Subvolumes.Skip(random.Next(Subvolumes.Count)).First()
&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/859-find-random-point-inside-a-tree-of-geometrical-object/refactors/156436" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor152303</id>
    <published>2009-03-21T16:28:00-07:00</published>
    <title>[C#] On Method to read Network stream from Socket</title>
    <content type="html">&lt;p&gt;If you want to perform an HTTP request, you should use WebRequest:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx" target="_blank"&gt;http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your code is incorrect because it claims to 'read fully' but may, even if there is no error, return less than the 'full' data.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/794-method-to-read-network-stream-from-socket/refactors/152303" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor67950</id>
    <published>2008-11-11T20:17:38-08:00</published>
    <title>[C#] On DirectoryInfo.CopyTo</title>
    <content type="html">&lt;p&gt;Here's a quick sketch of some improvements I'd make. This probably has lots of problems remaining, for example, I didn't deal with links either!&lt;/p&gt;

&lt;p&gt;Cheers,
&lt;br /&gt;Rik&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;namespace Whatever
{
    public static class FileUtils
    {
        public void CopyDirectory
        (
            string sourcePath,
            string destinationPath,
            ExistingDestinationDirectoryPolicy existingDestinationDirectoryPolicy
        )
        {
            if (!Directory.Exists(sourcePath))
            {
                throw new DirectoryNotFoundException(string.Format(&amp;quot;Source directory not found: {0}&amp;quot;, sourcePath));
            }

            if (Directory.Exists(destinationPath))
            {
                bool cancel = false;

                ApplyExistingDestinationDirectoryPolicy(destinationPath, existingDestinationDirectoryPolicy, ref cancel);

                if (cancel)
                {
                    return;
                }
            }
            else
            {
                Directory.CreateDirectory(destinationPath);
            }

            CopyDirectories(sourcePath, existingDestinationDirectoryPolicy);
            CopyFiles(sourcePath, destinationPath);
        }

        private void CopyDirectories(string sourceDirectoryPath, ExistingDestinationDirectoryPolicy existingDestinationDirectoryPolicy)
        {
            foreach (var directoryPath in Directory.GetDirectories(sourceDirectoryPath))
            {
                CopyDirectory
                (
                    directoryPath,
                    Path.Combine(sourceDirectoryPath, Path.GetFileName(directoryPath)),
                    existingDestinationDirectoryPolicy
                );
            }
        }

        private static void CopyFiles(string sourceDirectoryPath, string destinationDirectoryPath)
        {
            foreach (var filePath in Directory.GetFiles(sourceDirectoryPath))
            {
                File.Copy(filePath, Path.Combine(Path.GetFileName(filePath), destinationDirectoryPath));
            }
        }

        private static void ApplyExistingDestinationDirectoryPolicy
            (
                string destinationPath,
                ExistingDestinationDirectoryPolicy existingDestinationDirectoryPolicy,
                ref bool cancel
            )
        {
            switch (existingDestinationDirectoryPolicy)
            {
                case ExistingDestinationDirectoryPolicy.DeleteExisting:
                    Directory.Delete(destinationPath, true /* recursive */);
                    break;

                case ExistingDestinationDirectoryPolicy.CancelOnExisting:
                    cancel = true;
                    break;

                case ExistingDestinationDirectoryPolicy.ThrowOnExisting:
                    throw new DirectoryExistsException(destinationPath);

                default:
                    // Carry on!
                    break;
            }
        }
    }
}

&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/600-directoryinfo-copyto/refactors/67950" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor61707</id>
    <published>2008-11-06T11:31:10-08:00</published>
    <title>[C#] On Sorting by dependencies</title>
    <content type="html">&lt;p&gt;The real world scenario is adding user-defined functions to a SQL Server database.&lt;/p&gt;

&lt;p&gt;The functions must be created in order, such that when a function is created, the ones upon which it depends (those which it calls) have already been created.&lt;/p&gt;

&lt;p&gt;For example: I may write a function Foo which calls another function Bar. Function Bar must be created before I can create Foo, or the compiler will complain.&lt;/p&gt;

&lt;p&gt;The data structure expected as input by this (genericised) algorithm is a set of references to nodes, each paired with a collection of references to nodes upon which it depends.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/579-sorting-by-dependencies/refactors/61707" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor60656</id>
    <published>2008-11-05T12:47:54-08:00</published>
    <title>[Ruby] On xml rss feed</title>
    <content type="html">&lt;p&gt;It looks like you're concatenating plain text and HTML. Perhaps you should make your description HTML and embed the link inside it using standard XML tools?&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/577-xml-rss-feed/refactors/60656" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor60631</id>
    <published>2008-11-05T12:16:02-08:00</published>
    <title>[C#] On Sorting by dependencies</title>
    <content type="html">&lt;p&gt;Simple demo program.&lt;/p&gt;

&lt;pre&gt;foreach (var item in
    DependencySorter&amp;lt;int&amp;gt;.Sort
    (
        new Dictionary&amp;lt;int, IEnumerable&amp;lt;int&amp;gt;&amp;gt;
        {
            {5, new int[] {4, 3, 2}},
            {2, new int[] {1}},
            {1, new int[] {}},
            {4, new int[] {3}},
            {3, new int[] {1, 2}}
        }
    )
)
{
    Console.WriteLine(item);
}&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/579-sorting-by-dependencies/refactors/60631" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code579</id>
    <published>2008-11-05T12:14:53-08:00</published>
    <updated>2008-11-06T11:31:11-08:00</updated>
    <title>[C#] Sorting by dependencies</title>
    <content type="html">&lt;p&gt;This code sorts a collection by the dependencies of its contents.&lt;/p&gt;

&lt;p&gt;It is passed a dictionary mapping a key to a set of keys - the dependencies of that key.&lt;/p&gt;

&lt;p&gt;I knocked up this code quickly and realised as I wrote it that it was likely to blow the stack on a large data set due to its recursive nature. I'd try to make it tail recursive, but the C# compiler doesn't take advantage of that yet, so it should probably be iterative. I'd be interested to see any improved solution.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;public static class DependencySorter&amp;lt;T&amp;gt;
{
    public static IEnumerable&amp;lt;T&amp;gt; Sort(Dictionary&amp;lt;T, IEnumerable&amp;lt;T&amp;gt;&amp;gt; dependencies)
    {
        var visited = new Dictionary&amp;lt;T, bool&amp;gt;();

        foreach (var key in dependencies.Keys)
        {
            visited[key] = false;
        }

        foreach (var key in dependencies.Keys)
        {
            foreach (var ordered in Search(key, dependencies, visited))
                yield return ordered;
        }
    }

    private static IEnumerable&amp;lt;T&amp;gt; Search
    (
        T key,
        IDictionary&amp;lt;T, IEnumerable&amp;lt;T&amp;gt;&amp;gt; dependencies,
        IDictionary&amp;lt;T, bool&amp;gt; visited
    )
    {
        if (!visited[key])
        {
            visited[key] = true;

            foreach (T value in dependencies[key])
            {
                foreach (var ordered in Search(value, dependencies, visited))
                {
                    yield return ordered;
                }
            }

            yield return key;
        }
    }
}&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/579-sorting-by-dependencies" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor53093</id>
    <published>2008-10-07T12:13:01-07:00</published>
    <title>[C#] On Drop all Indexes in SQL Server</title>
    <content type="html">&lt;p&gt;I had to ask because often people want to recreate the indexes, in which case DBCC REINDEX is a much better idea.&lt;/p&gt;

&lt;p&gt;Here's my version, which is pretty much the same as yours, except uses a cursor as it's a little neater.&lt;/p&gt;

&lt;p&gt;You may want to add a check that you're not operating on sysdiagrams, as this logic will drops its indexes, too.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;## Transact-SQL [sql]

DECLARE @indexName VARCHAR(128)
DECLARE @tableName VARCHAR(128)

DECLARE [indexes] CURSOR FOR
	
	SELECT		[sysindexes].[name] AS [Index],
			[sysobjects].[name] AS [Table]
	
	FROM		[sysindexes]
	
	INNER JOIN	[sysobjects]
	ON		[sysindexes].[id] = [sysobjects].[id]
	
	WHERE		[sysindexes].[name] IS NOT NULL
	AND		[sysobjects].[type] = 'U'

OPEN [indexes]

FETCH NEXT FROM [indexes] INTO @indexName, @tableName

WHILE @@FETCH_STATUS = 0
BEGIN
	PRINT 'DROP INDEX [' + @indexName + '] ON [' + @tableName + ']'

	FETCH NEXT FROM [indexes] INTO @indexName, @tableName
END

CLOSE		[indexes]
DEALLOCATE	[indexes]&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/522-drop-all-indexes-in-sql-server/refactors/53093" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor32178</id>
    <published>2008-09-25T16:59:48-07:00</published>
    <title>[PHP] On Mortgage Calculator</title>
    <content type="html">&lt;p&gt;You're confusing the terms 'refactor' and 'reimplement'&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/505-mortgage-calculator/refactors/32178" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor32177</id>
    <published>2008-09-25T16:56:31-07:00</published>
    <title>[VB.NET] On Remove the Evil GOTO</title>
    <content type="html">&lt;p&gt;This kind of error handling is idiomatic in VB.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/506-remove-the-evil-goto/refactors/32177" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4642</id>
    <published>2008-04-02T16:05:44-07:00</published>
    <title>[C#] On Common StProc Code</title>
    <content type="html">&lt;p&gt;To be honest I haven't looked at this site much as I just added the C# feed to my feed reader when I found it - and then forgot about it, so I'm not really sure about ratings.&lt;/p&gt;

&lt;p&gt;Your code looks good to me now - the only thing left that's bothering me is the naming of various things ('restore' should be perhaps 'reverse', there are too many abbrvtns for my liking, etc.)&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/273-common-stproc-code/refactors/4642" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4640</id>
    <published>2008-04-02T15:11:49-07:00</published>
    <title>[C#] On Common StProc Code</title>
    <content type="html">&lt;p&gt;See the comments by alazela here: &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1742482&amp;amp;SiteID=1" target="_blank"&gt;http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1742482&amp;amp;SiteID=1&lt;/a&gt; - it looks like Prepare isn't going to do anything when you're calling a stored procedure, so you might as well take it out.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/273-common-stproc-code/refactors/4640" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4637</id>
    <published>2008-04-02T15:00:07-07:00</published>
    <title>[C#] On Common StProc Code</title>
    <content type="html">&lt;p&gt;I'm not sure what the Prepare call does, so I'm afraid you'll have to see if someone else knows what should be done with it.
&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>rikkus</name>
      <email>rik@rikkus.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/273-common-stproc-code/refactors/4637" rel="alternate"/>
  </entry>
</feed>

