<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Not Rocket Science &#187; Development</title>
	<atom:link href="http://www.stum.de/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stum.de</link>
	<description>Random thoughts of neat disorder</description>
	<lastBuildDate>Thu, 05 Jan 2012 20:42:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Thoughts on .net in 2012</title>
		<link>http://www.stum.de/2012/01/05/thoughts-on-net-in-2012/</link>
		<comments>http://www.stum.de/2012/01/05/thoughts-on-net-in-2012/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 10:36:35 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=1086</guid>
		<description><![CDATA[One of the hot topics in 2011 was the rise of server-side JavaScript thanks to node.js, the future of Windows with WinRT, and a perceived exodus of high profile .net developers to Ruby on Rails or node.js, proclaiming that it's so much easier to get stuff up and running once you leave .net. Also, in [...]]]></description>
			<content:encoded><![CDATA[<p>One of the hot topics in 2011 was the rise of server-side JavaScript thanks to <a href="http://nodejs.org/">node.js</a>, the future of Windows with <a href="http://en.wikipedia.org/wiki/Windows_Runtime">WinRT</a>, and a perceived exodus of high profile .net developers to <a href="http://rubyonrails.org/">Ruby on Rails</a> or node.js, proclaiming that it's so much easier to get stuff up and running once you leave .net. Also, in the wake of BUILD conference, .net was already declared dead, which turned out to be a false alarm, it's only Silverlight that's dead (unless you are building a video player, because HTML5 Audio/Video <a href="https://p.twimg.com/AiWTysKCAAE0SzG.png:large">sucks and will continue to suck</a>).</p>
<p>Personally, I'm 100% convinced that .net is doing fine and will do fine for years to come. We just have to realize though where .net (and Java, for that matter) are used: Inside Enterprises. There is not a single day where I don't see someone pulling off some amazing stuff on Twitter, usually built in JavaScript, CoffeeScript or Ruby. Looking at people pulling off amazing stuff in .net is always a big undertaking though, because there are pretty much no big non-Microsoft .net projects that are "hip". I think <a href="http://mvc.fubu-project.org/">FubuMVC</a>, <a href="http://caliburnmicro.codeplex.com/">Caliburn.Micro</a> and <a href="http://www.bouncycastle.org/csharp/">Bouncy Castle</a> are the only three that I could mention off-hand. But then again, I see amazing stuff done in .net almost every day, except that no one else outside of my work environment will ever see it. Talking to other .net developers in other companies usually yields similar experiences - people are pulling off some seriously fantastic stuff, but it will never ever leave the company it was created in. The open source environment in .net has improved since I <a href="http://www.stum.de/2007/11/21/open-source-and-net-not-really-an-ideal-partnership/">blogged about it 4 years ago</a>, but it is still only a fraction of what node.js or ruby has. Just look at the list of most watched C# projects on <a href="https://github.com/languages/C%23/most_watched">GitHub</a> or <a href="http://www.codeplex.com/site/search?query=&#038;sortBy=PageViews&#038;tagName=%2cC%23%2c&#038;licenses=|&#038;refinedSearch=true">CodePlex</a> and think how long ago some .net project really made headlines.</p>
<p>That doesn't mean it's a bad environment to work in, quite the opposite actually. If you look at popular libraries for node.js or ruby, you will often find that the BCL or Microsoft's .net Libraries already have something built-in, and it's usually rock solid and fast. You will find Microsoft supplying the big features that everyone uses or will use - LINQ, LINQ 2 Sql, Dynamics, TPL, Async/Await, ASP.net MVC are just a few I can think of. Combine the "No one ever got fired for choosing Microsoft" mentality in big enterprises with the fact that the vast majority of software development is done in enterprises, and you see why this attitude exists and why Microsoft has to deliver.</p>
<p>There is no denying that Microsoft had quite a few blunders, of course. WPF, EF and Workflow were downright garbage in .net 3.x, WCF is overly complex for simple scenarios and Velocity... let's just say that I've never heard anyone say anything good about it, and looking at it's API I doubt I'll have good things to say about it. Then, there are signs of the more than 10 year legacy that .net already has, together with the desire for backwards compatibility. System.Configuration is horrible because you cannot just create a Configuration object from a string or any other mechanism, that thing is hard wired to look at a file on your hard drive, which makes it really inflexible for modern, multi-tenant deployments. Have you tried parsing JSON in .net? How many different JSON Libraries/Deserializers do we have as part of the Framework? And how many did Silverlight add? And yet, people mostly prefer the <a href="http://james.newtonking.com/pages/json-net.aspx">Newtonsoft JSON.net</a>. Have you worked with the ASP.net Provider Model, specifically Membership Providers? Only a mother could love the API of that thing. And speaking of ASP.net, have you ever tried catching an "Attachment too large" error? The error that gets thrown twice, once in IIS before it reaches your app and then in ASP.net itself without a clear way to associate it with the request?</p>
<p>I could go on and on and on about things that are seriously broken in .net, and I haven't even started about testability yet (there is a very good reason for products like <a href="http://www.typemock.com/">TypeMock</a> to exist). It is still overall a great experience, but it is frustrating that we can't do much about the shortcomings of it. To work around the stupid file handling and Configuration madness in ASP.net, we would need a whole different web server since we can't just patch IIS or the ASP.net core components thanks to their closed source nature. Of course, running <a href="http://www.mono-project.com/Main_Page">Mono</a> on <a href="http://www.mono-project.com/FastCGI_Nginx">nginx</a> is a way to work around this, but enterprises won't approve of such a setup and if it breaks, your behind may be on the line.</p>
<p>I've often experimented with different setups, only to realize that I really like C#. I like its static nature which usually gives good, understandable compiler errors rather than cryptic runtime errors (face it, rails can be a pain to debug). I like that I can mix in dynamics for non-critical things like view models. I like that the Microsoft-provided libraries are mostly good. I like that we can buy support and that there are tons of resources about most topics, since everyone uses pretty much the same stack. I like that they keep backwards compatibility so sacred, even if it means that there is a lot of obsolete stuff in the API (like System.Collections.ArrayList - there is no reason whatsoever to use it. If you need a container for multiple types, fall back to List&lt;object&gt;.</p>
<p>On the other hand, it can be a really frustrating experience when you run against a wall that you can't climb because of its closed source nature and Microsofts reluctance to fix things (Take WPF in .net 3.5 as an example: Things were reported but not fixed. Then 4.0 comes out with Visual Studio, they run into the same problem (e.g., the scrolling popup menus) and HotFix it. That was the moment I've given up on WPF forever) until a major release, if ever. Sometimes it is a really tough sell to use other non-microsoft components like <a href="http://www.rabbitmq.com/">RabbitMQ</a> instead of MSMQ (Search for MSMQ on MSDN and look on the big highlighted article - to me, that's proof that Microsoft doesn't care about their own message queue anymore), or <a href="http://www.nservicebus.com/">NServiceBus</a>, or a sane ORM instead of Entity Framework.</p>
<p>Most applications start out as simple CRUD applications, and it is usually a lot quicker to start them in node or rails. However, simple CRUD applications usually evolve into more sophisticated pieces, requiring more business logic and safeguards in different places, and here I must say that .net shines - in the long run, its strengths play out well.</p>
<p>However, I really think that we can do better in the web world, much better. Chad Myers <a href="http://lostechies.com/chadmyers/2011/12/30/sweet-sweet-vindication/">had an interesting posting</a>, and even though it was <a href="http://wekeroad.com/2012/01/03/rails-has-turned-me-into-a-cannibalizing-idiot/">ripped to pieces by Rob Connery</a>, it still holds a lot of truth. Web Applications are unnecessarily complex. I will write a follow up posting with some thoughts about this.</p>
<p>My conclusion would be that .net is overall doing perfectly well. It is used by tons of companies for very important apps, but it was never aimed to be a "hip" language. Microsoft is deeply rooted in the Enterprise, they are pretty much the exact opposite of Apple. That doesn't mean that they do a worse job at it, it just means you won't see as much about it since enterprises like to keep their "secrets" internal and err on the side of safety/secrecy. I have no doubt that there will be .net jobs for the next decade and beyond, while some other language will come and go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2012/01/05/thoughts-on-net-in-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thoughts on Growl 1.3 being a paid app now</title>
		<link>http://www.stum.de/2011/10/07/growl-1-3-is-a-paid-app-now/</link>
		<comments>http://www.stum.de/2011/10/07/growl-1-3-is-a-paid-app-now/#comments</comments>
		<pubDate>Fri, 07 Oct 2011 17:46:53 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.stum.de/2011/10/07/growl-1-3-is-a-paid-app-now/</guid>
		<description><![CDATA[I just browsed the Mac AppStore and saw Growl on sale for $2. At first I wanted to give a 1-Star Review and call it a scam, selling free software for money. Then I went to the Growl Homepage and saw that this is true. Now, I don't have a problem with them charging now, [...]]]></description>
			<content:encoded><![CDATA[<p>I just browsed the Mac AppStore and saw Growl on sale for $2. At first I wanted to give a 1-Star Review and call it a scam, selling free software for money. Then I went to the Growl Homepage and saw that this is true.</p>
<p>Now, I don't have a problem with them charging now, it's their right. And $2 really isn't much, I bought it immediately since I use <a href="http://www.stum.de/2011/01/09/a-little-eggtimer-script-using-growl/">growlnotify</a> a lot.</p>
<p>What is prompting me to write this blog posting: I wonder what this means for third party app developers?</p>
<p>When Growl was free (1.2.2 still is), it was a no-brainer to implement it in your App. You could tell your user to download it, or you just snuck it in with your apps (much to the dismay of the developers, who had to put up a notice on their web site). Now, your users have to make an additional $2 purchase regardless if your app is free or paid. And due to the way the Mac AppStore works, you can't just decide to pay the $2 for your users and bundle Growl with your app - even if you include a $2 git certificate, your users now need an iTunes account.</p>
<p>Now, in an ideal world where every user has an iTunes/App Store account this isn't an issue, but I still wonder if this decision will lead to everyone reinventing the wheel and adding their own notification system again? (Or forking/distributing Growl 1.2.2 for all of eternity or until a Mac OS X version that doesn't support it comes up).</p>
<p>Again, I don't want to speak ill of developers who created an amazing piece of software and now charge a minimal amount for it. I just wonder if this may cause issues for us Third Party app developers?</p>
<p>In an ideal world, Tim Cook will announce that Mac OS X 10.8 comes bundled with Growl, that all Apple Apps support it, that GNTP becomes an integral part for distributed notifications, that it integrates into iOS' notifications system etc. pp. Your move, Apple.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2011/10/07/growl-1-3-is-a-paid-app-now/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Thoughts on OR/M</title>
		<link>http://www.stum.de/2011/09/28/thoughts-on-orm-2/</link>
		<comments>http://www.stum.de/2011/09/28/thoughts-on-orm-2/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 03:39:32 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.stum.de/2011/09/28/thoughts-on-orm-2/</guid>
		<description><![CDATA[It's hard to imagine a world without OR/Ms anymore. The old days of making a query, getting a reader, looping through it, getting every column into an object... I'm a Linq2Sql user. I like it's relatively easy setup and good flexibility, and it has a really good query generator. However, I'm running into some issues [...]]]></description>
			<content:encoded><![CDATA[<p>It's hard to imagine a world without OR/Ms anymore. The old days of making a query, getting a reader, looping through it, getting every column into an object...</p>
<p>I'm a Linq2Sql user. I like it's relatively easy setup and good flexibility, and it has a really good query generator. However, I'm running into some issues with Unit Testing it, because you can't really emulate all the features that it has (EntityRefs with a composite Key or fields that are DbGenerated are a nightmare). Since it is a one-off technology for SQL Server, it doesn't work with any other database (not even SQL Compact 4, which sucks as it would be the perfect Unit Test drop in).</p>
<p><a href="http://seldo.com/weblog/2011/08/11/orm_is_an_antipattern">Some people</a> are saying that OR/Ms are an anti-pattern, to which Ayende had an <a href="http://ayende.com/blog/105473/is-or-m-an-anti-pattern">interesting reply</a>. I don't fully agree with any of them. I think that OR/M is solving two different problems, one that it shouldn't solve.</p>
<p>The first problem is getting data from and to the database - the second problem is the mapping of the result to objects. The second problem is a good one, the first one is not a problem and thus doesn't require a solution.</p>
<p>Somehow, people seem to frown SQL, as if it were some evil or outdated concept. You know what? SQL is the language of your database, so you better learn it. Oh sure, it's nice to chain a gazillion Where/Select/ToDictionary/GroupBy calls together and don't have to worry about coming up with the SQL yourself. It's not so nice that the chance to produce a SELECT N+1 is pretty high. When was the last time you looked at the SQL that your OR/M generates and optimized it?</p>
<p>Speaking of optimizations: When was the last time your DBA improved your OR/M Code? Most DBAs I know speak one language very well, and that's the SQL Dialect of their database. They know these hidden tricks and tweaks, they can tweak the query plan and come up with ways to speed up your queries, thus lowering the load and therefore the cost of the system. I don't know many DBAs who would look at a piece of C# code and suggest moving the select outside of the foreach loop in order to turn a SELECT N+1 into a single select. They know the difference between the different JOIN Types (I use LEFT JOIN 99.99% of the time - who knows if there are better ways?) and are able to suggest putting in a READ UNCOMMITTED into a query that is okay to retreive slightly stale data. When was the last time you tweaked the TABLE LOCK setting on your OR/M query?</p>
<p>Granted, most developers don't seem to have access to DBA people and thus have to wear a DBA hat quite often. But you know what? If you have to know about DB stuff anyway, you can as well learn SQL. Even though you may not get everything highly optimized, in the moment a DBA looks at it and optimizes stuff you just have to change your query and call it a day. Maybe you two decide to create a Stored Procedure instead, in which case your SELECT becomes an EXEC - big deal, not.</p>
<p>Yes, I am aware that some queries can get pretty harsh. I have a query with 14 subselects (mainly because it has to do paging on the database) and I am glad that I can write it in Linq2Sql, creating an IQueryable and passing it through a dozen methods that apply more Where clauses based on some ridiculously huge filter class a user passes in. Not having to generate that through StringBuilders or other stuff is a great thing.</p>
<p>But you know what? That query is the one anomaly in a sea of standard CRUD queries. I can write those CRUD queries in my sleep, blindfolded. And I can look at them and immediately understand what this thing is doing. I can read up the T-SQL manual, run it in SQL Management Studio while the SQL profiler is running, look at the Execution plan, tweak the query, put it on Stack Overflow to get some input - I am talking to the DB in a language it understands and have no limitations in how I tweak it.</p>
<p>Really, all I want is the Mapping. I want to write a (parameterized) SQL query, but I don't want to do the ExecuteReader/foreach loop crap, because that's stupid work. I want to be able to insert a Parent Row and it's child rows in one go, without having to use client-side generated GUIDs for the primary key so that I can add it to the child rows. I love the EntitySet/EntityRef stuff in Linq2Sql, and that's what I want. Create an Object and it's children, put it in the database and not worry about capturing the @@IDENTITY and populating it on the children. I don't want an OR/M, I want an Object Mapper.</p>
<p>Luckily, there seems to be a small (but truly existing) movement into it, <a href="http://code.google.com/p/dapper-dot-net/">Dapper</a> and <a href="https://github.com/robconery/massive">Massive</a> being two of the better known ones. Massive uses the new dynamic functionality in .net 4, a technology that could revolutionize OR/Ms once they start embracing it (rather than trying to stick to their Java roots or stay compatible with .net 2.0). I don't fully like how I have to create &quot;empty&quot; classes and it seems that it doesn't do &quot;real&quot; SQL but rather a DSL that looks like SQL. That would kinda suck.</p>
<p>Dapper is currently used as the OR/M for <a href="http://stackoverflow.com/">Stack Overflow</a>, It's very bare bones (doesn't manage your connection, no UPDATE helpers), has some not-so-nice syntax quirks (Having to pass IsAnsi = true when querying a non-unicode - but that's an edge case I'd argue) and although I have not yet had a chance to use it, if Jeff Atwood is willing to use it on the thing that puts food on his table and rock band songs onto his Xbox, I'd assume it's stable.</p>
<p>Granted, one can argue that you're not paying your developers to write code that can be written faster in an OR/M, a claim I'd like to dispute when looking at the total lifetime of an application including maintenance, but if you want a catchy, quotable sentence: Traditional OR/M is like WebForms. It's rapid at first, does stuff just well, may even have a fancy GUI to drag/drop your tables. But just like WebForms limits you tweaking the genrated markup or core functionality (&quot;I need my Data Grid to render the headers in a &lt;thead&gt; so that the jQuery TableSorter plugin works&quot;) and sometimes requires some hacks (&quot;I know, I just put some javascript that wraps the generated table header rows into a &lt;thead&gt;&quot;), an OR/M will put up some walls that you can't climb as well.</p>
<p>I think it's time for an OR/M that is to other OR/Ms what ASP.net MVC is to ASP.net WebForms: A Framework that allows and requires us to get deep into the guts, but does not limit the tweaking we can do, while still handling a lot of the stupid work, and I think it's great to see projects move into that direction. Just as MVC and WebForms both sit on top of an ASP.net core stack, ADO.net serves as a rock solid foundation for multiple philosophies of OR/M systems that can happily co-exist and serve both the people able and willing to write SQL and the people who'd rather spend money on an OR/M, profiling tools, and maintenance costs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2011/09/28/thoughts-on-orm-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>I would like Tech Books to be like this</title>
		<link>http://www.stum.de/2011/08/31/i-would-like-tech-books-to-be-like-this/</link>
		<comments>http://www.stum.de/2011/08/31/i-would-like-tech-books-to-be-like-this/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 03:49:08 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.stum.de/2011/08/31/i-would-like-tech-books-to-be-like-this/</guid>
		<description><![CDATA[I just received my copy of Using the HTML5 Filesystem API by Eric Bidelman and wanted to use it as an example of how I would like Tech Books to be: This book is specialized and concise. As you might see from the image, it comes it an well under 100 Pages and covers exactly [...]]]></description>
			<content:encoded><![CDATA[<p>I just received my copy of <a href="http://www.amazon.com/Using-HTML5-Filesystem-Eric-Bidelman/dp/1449309453/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1314760220&amp;sr=1-1">Using the HTML5 Filesystem API</a> by Eric Bidelman and wanted to use it as an example of how I would like Tech Books to be:</p>
<p><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="HTML5FS" border="0" alt="HTML5FS" src="http://www.stum.de/wp-content/uploads/2011/08/HTML5FS.jpg" width="600" height="463" /></p>
<p>This book is specialized and concise. As you might see from the image, it comes it an well under 100 Pages and covers exactly one topic.</p>
<p>I have only very limited time compared to the amount of technology that’s out there. The Web is evolving so fast, it’s hard to keep up with new developments. Information is scattered in blog posts, and it’s often hard to just find a cohesive front-to-back tutorial. Also, even though I own both a Kindle and an iPad, I prefer my books to be on dead trees.</p>
<p>Tech Books are often massive, 500 to 1000 page compendiums, and there are cases when it’s necessary. If you read the .net Specification, the C# Language Specification or David Flanagan’s excellent <a href="http://oreilly.com/catalog/9780596805531">JavaScript: The Definitive Guide</a>, it’s hard to get these under 500 pages, and that’s okay because they are compendiums about an entire language.</p>
<p>However, way too often I see books that have a lot of text but very little information. One of my Pet Peeves is the Introduction and Installation section. Many Web Books start with a 50 page history of the Internet, from CERN to HTML5, how the internet has revolutionized the world and what great things lie in front of us. Sorry, if I really want to know about the history of the Internet, I hit up <a href="http://en.wikipedia.org/wiki/Internet">Wikipedia</a>, just like the book author likely did when researching. The only interesting piece of History is the motivation behind a technology, answered by asking two questions: What Problem did the creator of a technology run into, and how is this technology attempting to solve it?</p>
<p>Same for the installation Section. I don’t need a 20 Page installation that’s outdated anyway when the book comes out, Give us the official Project Website and maybe a few gotchas, but we can take it from there.</p>
<p>Being able to read a book in an evening is a huge win because it already plants knowledge into our brain about what’s available. I might not have immediate need for a technology, but knowing what and how it does stuff means that the next time I run into a technical problem, I might think “Hey, I can use X to solve that!”.</p>
<p>The HTML5 Filesystem API book does just that. It has a small Intro and dives right into code examples. The API is simple, yet does introduce new objects into JavaScript. There is no example application per se (One could think that building an Address Book with uploadable Photos would be cool), presumably because it would take the focus away from the core. The code builds on top of each other though, from the creation of a file system to adding, deleting, uploading and remote getting files.</p>
<p>In fact, this book is good for another point: Tech Books are usually behind the applications they release. When a technology comes out, the early books are usually garbage, based on Beta/Prerelease versions with code examples that are slightly broken because RTM changed stuff. They don’t have many real world usage scenarios, often cover too much too shallow. This is even worse when the technology is an update (e.g., ASP.net MVC 3) and the early books are just previously released books (e.g., for ASP.net MVC 2) updated with some new features rather than redoing the entire book front to end with the new technology in mind.</p>
<p>The later books are usually too late – by the time they come out, the successor technology has already been announced, and people gained knowledge through blog posting, cursing at how bad the technology is documented.</p>
<p>Tech Books remind me a lot of Waterfall development, in that they simply take way too long to come to the market and don’t meet the needs of the customers. Writing a 500+ page tome simply takes time.</p>
<p>The agile version of that is what the above book demonstrates: The technology it’s discussing isn’t even out yet and may change, a fact that’s clearly stated in the book. However, because it’s only 80 or so pages, it’s quick to write and gives the reader enough knowledge about all aspects of the API, so that eventual changes should be fairly trivial to do (Eric also includes the link to the W3C Spec, so you can stay up to date). But even if they completely throw away and redo the API, it only wasted a small amount of my time – and my money. The book is $20 (cheaper on Amazon right now), so it’s not a massive investment at all, especially if I would calculate the time it would take me to scour through scattered blog postings as billable time.</p>
<p>Another great example is <a href="http://leanpub.com/nodebeginner">The Node Beginner Book</a>. It’s cheap ($5), comes in at about 60 pages, is cohesive and available right now (compared to some books that are scheduled to come out in 4 months or more). <a href="http://pragprog.com/book/tbcoffee/coffeescript">CoffeeScript: Accelerated JavaScript Development</a> is another excellent one. I heard that the Nuget folks are considering a book as well, and I would love to just get a book that covers how Nuget works internally, how the Server was written and how to interact with the Visual Studio plugin. Sure, I can look at the source code, but again, a concise and cohesive overview with samples and a reference part is all that’s really needed.</p>
<p>Some other books I would totally buy: Model Binding and Validation in ASP.net MVC 3/4, RESTFul WCF Applications, Ways to send data from the Server to a browser (From COMET and setInterval/JSONP crutches to Web Sockets and Server Side Events), Writing an ORM from Scratch.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2011/08/31/i-would-like-tech-books-to-be-like-this/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Business IT needs to step up into the 21st century</title>
		<link>http://www.stum.de/2011/06/24/business-it-needs-to-step-up-into-the-21st-century/</link>
		<comments>http://www.stum.de/2011/06/24/business-it-needs-to-step-up-into-the-21st-century/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 17:34:55 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=998</guid>
		<description><![CDATA[There is a lot of discussion going on at the moment about Firefox' rapid release cycles, releasing Firefox 5 just a few weeks after Firefox 4. A lot of business customers are complaining about this, saying that the testing cycle of FF4 is still ongoing, and now it's EOL already. Others think that it should [...]]]></description>
			<content:encoded><![CDATA[<p>There is a lot of discussion going on at the moment about Firefox' rapid release cycles, releasing Firefox 5 just a few weeks after Firefox 4. A lot of business customers are complaining about this, saying that the testing cycle of FF4 is still ongoing, and now it's EOL already. Others think that it should be Firefox 4.0.2 or 4.1 to appease both.</p>
<p>I say that Business IT finally needs to move into the 21st century and embrace this thing called "Internet". Why do you need a long testing cycle for a web browser? This is most likely a sign that your internal web applications are written for a specific browser, usually Internet Explorer 6. It means that your web applications contain countless hacks for that browser and because you didn't upgrade to Internet Explorer 7 and 8 when they came out in 2006 and 2009 respectively. It means that you have amassed considerable technical debt, and now you have to pay the piper.</p>
<p>I say: Sucks to be you. The Web is improving fast. When you wrote that ASP.net 1.1 Application that displays an Oracle Database in a data grid, you made a commitment to the web. You committed to abandon a stable platform like Windows (even with all the negative reactions to Vista, Microsoft is doing a lot to ensure backwards compatibility) and chose a platform that has been chaotic, anarchistic and unstable since the dawn of time, or at least since Netscape 2.</p>
<p>The year is 2011. You don't have to embrace HTML5, but you should make sure all your applications work in IE8, Firefox, Chrome and Safari/Mac without hacks, because then browser updates will be no problem. If you are still on Internet Explorer 6 or 7, upgrade to 8 (XP) or 9 (Modern Windows). If you are vendor-locked in to IE6 - I'm sorry, but you sucked. Your Purchasing Department really dropped the ball when they bet on IE6, because no sane company ties itself to a single product version of anything.</p>
<p>If you wrote an in-house web app or browser extension, then just upgrade it. And don't tell me anything about cost here - your business made the decision to go to the web, so if changes to the web environment come as a surprise, you didn't do your homework properly. Please, go back and write Windows Client apps again or do your job properly. If you want to be a web developer, then you need to do more than just drag/drop a DataGridView and connect it to a SqlDataSource. If you want to be an IT Person, learn how to work with the developers to make them change their apps.</p>
<p>Or, more likely: Continue to whine about how fast stuff changes, how vendors dare to improve their products, how they change the version number. Continue to embrace the waterfall model, get obsessed with version numbers, create test plans that take a year to execute, and stay out of touch with the real world. After all, business needs are more important than quality.</p>
<p>Just do me one favor then: Make sure that your crappy web apps can't be easily hacked. I'm getting tired with incompetent companies constantly losing my customer data to hackers. But then again, there are no penalties for losing customer data, so my plea will go unheard.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2011/06/24/business-it-needs-to-step-up-into-the-21st-century/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NDepend &#8211; &#8220;SQL&#8221; for your code</title>
		<link>http://www.stum.de/2011/03/21/ndepend-sql-for-your-code/</link>
		<comments>http://www.stum.de/2011/03/21/ndepend-sql-for-your-code/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 02:38:25 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ndepend]]></category>

		<guid isPermaLink="false">http://www.stum.de/2011/03/21/ndepend-sql-for-your-code/</guid>
		<description><![CDATA[Some time ago I received an offer for a NDepend Professional license. I was vary at first since Credibility and Free Stuff is hard to mix together, but I accepted it because it was on my list of tools to play around with anyway. It took me several months to actually use it in a [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I received an offer for a <a href="http://www.ndepend.com/">NDepend</a> Professional license. I was vary at first since Credibility and Free Stuff is hard to mix together, but I accepted it because it was on my list of tools to play around with anyway. It took me several months to actually use it in a real application, but here are some thoughts about it. Again, I received the license for free but the posting here is unbiased.</p>
<p>I guess unbiased reviews need to start with something negative to support the claim that the review is truly unbiased <img style="border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://www.stum.de/wp-content/uploads/2011/03/wlEmoticon-smile.png" /> So to roll with it, let me say that you shouldn’t judge the product by it’s homepage which sadly looks like a 2003-ish WebForms site. The application itself isn’t the best looking one either, but it has fancy colors and stuff. It also looks like 2003-Windows XP Luna when in 2011 we’ve moved back a bit to clean, serious, calm colors. But well, again, this is about the product.</p>
<p>My first question when looking at commercial software is of course: What problem does it try to solve, and is that problem really a problem?</p>
<p>NDepend is an analyzer to find some flaws that easily fall through the cracks: High <a href="http://stackoverflow.com/questions/911637/">Cyclomatic Complexity</a> (which is not a problem when the code is still fresh in your head, but causes nightmares 6 months down the road when you need to find a bug in it). <a href="http://thedailywtf.com/Articles/Enterprise-Dependency-Big-Ball-of-Yarn.aspx">Dependencies with more dependencies</a> with more dependencies with more… Comparing two code bases for a list of all changed methods.</p>
<p>It does a lot more, but what I was interested right now was their Code Query language, CQL. It’s like SQL, but for code.</p>
<p>For example, I’ve analyzed the Code, added a public method, analyzed again and the performed a “SELECT METHODS WHERE WasAdded AND IsPublic”. As a result, I get a list of all newly added Public methods:</p>
<p><a href="http://www.stum.de/wp-content/uploads/2011/03/image.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.stum.de/wp-content/uploads/2011/03/image_thumb.png" width="336" height="311" /></a></p>
<p>What problem does this solve? It solved the problem of Diff Tools being unaware what they are diffing. Let’s say you merge a large commit that has 200 changed files. You glance at them and see that there’s tons and tons of tab vs. spaces changes, many added code comments, some refactoring. It’s a very bad signal to noise ratio when in reality you are mainly concerned with “How did functionality change?”. Of course, you can now easily say “One change per commit. Commit Messages are enough documentation. Do not commit both functional and semantic changes” and you are right – but this is not how the real world works.</p>
<p>Maybe you don’t even know the code base because your predecessor didn’t do a good job documenting stuff or even checking stuff into source control and now you have two codebases (one possibly a result of decompiling the assembly) and need to get an overview.</p>
<p>The CQL Query edit window has IntelliSense which is really helpful, but not without it’s flaws: “SELECT methods&#160; WHERE&#160; WasAdded” does not work, it needs METHODS to be uppercase. Also there seems to be no &quot;SELECT * WHERE WasAdded”</p>
<p>The one thing I really dislike about this feature though is that it’s relatively complex to setup: Ideally I’d just love to tell NDepend “Here’s two Visual Studio solutions and/or assemblies, please analyze them and show me a list of functional differences”. Specialized tools like <a href="http://inca-app.com/">Inca</a> are available to make that easier of course.</p>
<p><strong>Methods with a lot of instructions</strong></p>
<p>You can query for a lot of really neat stuff, like the number of IL Instructions:</p>
<p>SELECT TYPES FROM ASSEMBLIES &quot;SWiki&quot; WHERE NbILInstructions &gt; 300 ORDER BY NbILInstructions DESC</p>
<p><a href="http://www.stum.de/wp-content/uploads/2011/03/image1.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.stum.de/wp-content/uploads/2011/03/image_thumb1.png" width="325" height="340" /></a></p>
<p>This is a LOT more useful than lines of code (which is also possible) because I can easily write very terse but unneccessarily complex code among the line of this:</p>
<pre>for (int i = 0, y = 1, z = 13; i++ &lt; 10 &amp;&amp; ++y &lt; 8; CallSomeMethod(i, y, z)) ;</pre>
<p>Only to save a few lines while still writing insanely crappy code that would make a C++ developer proud.</p>
<p><strong>Methods called by many other methods</strong></p>
<p>What else can we query by? Most Used Methods is nice, ignoring .net Framework assemblies and Properties:</p>
<p>SELECT TOP 50 METHODS WHERE !IsInFrameworkAssembly AND !IsPropertyGetter AND !IsPropertySetter&#160; ORDER BY MethodCa DESC </p>
<p><a href="http://www.stum.de/wp-content/uploads/2011/03/image2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.stum.de/wp-content/uploads/2011/03/image_thumb2.png" width="448" height="201" /></a></p>
<p>This is different from running a profiler, because this analyzes all the theoretical calls: There’s 23 places in my source where a certain constructor is called. Sadly there seems to way to get the Method Name as well as the Signature… Note that this is NOT a profiler thing: Just because the ctor is called in 23 different places does not mean that it needs to be performance critical because no statement how often it is called is made. This list is not a “Where to optimize speed next”, it’s a “Do we have any methods that should be abstracted away a bit more?” thing.</p>
<p><strong>Classes implementing an Interface</strong></p>
<p>MSDN has a list of derived classes on certain base classes, but it lacks this for Interfaces. Quick, tell me which classes implement IPrincipal? The <a href="http://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.aspx">MSDN Page</a> does not show, but NDepend does:</p>
<p><a href="http://www.stum.de/wp-content/uploads/2011/03/image3.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.stum.de/wp-content/uploads/2011/03/image_thumb3.png" width="396" height="373" /></a></p>
<p>I was looking for ages for a way to “Query” MSDN like that, and NDepend does it once you created a project to analyze all the .net Framework assemblies (and any additional ones of course).</p>
<p><strong>Comparing SharePoint 2007 to 2010</strong></p>
<p>As NDepend works against assemblies rather than source code, you can run analysis of SharePoint 2007 DLLs, then the SharePoint 2010 ones. The result is a full report about public API changes.</p>
<p>Even better: Run analysis before and after installing a Hotfix and see what actually changed and analyze your custom code precisely for these changes.</p>
<p><strong>What about existing Code Analysis and Resharper users?</strong></p>
<p>Now, I’m using Visual Studio 2010 Premium with Code Analysis and ReSharper which means that a lot of the simple mistakes are already caught without NDepend: Making Fields readonly, marking assemblies CLSCompliant, methods that don’t use “this” could be static, methods with way too many dependencies etc. pp.</p>
<p>For some of the more common problems, users of Code Analysis should better write their own rules so that they run on every build. But when it comes for just quickly querying the code prior to a refactor (How much stuff is affected?) or just for finding stuff (I wonder what ActionResults ASP.net MVC has, and which ones we’ve added?) NDepend is a great resource.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2011/03/21/ndepend-sql-for-your-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Connected Web Parts are bugged in SharePoint 2010</title>
		<link>http://www.stum.de/2011/01/23/connected-web-parts-are-bugged-in-sharepoint-2010/</link>
		<comments>http://www.stum.de/2011/01/23/connected-web-parts-are-bugged-in-sharepoint-2010/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 01:02:35 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Sharepoint]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=963</guid>
		<description><![CDATA[Update: This is an intentional design change, see the Microsoft answer in this thread. Just stumbled upon a bug in SharePoint 2010. Imagine this scenario: You have two lists and add them to a Site as DataForm WebParts. You then configure the Web Parts so that one of them sends Filter values to the other [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> This is an intentional design change, see the Microsoft answer in <a href="http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010general/thread/a6f73662-03a8-4d0e-8dad-bd4a2df0a180">this thread</a>.</p>
<p>Just stumbled upon a bug in SharePoint 2010. Imagine this scenario: You have two lists and add them to a Site as DataForm WebParts. You then configure the Web Parts so that one of them sends Filter values to the other one and uncheck the option for "Send first row to connected Web Parts when page loads". You refresh the Page and notice that the list that receives the Filter (the Consumer) does not show any items because the list that provides the Filter (the Provider) does not have any items selected.</p>
<p>In SharePoint 2007, not selecting an item in the Provider List would cause the consumer List to display all items instead, which was (at least for us) highly desirable. In SharePoint 2010, you have to select an Item in the Provider to provide a filter and there is no way to have the Consumer List display all items anymore.</p>
<p>The reason why I think this is a bug is the result of an extended Debugging session that eventually led me to the class Microsoft.SharePoint.WebPartPages.DataFormWebPart and specifically it's method ApplyReceivedFilterData. I'm not going to paste the source of this class, but the culprit is this line: <code>if (dictionary2[str] != null)</code></p>
<p>This is supposed to only add Filters that have a value and skip the ones that are null. The problem is that dictionary2[str] is not null but <a href="http://msdn.microsoft.com/en-us/library/system.dbnull(VS.90).aspx">DBNull</a>, which then causes SharePoint 2010 to process the value anyway and generate a nasty <code>&lt;Query&gt;&lt;Where&gt;&lt;IsNull&gt;&lt;FieldRef Name="FieldToReceiveFilter" /&gt;&lt;/IsNull&gt;&lt;/Where&gt;&lt;/Query&gt;</code> in the SelectCommand of the underlying DataSource</p>
<p>Unfortunately, DataFormWebPart.ApplyReceivedFilterData is not virtual, so sub-classing it does not seem to be an option. Not sure if there is a good workaround for this. I verified this behavior in both RTM and December 2010 Hotfixed versions of SharePoint 2010.</p>
<p>Edit: Just checked the SharePoint 2007 DataFormWebPart.ApplyReceivedFilterData() in Reflector and lo and behold, they did check for DBNull in 2007:</p>
<pre class="prettyprint lang-cs">
        Label_0046:
            if (current[str] is DBNull)
            {
                goto Label_006D;
                // Note by myself: Label_006D is the call to MoveNext, aka. continue
            }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2011/01/23/connected-web-parts-are-bugged-in-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A little Eggtimer script using Growl</title>
		<link>http://www.stum.de/2011/01/09/a-little-eggtimer-script-using-growl/</link>
		<comments>http://www.stum.de/2011/01/09/a-little-eggtimer-script-using-growl/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 13:59:09 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[zsh]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=945</guid>
		<description><![CDATA[The quasi-standard notification system for Mac OS X is Growl, and one of the extras included in the download is the growlnotify app that lets you push notifications from shell scripts. Then, there is the at command which allows to execute a job at a given time. It's essentially another way to modify the crontab, [...]]]></description>
			<content:encoded><![CDATA[<p>The quasi-standard notification system for Mac OS X is <a href="http://growl.info/">Growl</a>, and one of the extras included in the download is the growlnotify app that lets you push notifications from shell scripts. Then, there is the at command which allows to execute a job at a given time. It's essentially another way to modify the crontab, that doesn't use the crontab directly.</p>
<p>So with a notification system and a scheduler at our disposal, I thought it would be good to write a little eggtimer script so that I can specify "Remove Pizza from oven in 10 minutes". I'm sure there are tons of graphical tools available, but I prefer a simple shell script.</p>
<p>The growl side is easy, this command needs to be executed:</p>
<pre class="prettyprint">
growlnotify -t "Reminder at 03:54" -m "Remove Pizza from oven" -s -a iCal
</pre>
<p>The -t parameter specifies the title (even though the manpage incorrectly states "this does nothing"), -m is the message, -s means it's sticky (so it doesn't disappear unless explicitly closed) and -a specifies the icon to use, in this case the iCal icon.</p>
<p>The at command is used to create jobs that run at a specified time. However, it requires another "service" to run, called atrun. By default, this is disabled. Check the atrun manpage for the command to enable it, on my OS X 10.6.6 it's
<pre class="prettyprint">
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist
</pre>
<p>I found the syntax of at a bit weird as it kept complaining about garbled or incomplete time. It actually needs to read a whole input from stdin rather than just accepting a single command. So the invocation looks like this:</p>
<pre class="prettyprint">
at now + 10 minutes << eol
growlnotify -t "Reminder at 03:54" -m "Remove Pizza from oven" -s -a iCal
eol
</pre>
<p>That works, but of course it's WAY too much to type for us lazy people <img src='http://www.stum.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So let's wrap this in another shellscript that accepts two parameters: A number for "Remind in X minutes" and a message:</p>
<pre class="prettyprint">
#! /bin/zsh
function verifyminutes () {
  echo -n $1 | grep -Eq "^[0-9]+$"
  echo $?
}
function gettargettime() {
  date -v +$1M "+%H:%M"
}
function getmessage() {
  # function to collapse arguments $2..$n together (skipping $1)
  # I'm pretty sure there is a better way of doing this
  local FIRST_PARAMETER=0
  for i;
  do {
    if [ "$FIRST_PARAMETER" -eq 0 ]; then
      FIRST_PARAMETER=1
      continue
    fi
    echo -n $i" ";
  }
  done
  echo "\b"
}

local ISNUMBER=`verifyminutes $1`
[[ -z $1 || -z $2 || "$ISNUMBER" -gt 0 ]] &#038;& {
  echo "usage: $0 minutes message"
  exit 1
}

local TARGET_TIME=`gettargettime $1`
MESSAGE=`getmessage $@`
echo "Reminder set at $TARGET_TIME: $MESSAGE"

at now + $1 minutes >/dev/null 2>&#038;1 << eol
growlnotify -t "Reminder at $TARGET_TIME" -m "$MESSAGE" -s -a iCal
eol

exit 0
</pre>
<p>Usage of this script is simple, just call <code>eggtimer 10 Remove Pizza from oven</code> and 10 minutes later you will get a Growl notification.<br />
<img src="http://www.stum.de/wp-content/uploads/2011/01/Screen-shot-2011-01-09-at-5.12.41-AM.png" alt="" title="Egg Timer in Growl" width="298" height="94" class="aligncenter size-full wp-image-954" /></p>
<p>PS: If you are wondering about the getmessage function: Normally if you have spaces in an argument, you need to enclose the entire message in quotes. I wanted to get rid of the quotes, hence I'm collapsing all parameters after the first one together.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2011/01/09/a-little-eggtimer-script-using-growl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Misc Utilities Library</title>
		<link>http://www.stum.de/2010/12/27/misc-utilities-library/</link>
		<comments>http://www.stum.de/2010/12/27/misc-utilities-library/#comments</comments>
		<pubDate>Mon, 27 Dec 2010 09:08:47 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=930</guid>
		<description><![CDATA[I have various code snippets here and there in my blog and various tools, so I thought I'll just bundle them all together. I have created a project on GitHub for this, licensed under WTFPL as usual. There are no binary downloads because you're supposed to just cherry pick the stuff you want into your [...]]]></description>
			<content:encoded><![CDATA[<p>I have various code snippets here and there in my blog and various tools, so I thought I'll just bundle them all together. I have created a <a href="https://github.com/mstum/mstum.utils">project on GitHub</a> for this, licensed under <a href="http://sam.zoy.org/wtfpl/">WTFPL</a> as usual. There are no binary downloads because you're supposed to just cherry pick the stuff you want into your own project, but if you want to compile an assembly and use that, feel free to do so.</p>
<p>References for this on my blog:</p>
<ul>
<li><a href="http://www.stum.de/2008/06/22/parsing-the-command-line/">Parsing the command line</a></li>
<li><a href="http://www.stum.de/2008/10/20/base36-encoderdecoder-in-c/">Base36 Encoder/Decoder in C#</a></li>
<li><a href="http://www.stum.de/2010/01/22/a-simple-evenodd-cycler-for-net/">A simple Even/Odd Cycler for .net</a></li>
<li><a href="http://www.stum.de/2009/08/15/a-simple-ini-file-parser-for-c/">A simple .ini File parser for C#</a></li>
<li><a href="http://www.stum.de/2010/01/16/extension-method-return-another-string-if-string-is-null-or-empty/">Extension Method: Return another string if string is null or empty</a></li>
<li><a href="http://www.stum.de/2009/08/20/turning-string-format-into-an-extension-method/">Turning string.Format into an Extension Method</a></li>
<li><a href="http://www.stum.de/2010/02/16/an-extension-method-to-replace-multiple-chars-in-a-string/">An Extension method to replace multiple Chars in a string</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2010/12/27/misc-utilities-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NuGet: Description is Required</title>
		<link>http://www.stum.de/2010/12/13/nuget-description-is-required/</link>
		<comments>http://www.stum.de/2010/12/13/nuget-description-is-required/#comments</comments>
		<pubDate>Mon, 13 Dec 2010 21:16:01 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[nuget]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=919</guid>
		<description><![CDATA[Just got a new error message after updating to ASP.net MVC 3 RC2 which seems to contain a new NuGet version (displays as 1.0.10128.89 in Visual Studio Extension Manager). Any attempt to install a package - be it through 'Add Library Reference' or through PowerShell Install-Package - displays an error message: Description is required. Turns [...]]]></description>
			<content:encoded><![CDATA[<p>Just got a new error message after updating to ASP.net MVC 3 RC2 which seems to contain a new NuGet version (displays as 1.0.10128.89 in Visual Studio Extension Manager).</p>
<p>Any attempt to install a package - be it through 'Add Library Reference' or through PowerShell Install-Package - displays an error message: Description is required.</p>
<p>Turns out that one of the nuspec files has indeed an empty &lt;description /&gt; tag. Sadly, that breaks the entire repository.</p>
<p>The solution is to put in a description and rebuild the nupkg. In parallel, I have <a href="http://nuget.codeplex.com/workitem/463">filed a bug</a> on CodePlex in the hope that a single broken package does not break the entire Repository anymore.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2010/12/13/nuget-description-is-required/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPWebApplication.BlockedFileExtensions does not contain dots</title>
		<link>http://www.stum.de/2010/09/17/spwebapplication-blockedfileextensions-does-not-contain-dots/</link>
		<comments>http://www.stum.de/2010/09/17/spwebapplication-blockedfileextensions-does-not-contain-dots/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 18:43:16 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Sharepoint]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=904</guid>
		<description><![CDATA[Just a reminder post for me: SPWebApplication.BlockedFileExtensions outputs a Collection&#60;string> that does not contain the leading dot. However, Path.GetExtension does return the extension with a dot (or string.Empty in case the file does not have an extension). Additionally, there may be upper/lowercase conflicts.]]></description>
			<content:encoded><![CDATA[<p>Just a reminder post for me: <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebapplication.blockedfileextensions.aspx">SPWebApplication.BlockedFileExtensions</a> outputs a Collection&lt;string> that does not contain the leading dot.</p>
<p>However, <a href="http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx">Path.GetExtension</a> does return the extension with a dot (or string.Empty in case the file does not have an extension).</p>
<p>Additionally, there may be upper/lowercase conflicts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2010/09/17/spwebapplication-blockedfileextensions-does-not-contain-dots/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing a BF Compiler for .net (Part 7: The ret instruction)</title>
		<link>http://www.stum.de/2010/09/12/writing-a-bf-compiler-for-net-part-7-the-ret-instruction/</link>
		<comments>http://www.stum.de/2010/09/12/writing-a-bf-compiler-for-net-part-7-the-ret-instruction/#comments</comments>
		<pubDate>Sun, 12 Sep 2010 21:52:47 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[brainfk]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=901</guid>
		<description><![CDATA[Yesterday I have concluded the .net BF series with the explanation and code of the .net BF Compiler. But there was one thing that is important and that was unclear in the post: The ret instruction. Remember how the compiler emits a ret instruction at the end of the constructor and Execute method? I said [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.stum.de/2010/09/12/writing-a-bf-compiler-for-net-part-6-the-actual-compiler/">Yesterday I have concluded</a> the .net BF series with the explanation and code of the .net BF Compiler. But there was one thing that is important and that was unclear in the post: The ret instruction.</p>
<p>Remember how the compiler emits a ret instruction at the end of the constructor and Execute method? I said it's optional and only required for verifiable code. Well, <a href="http://stackoverflow.com/questions/3692937/">I've asked on StackOverflow</a> in the meantime and got the answer I was looking for:</p>
<blockquote><p>Control is not permitted to simply “fall through” the end of a method. All paths shall terminate with one of these instructions: ret, throw, jmp, or (tail. followed by call, calli, or callvirt).</p>
<p>(<a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm">ECMA-335</a>; 12.4, 6)</p></blockquote>
<p>So, there you have it: Make sure each method ends with one of the above instructions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2010/09/12/writing-a-bf-compiler-for-net-part-7-the-ret-instruction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing a BF Compiler for .net (Part 6: The actual compiler)</title>
		<link>http://www.stum.de/2010/09/12/writing-a-bf-compiler-for-net-part-6-the-actual-compiler/</link>
		<comments>http://www.stum.de/2010/09/12/writing-a-bf-compiler-for-net-part-6-the-actual-compiler/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 23:54:56 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[brainfk]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=894</guid>
		<description><![CDATA[In the last 5 parts we looked at each of the 8 instructions, how it works in BF, how it would work in C# and how it looks in IL. Now, what is a compiler actually? Quick Compiler Theory Basically our end result has to be a series of bytes. For example, the IL Instruction [...]]]></description>
			<content:encoded><![CDATA[<p>In the last 5 parts we looked at each of the 8 instructions, how it works in BF, how it would work in C# and how it looks in IL. Now, what is a compiler actually?</p>
<p><strong>Quick Compiler Theory</strong><br />
Basically our end result has to be a series of bytes. For example, the IL Instruction ldc.i4.0 is compiled as 0x16. Some instructions take parameters. For example, the br instruction is compiled as 0x38 0x?? 0x?? 0x?? 0x??. That's 5 bytes: 0x38 for the actual br and then 4 bytes for the address to jump to.</p>
<p>However, there is more. .net assemblies are usually .exe or .dll files and pure IL may only help you on mono (not sure if that's true and if you can feed mono with a pure IL file). If you're running on Windows, you need some additional "stuff" to make a proper .exe - see <a href="http://blog.vuscode.com/malovicn/archive/2007/12/24/net-foundations-net-assembly-structure.aspx">this Article</a>.</p>
<p>Now, how do we write a compiler? In what language do we write compilers? If you looked into Compiler development you may have heard the term "<a href="http://en.wikipedia.org/wiki/Bootstrapping_(compilers)">bootstrapping</a>" which means "Write a compiler for language X in language X through an intermediary compiler written in language Y". So if you want to write the very first C# compiler you can't write it in C# as there are no C# compilers to compile your C# code with, so you use another language. You could use C, C++, Delphi, Assembler or any other language.</p>
<p>Needless to say, this whole process is very complicated and involves a lot of plumbing (many lines of code for simple things because you have to manage everything). Luckily, Microsoft gave us a fantastic built-in functionality to compile .net Assemblies: The <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx">System.Reflection.Emit</a> Namespace.</p>
<p><strong>Reflection.Emit and how a Console Application works</strong><br />
What does Reflection.Emit allow us to do? It allows us to emit IL instructions into an assembly - we just feed it the IL instructions and it takes care of everything else. As a side effect, this allows us to write our compiler in any .net language! In our case, it means we will write our Compiler in C#.</p>
<p>But, what will we actually need to emit? Can we really just take the BF code and emit IL from it? No, not really. Each .net Assembly needs an Entry Point: A method. Create a new C# Console Application and notice that you start with a method "Main":</p>
<pre class="prettyprint lang-cs">
static void Main(string[] args)
{
}
</pre>
<p>Try renaming the method to something else and compile your Application. You will get this error message:<br />
<code>Program 'test.exe' does not contain a static 'Main' method suitable for an entry point</code></p>
<p>Each C# Application needs a method called Main. This is the entry point of the Application, this is the very first method that .net calls. Does it have to be called "Main"?</p>
<p>Not really. In .net, any method can be an entry point. It's just that the C# guys at Microsoft decided that for C# it shall be called Main, possibly because that's how it's usually called in C/C++.</p>
<p>Additionally, you may notice that the Main method is part of a class called Program. Do methods have to be part of a Type? In C# yes, but in C++/CLI you can have global methods. So does .net as a whole require a Type to hold methods? Yes, it does. Let me quote from Partition I, 9.8: "The CTS does not have the notion of global statics: all statics are associated with a particular class.".  There is an annotation to that: "[...] the CLI supports languages that supports global statics by creating a special class names &lt;module&gt;".</p>
<p>So to conclude, we need a class which contains a method. So, what will we actually compile now?</p>
<p>The BF Compiler we are going to write will create a Console Application. Specifically, we will create this class (shown as a C# class for illustration purposes):</p>
<pre class="prettyprint lang-cs">
public static class Program
{
    private static byte[] memory = new byte[short.MaxValue]; // 32767 bytes
    private static short pointer = 0;

    public static void Execute() {
        // Our BF Code here
    }
}
</pre>
<p>As you see, we have a byte-array that serves as our memory. We offer BF a generous 32 kilobyte of memory. Additionally, we have a pointer into the memory so that we know where we are in the memory. Our containing class is called Program and our entry point is called Execute (just to show that we are so cool, we don't need a method called Main). This method will then contain our compiled BF Code.</p>
<p><strong>Creating the bare minimum compiler skeleton</strong><br />
Let's start simple. Create a new C# Console Application and use this code for your main method:</p>
<pre class="prettyprint">
static void Main(string[] args)
{
    string sourceFileName = args[0];
    string outputName = args[1];

    var sourceCode = File.ReadAllText(sourceFileName);
    BFCompiler.Compile(outputName, outputName + ".exe", sourceCode);
    Console.WriteLine("{0} compiled to {1}.exe",sourceFileName,outputName);
}
</pre>
<p>Then, create a new class BFCompiler:</p>
<pre class="prettyprint">
public static class BFCompiler
{

    public static void Compile(string assemblyName, string outputFileName, string sourceCode)
    {
    }
}
</pre>
<p>Create a new text file and call it "hello.bf":</p>
<pre class="prettyprint">
++++++++++[>+++++++>++++++++++>+++>+&lt;&lt;&lt;&lt;-]>++.>+.+++++++..+++.>++.&lt;&lt;+++++++++++++++.>.+++.------.--------.>+.>.
</pre>
<p>In Visual Studio, set "Copy to Output Directory" to "Copy Always" and compile. You can now invoke your compiler using<br />
<code>bfnetc.exe hello.bf hello</code><br />
and it should print "hello.bf compiled to hello.exe" on the command line.</p>
<p>Okay, that was our skeleton: Read the .bf source code, call the compiler with the desired assembly name, output filename and source code.</p>
<p><strong>Creating an Assembly</strong><br />
Now we need to actually create the assembly. Change BFCompiler.Compile to contain this code:</p>
<pre class="prettyprint">
public static void Compile(string assemblyName, string outputFileName, string sourceCode)
{
    var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName),
                                                AssemblyBuilderAccess.Save);

    var mod = asm.DefineDynamicModule(assemblyName, outputFileName);

    var mainClassTypeName = assemblyName + ".Program"; // e.g., hello.Program
    var type = mod.DefineType(mainClassTypeName, TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Abstract | TypeAttributes.Public);

    var pointerField = type.DefineField("pointer", typeof (Int16), FieldAttributes.Static | FieldAttributes.Private);
    var memoryField = type.DefineField("memory", typeof(Byte[]), FieldAttributes.Static | FieldAttributes.Private);

    var constructor = type.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard, null);
    var cctorIlGen = constructor.GetILGenerator();
    GenerateConstructorBody(cctorIlGen,pointerField,memoryField);

    var mainMethod = type.DefineMethod("Execute", MethodAttributes.Public | MethodAttributes.Static);
    var ilGen = mainMethod.GetILGenerator();

    ilGen.Emit(OpCodes.Ret);
    type.CreateType();
    asm.SetEntryPoint(mainMethod);
    asm.Save(outputFileName);
}
</pre>
<p>Whoa, that looks complicated! (And also it doesn't compile as GenerateConstructorBody doesn't exist). Let us walk through the code step by step:</p>
<pre class="prettyprint">
var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName),
                                            AssemblyBuilderAccess.Save);

var mod = asm.DefineDynamicModule(assemblyName, outputFileName);

var mainClassTypeName = assemblyName + ".Program"; // e.g., hello.Program
var type = mod.DefineType(mainClassTypeName, TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Abstract | TypeAttributes.Public);
</pre>
<p>We start by <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilderaccess.aspx">creating a new Assembly</a> that has a certain name (in our example that would be "Hello"). This Assembly exists in the current Application Domain (This could be a security risk - please read up about application domains in .net if you want to know more). The <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilderaccess.aspx">second parameter</a> specifies what can be done with this assembly. This is another security feature, as we specified that "The dynamic assembly can be saved, but not executed.". The "but not executed" part does not mean that we generate an assembly that can't be run (that would be stupid, right?). It merely means that the Assembly (which only exists in memory for now) cannot be executed in the context of our running compiler. We can save it to disk though.</p>
<p>We then <a href="http://msdn.microsoft.com/en-us/library/xhsfddt5.aspx">specify a module</a>. Assemblies are collections of modules, and each assembly needs to contain at least one module. Usually, it contains exactly one module as Visual Studio doesn't support us in packaging multiple modules into one assembly. If you come from a C/C++ background, think of modules as static compilations. If you don't come from a C/C++ background, read up on <a href="http://www.microsoft.com/downloads/en/details.aspx?familyid=22914587-b4ad-4eae-87cf-b14ae6a939b0&#038;displaylang=en">ilmerge</a> and netmodules.</p>
<p>So our module is called "hello" and has a filename of "hello.exe".</p>
<p>We then create a new class (or Type) called "Program". This is a public sealed abstract class. Uhm... "sealed abstract"? Well, .net does not support static classes. But C# does? How? By cheating: A static class in C# is a class that is both abstract and sealed (something that's legal in .net but illegal in C#). By declaring it abstract it is ensured that nobody can instantiate it and by making it sealed it is ensured that nobody can subclass it. (For some trivia around C# static classes, see <a href="http://en.csharp-online.net/.NET_Type_Design_Guidelines%E2%80%94Static_Class_Design">this article</a>).</p>
<p>The other thing: What is the mainClassTypeName doing? It generates a namespace for us. Our typename in this example isn't Program - it's "hello.Program". During compilation, AssemblyBuilder will create a namespace "hello" and a Type "Program" automatically.</p>
<p>So now we have the "outer shell" of our compiled Application: An assembly called "hello" containing a module "hello.exe" containing a public "static" (abstract sealed) class "Program" in a namespace "hello".</p>
<p>Next, we define two fields in the Program Class:</p>
<pre class="prettyprint">
var pointerField = type.DefineField("pointer", typeof (Int16), FieldAttributes.Static | FieldAttributes.Private);
var memoryField = type.DefineField("memory", typeof(Byte[]), FieldAttributes.Static | FieldAttributes.Private);
</pre>
<p>These fields are private and static and of type short (=Int16) and Byte-Array respectively. But wait, don't we specify the size of them?</p>
<p>No, we don't. See, the C# way of writing</p>
<pre class="prettyprint lang-cs">
private static byte[] memory = new byte[short.MaxValue];
</pre>
<p>is just a convenience feature. If you compile this assembly and look at the IL, you will notice that the assignment is done in a static constructor. This is why we define a constructor:</p>
<pre class="prettyprint">
var constructor = type.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard, null);
</pre>
<p>The first parameter declares that we want to create a static constructor (.cctor). The second parameter means that the constructor is a normal method (as opposed to a virtual method or one that takes variable arguments). The third parameter defines any arguments the method takes - static constructors are always parameterless, hence we pass null.</p>
<p>We have our constructor method now, next we need to put some code into it:</p>
<pre class="prettyprint">
var cctorIlGen = constructor.GetILGenerator();
GenerateConstructorBody(cctorIlGen,pointerField,memoryField);
</pre>
<p><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.ilgenerator.aspx">GetILGenerator</a> is the magic that makes compilers happen. This thing allows you to write code into the method. Add a new method to the BFCompiler class:</p>
<pre class="prettyprint">
private static void GenerateConstructorBody(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
    // construct the memory as byte[short.MaxValue]
    ilGen.Emit(OpCodes.Ldc_I4,0x7fff);
    ilGen.Emit(OpCodes.Newarr,typeof(Byte));
    ilGen.Emit(OpCodes.Stsfld,memoryField);

    // Construct the pointer as short = 0
    ilGen.Emit(OpCodes.Ldc_I4_0);
    ilGen.Emit(OpCodes.Stsfld, pointerField);

    ilGen.Emit(OpCodes.Ret);
}
</pre>
<p>Whoa, another highly complex method. Well, actually it's rather simple, just many lines of code. Remember, you are now directly generating IL, so no convenience features. You have to do all the plumbing yourself. What are we doing? First, we call <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.ldc_i4.aspx">ldc.i4</a> 0x7fff which pushes the 32-Bit value 0x7fff (=32767 or short.MaxValue) to the stack. Then we call <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.newarr.aspx">newarr</a> typeof(byte) which creates a new Byte Array. Important: It's typeof(Byte), not typeof(Byte[])! newarr pops the length from the stack, so this now created a new Byte[32767] for us.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.stsfld(VS.85).aspx">stsfld</a> puts it into the memoryField.</p>
<p>So the static constructor now looks like this:</p>
<pre class="prettyprint">
static Program(){
    memory = new Byte[0x7fff];
}
</pre>
<p>The next two instructions of the constructor initialize the pointer field: Push 0 to the stack, replace pointer with the value on the stack.</p>
<p>You may wonder about the last instruction:</p>
<pre class="prettyprint">
ilGen.Emit(OpCodes.Ret);
</pre>
<p>This generates a return; statement. Why a return statement on a void method? Is this required? Technically, it's not. But in order to create verifiable assemblies, you have to emit it. Veri-what? <a href="http://www.vistadb.net/technical/managed-code-review.aspx">Verifiable Code</a> is a security feature in .net. It essentially allows tools (like <a href="http://msdn.microsoft.com/en-us/library/62bwd2yd(VS.90).aspx">PEVerify</a>) to determine that no security boundaries are violated (that is: no unsafe pointer manipulation and a well defined "flow" through the application). In a nutshell: By emitting the ret statement at the end of a method, any static analysis tool knows for sure that at this point, control is returned to the calling method. So always generate ret at the end of each method, even for void methods.</p>
<p>So that's our constructor. We now have a class Program with two fields (pointer and memory) that are properly initialized. Great! Now we create the main method:</p>
<pre class="prettyprint">
var mainMethod = type.DefineMethod("Execute", MethodAttributes.Public | MethodAttributes.Static);
var ilGen = mainMethod.GetILGenerator();
ilGen.Emit(OpCodes.Ret);
</pre>
<p>This is almost the same as above, except we call <a href="http://msdn.microsoft.com/en-us/library/bf36892y.aspx">DefineMethod</a>. The method is called "Execute" and is public static. We then get the ILGenerator for the method and emit a single instruction: <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.ret(VS.80).aspx">Ret</a>. Ignoring the issue with verifiable code outlined above, do we really need the ret statement? Yes, we do. No, actually we don't. We just need any statement. Why?</p>
<p>Because each method must have a method body. If you generate a method without any instructions in the body, you will get an exception:<br />
<code>System.InvalidOperationException: Method 'Execute' does not have a method body.</code></p>
<p>So we have to emit something, and we just return. We could also have chosen <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.nop.aspx">nop</a> (no-operation), but since we want to create verifiable code, we emit ret.</p>
<p>The next three instructions then actually create the Type and Assembly:</p>
<pre class="prettyprint">
type.CreateType();
asm.SetEntryPoint(mainMethod);
asm.Save(outputFileName);
</pre>
<p><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.createtype.aspx">CreateType</a> essentially "compiles" the type - until this moment, the Type only exists as some description, but we can't use it from other code.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilder.setentrypoint.aspx">SetEntryPoint</a> then points to the Execute method. If you don't set an entry point then the Assembly is still generated, but you can't run it ("hello.exe is not a valid Win32 application").</p>
<p><a href="http://msdn.microsoft.com/en-us/library/8zwdfdeh.aspx">Save</a> then finally writes the Assembly to disk.</p>
<p>Run your compiler again and you should get an hello.exe which does nothing, but at least it runs! Look at it in ildasm or Reflector and you should see this code:</p>
<pre class="prettyprint">
class public abstract auto ansi sealed Program
   extends [mscorlib]System.Object
{
   .method privatescope specialname rtspecialname static void .cctor() cil managed
   {
       .maxstack 1
       L_0000: ldc.i4 0x7fff
       L_0005: newarr uint8
       L_000a: stsfld uint8[] Program::memory
       L_000f: ldc.i4.0
       L_0010: stsfld int16 Program::pointer
       L_0015: ret
   }

   .method public static void Execute() cil managed
   {
       .entrypoint
       .maxstack 0
       L_0000: ret
   }

   .field private static uint8[] memory
   .field private static int16 pointer
}
</pre>
<p>Whoa, nice! Notice that Program derives from System.Object - we don't have to do that manually, Reflection.Emit does that for us automatically. Also, we don't have to worry about all this other stuff that is required to create a Windows Executable - AssemblyBuilder does that as well.</p>
<p><strong>Compiling the BF Code</strong><br />
We now have a working .net assembly that sets up some fields, but it doesn't do much. So let's do something with the BF Source Code! Add the following code to the BFCompiler.Compile method:</p>
<pre class="prettyprint lang-cs">
var ilGen = mainMethod.GetILGenerator();
// New Code
foreach(char c in sourceCode)
{
    switch (c)
    {
        case '>':
            GenerateMovePointerForwardInstruction(ilGen, pointerField, memoryField);
            break;
        case '&lt;':
            GenerateMovePointerBackwardsInstruction(ilGen, pointerField, memoryField);
            break;
        case '+':
            GenerateIncrementInstruction(ilGen, pointerField, memoryField);
            break;
        case '-':
            GenerateDecrementInstruction(ilGen, pointerField, memoryField);
            break;
        case '.':
            GenerateWriteInstruction(ilGen, pointerField, memoryField);
            break;
        case ',':
            GenerateReadInstruction(ilGen, pointerField, memoryField);
            break;
        case '[':
            GenerateOpenBracketInstruction(ilGen, pointerField, memoryField);
            break;
        case ']':
            GenerateCloseBracketInstruction(ilGen, pointerField, memoryField);
            break;
    }
}
// New Code
ilGen.Emit(OpCodes.Ret);
type.CreateType();
</pre>
<p>We are walking to the sourceCode, character by character. For each of the 8 known BF instructions we call another function that generates the proper IL. Let's quickly look at each of them:<br />
<strong>&lt; and > instructions</strong></p>
<pre class="prettyprint">
private static void GenerateMovePointerForwardInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
    ilGen.Emit(OpCodes.Ldsfld, pointerField);
    ilGen.Emit(OpCodes.Ldc_I4_1);
    ilGen.Emit(OpCodes.Add);
    ilGen.Emit(OpCodes.Conv_I2);
    ilGen.Emit(OpCodes.Stsfld, pointerField);
}

private static void GenerateMovePointerBackwardsInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
    ilGen.Emit(OpCodes.Ldsfld, pointerField);
    ilGen.Emit(OpCodes.Ldc_I4_1);
    ilGen.Emit(OpCodes.Sub);
    ilGen.Emit(OpCodes.Conv_I2);
    ilGen.Emit(OpCodes.Stsfld, pointerField);
}
</pre>
<p>We've been through them in <a href="http://www.stum.de/2010/04/19/writing-a-bf-compiler-for-net-part-3-pointer-and-pointer/">Part 3</a>: Load the pointer value, add/subtract 1 from it, convert the result to an Int16, store it in the pointer field again.</p>
<p><strong>+ and - instructions</strong></p>
<pre class="prettyprint">
private static void GenerateIncrementInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
    ilGen.Emit(OpCodes.Ldsfld, memoryField);
    ilGen.Emit(OpCodes.Ldsfld, pointerField);
    ilGen.Emit(OpCodes.Ldelema, typeof(Byte));
    ilGen.Emit(OpCodes.Dup);
    ilGen.Emit(OpCodes.Ldobj, typeof(Byte));
    ilGen.Emit(OpCodes.Ldc_I4_1);
    ilGen.Emit(OpCodes.Add);
    ilGen.Emit(OpCodes.Conv_U1);
    ilGen.Emit(OpCodes.Stobj, typeof(Byte));
}

private static void GenerateDecrementInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
    ilGen.Emit(OpCodes.Ldsfld, memoryField);
    ilGen.Emit(OpCodes.Ldsfld, pointerField);
    ilGen.Emit(OpCodes.Ldelema, typeof(Byte));
    ilGen.Emit(OpCodes.Dup);
    ilGen.Emit(OpCodes.Ldobj, typeof(Byte));
    ilGen.Emit(OpCodes.Ldc_I4_1);
    ilGen.Emit(OpCodes.Sub);
    ilGen.Emit(OpCodes.Conv_U1);
    ilGen.Emit(OpCodes.Stobj, typeof(Byte));
}
</pre>
<p>These were covered in <a href="http://www.stum.de/2010/04/12/writing-a-bf-compiler-for-net-part-2-writing-bf-in-c-and-looking-at-the-il/">part 2</a>. Load the memory and the pointer, get the memory-byte at the pointer address, add/subtract 1 from it, convert it to a byte (UInt8), replace it in the byte array.</p>
<p><strong>. and , instructions</strong></p>
<pre class="prettyprint">
private static void GenerateWriteInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
    ilGen.Emit(OpCodes.Ldsfld, memoryField);
    ilGen.Emit(OpCodes.Ldsfld, pointerField);
    ilGen.Emit(OpCodes.Ldelem_U1);
    ilGen.Emit(OpCodes.Call,
               typeof(Console).GetMethod("Write", BindingFlags.Public | BindingFlags.Static, null,
                                          new Type[] { typeof(Char) }, null));
}

private static void GenerateReadInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
    ilGen.Emit(OpCodes.Ldsfld, memoryField);
    ilGen.Emit(OpCodes.Ldsfld, pointerField);
    ilGen.Emit(OpCodes.Ldelem_U1);
    ilGen.Emit(OpCodes.Call, typeof(Console).GetMethod("Read", BindingFlags.Public | BindingFlags.Static));
    ilGen.Emit(OpCodes.Conv_U1);
    ilGen.Emit(OpCodes.Stelem_I1);
}
</pre>
<p><a href="http://www.stum.de/2010/04/27/writing-a-bf-compiler-for-net-part-4-and/">Part 4</a> covered these two, but now it gets a bit interesting. We need to call a method, Console.Read and Console.Write.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.call.aspx">OpCodes.Call</a> expects a <a href="http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.aspx">MethodInfo</a> and optionally an array of Types.</p>
<p>For Console. Read, We get the Console Type through typeof. Then we get a Method called "Read" which is defined as Public and Static. We pass this to the Call opcode. Console.Read writes it's return value to the stack, so we convert it to a Byte again and store it in memory[pointer].</p>
<p>With Console.Write it is a little bit more complicated because there are a lot of overloads to this methods. Remember that overload resolution is a convenience function of a compiler - as we are generating raw IL we need to precisely define which function we want to have. So we start again from the Console type, getting a Method called "Write". The fourth parameter is an array of types: The Argument signature. As we want the <a href="http://msdn.microsoft.com/en-us/library/testw4x7.aspx">Console.Write(char)</a> overload, we create a new Array containing the Char type. How do we pass parameters to Console.Write? By putting memory[pointer] onto the stack before.</p>
<p><strong>[ and ] instructions</strong><br />
<a href="http://www.stum.de/2010/04/28/writing-a-bf-compiler-for-net-part-5-and-while-loops-in-il/">Part 5</a> covered the while loop. As you know, we need to create <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.br.aspx">br instructions</a> to jump into some parts of the method. Do we have to dynamically calculate the locations? No, we don't.</p>
<p>Add a new field to the BFCompiler class:</p>
<pre class="prettyprint">
private readonly static Stack&lt;System.Reflection.Emit.Label> _bracketStack = new Stack&lt;System.Reflection.Emit.Label>();
</pre>
<p>Then add this instruction to the Compile method:</p>
<pre class="prettyprint">
public static void Compile(string assemblyName, string outputFileName, string sourceCode)
{
    _bracketStack.Clear(); // Ensure that the BracketStack is clear (if Compile is called multiple times)
</pre>
<p>Then add these two methods to implement the [ and ] commands:</p>
<pre class="prettyprint">
private static void GenerateOpenBracketInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{
    var firstLabel = ilGen.DefineLabel();
    var secondLabel = ilGen.DefineLabel();
    ilGen.Emit(OpCodes.Br, secondLabel);
    ilGen.MarkLabel(firstLabel);
    _bracketStack.Push(firstLabel);
    _bracketStack.Push(secondLabel);
}

private static void GenerateCloseBracketInstruction(ILGenerator ilGen, FieldInfo pointerField, FieldInfo memoryField)
{

    var secondLabel = _bracketStack.Pop();
    var firstLabel = _bracketStack.Pop();
    ilGen.MarkLabel(secondLabel);
    ilGen.Emit(OpCodes.Ldsfld, memoryField);
    ilGen.Emit(OpCodes.Ldsfld, pointerField);
    ilGen.Emit(OpCodes.Ldelem_U1);
    ilGen.Emit(OpCodes.Ldc_I4_0);
    ilGen.Emit(OpCodes.Bgt, firstLabel);
}
</pre>
<p>This looks terribly complicated, but it's relatively simple. We are essentially implementing a GOTO. But we are not jumping to an address, we are jumping to a <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.label.aspx">Label</a>. The [ instruction defines two labels, firstLabel and secondLabel. These are essentially just uninitialized variables. We then emit a jump to the secondLabel - this is not defined yet, but it doesn't have to be.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.ilgenerator.marklabel.aspx">MarkLabel</a> then defines the Label firstLabel at the current location in the method. But wait, something is missing: We need to define secondLabel and we need to jump back to the first Label. We do this on the ] instruction. But we somehow need to pass the two labels from the [ to the ] function, and we need to support nested [ ] loops. This is why we defined a Stack&lt;Label> in our Compiler: Whenever [ is called, we push the two labels onto it. Whenever ] is called, we pop the last two labels out of it.</p>
<p>So with the two labels available to the ] instruction, we mark the location of the secondLabel, then generate the comparison of memory[pointer] with 0. What we do was outlined in Part 5: If memory[pointer] is greater than 0, we jump to firstLabel.</p>
<p>Compile your Application again and run the compiler on hello.bf again. If everything went fine, you should now be able to run the generated hello.exe and see a satisfying "Hello World!" on the console.</p>
<p>Congratulations - you now have a BF compiler that generates a .net Console Application!</p>
<p>You can find the source code as a Visual Studio 2010 solution on <a href="http://github.com/mstum/bfnetc">http://github.com/mstum/bfnetc</a></p>
<p>This could conclude this series, but I'm going to write a Part 7 containing a few more considerations: EXE vs. DLL, Release vs. Debug builds and some more.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2010/09/12/writing-a-bf-compiler-for-net-part-6-the-actual-compiler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The one dynamic language I think Microsoft needs to embrace in .net&#8230;</title>
		<link>http://www.stum.de/2010/08/08/the-one-dynamic-language-i-think-microsoft-needs-to-embrace-in-net/</link>
		<comments>http://www.stum.de/2010/08/08/the-one-dynamic-language-i-think-microsoft-needs-to-embrace-in-net/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 00:16:47 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=890</guid>
		<description><![CDATA[So a few days ago, Jimmy Schementi announced the death of IronRuby. Oh, sorry, "IronRuby isn't dead, it's just back in the hands of the community" which is essentially the same. Now, while there may be a chance for IronRuby to survive, I personally think it's not something that Microsoft should do, so I think [...]]]></description>
			<content:encoded><![CDATA[<p>So a few days ago, Jimmy Schementi announced the <a href="http://blog.jimmy.schementi.com/2010/08/start-spreading-news-future-of-jimmy.html">death of IronRuby</a>. Oh, sorry, "<a href="http://twitter.com/jschementi/status/20508631769">IronRuby isn't dead, it's just back in the hands of the community</a>" which is essentially <a href="http://evain.net/blog/articles/2010/08/07/on-ironruby">the same</a>.</p>
<p>Now, while there may be a chance for IronRuby to survive, I personally think it's not something that Microsoft should do, so I think it makes sense for them to kill it off internally. Same for IronPython. I think the DLR is a great addition to .net though, and I think there is a far better language already available.</p>
<p><a href="http://en.wikipedia.org/wiki/JavaScript">JavaScript</a>.</p>
<p>Many of us perceive JavaScript as a Browser-only language that is hampered by different implementations in different Browsers (<a href="http://blog.coderlab.us/2006/04/18/the-textcontent-and-innertext-properties/">textContent vs. innerText</a> anyone?), something which is being addressed through the use of frameworks like <a href="http://jquery.com/">jQuery</a>.</p>
<p>But think it one step further: How many languages do you need to write a Web Application? At least two: JavaScript and whatever backend language you use, for example C#, Java, Ruby or PHP. How do you write Form Validation? You write separate Code for the Client Side and for the Server Side so that users get instant feedback without being able to compromise the system by turning JavaScript off. This is stupid and a severe violation of <a href="http://en.wikipedia.org/wiki/DRY">DRY</a>. Also there are subtle differences between the Code, for example because JavaScript's RegEx Engine works slightly different than the .net one.</p>
<p>.net is at it's core a language-independent technology that doesn't only support but encourage using multiple languages. Sure, there are only 3-4 really supported languages which are rarely mixed (Some people mix C++/CLI with C# or VB.net for some COM stuff, or F# with C# for financial/statistic stuff) and some languages that have some limited support (<a href="http://en.wikipedia.org/wiki/Boo_(programming_language)">Boo</a> is my favorite in this category).</p>
<p>So why not do what's logical and embrace JavaScript as a Server-Side technology? <a href="http://nodejs.org/">Node.js</a> arrived recently and showed it's possible. Microsoft already somewhat supports JavaScript in Visual Studio (although the experience is far less than stellar) and they even had their own bastard child in form of <a href="http://en.wikipedia.org/wiki/JScript.NET">JScript.net</a>.</p>
<p>There is a big discussion whether or not C# is a good language to write Web Applications in. People point at <a href="http://cukes.info/">Cucumber</a> and talk about how great Ruby works with the Web while C# feels like a chore because of it's static typing, verbose Syntax and need for IoC Containers to do Unit Testing properly. Other people point at the insecurity of dynamic languages, about the lack of compiler errors, about the confusion created by not having to declare variables which leads to subtle bugs like `$total = 0; foreach(item in items) $totel += item.price`.</p>
<p>I say: Use both. Use C# for your backend code, for your Business Classes. Get Compiler Errors when you screw up. Make sure everything needs to be explicitly casted to whatever it has to be and that all variables have to be defined officially before use. For the frontend, use JavaScript. Create your Views and View Models like you would create them in the browser, through DOM Manipulation. Write and maintain your verification code exactly once, in one language, browser and server side. Feel free to whatever crazy manipulation you need to do without having to declare and cast tons of variables. If you screw up, it's "only" your Views, not your data structure because that's C# code.</p>
<p>Utopia? Maybe. JavaScript is not without it's faults and it may not be as elegant as Ruby in places. People would have to learn two languages, and people may ask "Why should we switch to an insecure language if C# served us well for years?" - well, the latter people are usually the ones who don't think they need Unit Testing and believe that WebForms is a perfectly good technology, so let them continue to use it and let the rest of us move forward to face the challenges of 2010 and beyond.</p>
<p>Customers want better apps. They want AJAX, they want snappy, cool looking UI. The Browser market is not a "Make sure it runs in IE and doesn't suck in Netscape 4.78" market anymore. We need to create cool apps, and we need to run them on Internet Explorer, Firefox, Safari on OS X, Safari on iPhone and Android. Our Web Apps need to do more in less time.</p>
<p>And Microsoft is in a great position because they have the foundation already built. Make Visual Studio a kick-ass JavaScript Development tool and bring it to the server side. Give us great debugging because even with FireBug and the IE Development Tools, it still sucks. Look what Node.js did. Look what you are doing with <a href="http://blogs.msdn.com/b/ie/archive/2010/03/18/the-new-javascript-engine-in-internet-explorer-9.aspx">Internet Explorer 9's JS Engine</a>. Look what you have with <a href="http://en.wikipedia.org/wiki/Active_Scripting">Active Scripting</a> and look what you did with <a href="http://en.wikipedia.org/wiki/JScript.NET">JScript.net</a>.</p>
<p>It's incredible to see that Microsoft has all this technology already lying around and that no one had the idea to just combine them all together for ASP.net. Sure, Internet Explorer got a lot of (well deserved) crap for issues in it's JavaScript implementation, but IE9's previews look really promising.</p>
<p>Because let's face it: Any .net Language not supported by Microsoft in Visual Studio is doomed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2010/08/08/the-one-dynamic-language-i-think-microsoft-needs-to-embrace-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>64-Bit Bitfield Cheat Sheet</title>
		<link>http://www.stum.de/2010/08/02/64-bit-bitfield-cheat-sheet/</link>
		<comments>http://www.stum.de/2010/08/02/64-bit-bitfield-cheat-sheet/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 10:43:58 +0000</pubDate>
		<dc:creator>mstum</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.stum.de/?p=884</guid>
		<description><![CDATA[Just as a cheat sheet for me, a 64-Bit Bitfield in Dec and Hex. Bit Int Hex 1 1 0x1 2 2 0x2 3 4 0x4 4 8 0x8 5 16 0x10 6 32 0x20 7 64 0x40 8 128 0x80 9 256 0x100 10 512 0x200 11 1024 0x400 12 2048 0x800 13 4096 [...]]]></description>
			<content:encoded><![CDATA[<p>Just as a cheat sheet for me, a 64-Bit Bitfield in Dec and Hex.</p>
<table cellspacing="0" cellpadding="0" width="600">
<tbody>
<tr>
<td width="30" valign="top"><strong>Bit</strong></td>
<td width="285" valign="top"><strong>Int</strong></td>
<td width="285" valign="top"><strong>Hex</strong></td>
</tr>
<tr>
<td valign="top"><strong>1</strong></td>
<td valign="top">1</td>
<td valign="top">0x1</td>
</tr>
<tr>
<td valign="top"><strong>2</strong></td>
<td valign="top">2</td>
<td valign="top">0x2</td>
</tr>
<tr>
<td valign="top"><strong>3</strong></td>
<td valign="top">4</td>
<td valign="top">0x4</td>
</tr>
<tr>
<td valign="top"><strong>4</strong></td>
<td valign="top">8</td>
<td valign="top">0x8</td>
</tr>
<tr>
<td valign="top"><strong>5</strong></td>
<td valign="top">16</td>
<td valign="top">0x10</td>
</tr>
<tr>
<td valign="top"><strong>6</strong></td>
<td valign="top">32</td>
<td valign="top">0x20</td>
</tr>
<tr>
<td valign="top"><strong>7</strong></td>
<td valign="top">64</td>
<td valign="top">0x40</td>
</tr>
<tr>
<td valign="top"><strong>8</strong></td>
<td valign="top">128</td>
<td valign="top">0x80</td>
</tr>
<tr>
<td valign="top"><strong>9</strong></td>
<td valign="top">256</td>
<td valign="top">0x100</td>
</tr>
<tr>
<td valign="top"><strong>10</strong></td>
<td valign="top">512</td>
<td valign="top">0x200</td>
</tr>
<tr>
<td valign="top"><strong>11</strong></td>
<td valign="top">1024</td>
<td valign="top">0x400</td>
</tr>
<tr>
<td valign="top"><strong>12</strong></td>
<td valign="top">2048</td>
<td valign="top">0x800</td>
</tr>
<tr>
<td valign="top"><strong>13</strong></td>
<td valign="top">4096</td>
<td valign="top">0x1000</td>
</tr>
<tr>
<td valign="top"><strong>14</strong></td>
<td valign="top">8192</td>
<td valign="top">0x2000</td>
</tr>
<tr>
<td valign="top"><strong>15</strong></td>
<td valign="top">16384</td>
<td valign="top">0x4000</td>
</tr>
<tr>
<td valign="top"><strong>16</strong></td>
<td valign="top">32768</td>
<td valign="top">0x8000</td>
</tr>
<tr>
<td valign="top"><strong>17</strong></td>
<td valign="top">65536</td>
<td valign="top">0x10000</td>
</tr>
<tr>
<td valign="top"><strong>18</strong></td>
<td valign="top">131072</td>
<td valign="top">0x20000</td>
</tr>
<tr>
<td valign="top"><strong>19</strong></td>
<td valign="top">262144</td>
<td valign="top">0x40000</td>
</tr>
<tr>
<td valign="top"><strong>20</strong></td>
<td valign="top">524288</td>
<td valign="top">0x80000</td>
</tr>
<tr>
<td valign="top"><strong>21</strong></td>
<td valign="top">1048576</td>
<td valign="top">0x100000</td>
</tr>
<tr>
<td valign="top"><strong>22</strong></td>
<td valign="top">2097152</td>
<td valign="top">0x200000</td>
</tr>
<tr>
<td valign="top"><strong>23</strong></td>
<td valign="top">4194304</td>
<td valign="top">0x400000</td>
</tr>
<tr>
<td valign="top"><strong>24</strong></td>
<td valign="top">8388608</td>
<td valign="top">0x800000</td>
</tr>
<tr>
<td valign="top"><strong>25</strong></td>
<td valign="top">16777216</td>
<td valign="top">0x1000000</td>
</tr>
<tr>
<td valign="top"><strong>26</strong></td>
<td valign="top">33554432</td>
<td valign="top">0x2000000</td>
</tr>
<tr>
<td valign="top"><strong>27</strong></td>
<td valign="top">67108864</td>
<td valign="top">0x4000000</td>
</tr>
<tr>
<td valign="top"><strong>28</strong></td>
<td valign="top">134217728</td>
<td valign="top">0x8000000</td>
</tr>
<tr>
<td valign="top"><strong>29</strong></td>
<td valign="top">268435456</td>
<td valign="top">0x10000000</td>
</tr>
<tr>
<td valign="top"><strong>30</strong></td>
<td valign="top">536870912</td>
<td valign="top">0x20000000</td>
</tr>
<tr>
<td valign="top"><strong>31</strong></td>
<td valign="top">1073741824</td>
<td valign="top">0x40000000</td>
</tr>
<tr>
<td valign="top"><strong>32</strong></td>
<td valign="top">2147483648</td>
<td valign="top">0x80000000</td>
</tr>
<tr>
<td valign="top"><strong>33</strong></td>
<td valign="top">4294967296</td>
<td valign="top">0x100000000</td>
</tr>
<tr>
<td valign="top"><strong>34</strong></td>
<td valign="top">8589934592</td>
<td valign="top">0x200000000</td>
</tr>
<tr>
<td valign="top"><strong>35</strong></td>
<td valign="top">17179869184</td>
<td valign="top">0x400000000</td>
</tr>
<tr>
<td valign="top"><strong>36</strong></td>
<td valign="top">34359738368</td>
<td valign="top">0x800000000</td>
</tr>
<tr>
<td valign="top"><strong>37</strong></td>
<td valign="top">68719476736</td>
<td valign="top">0x1000000000</td>
</tr>
<tr>
<td valign="top"><strong>38</strong></td>
<td valign="top">137438953472</td>
<td valign="top">0x2000000000</td>
</tr>
<tr>
<td valign="top"><strong>39</strong></td>
<td valign="top">274877906944</td>
<td valign="top">0x4000000000</td>
</tr>
<tr>
<td valign="top"><strong>40</strong></td>
<td valign="top">549755813888</td>
<td valign="top">0x8000000000</td>
</tr>
<tr>
<td valign="top"><strong>41</strong></td>
<td valign="top">1099511627776</td>
<td valign="top">0x10000000000</td>
</tr>
<tr>
<td valign="top"><strong>42</strong></td>
<td valign="top">2199023255552</td>
<td valign="top">0x20000000000</td>
</tr>
<tr>
<td valign="top"><strong>43</strong></td>
<td valign="top">4398046511104</td>
<td valign="top">0x40000000000</td>
</tr>
<tr>
<td valign="top"><strong>44</strong></td>
<td valign="top">8796093022208</td>
<td valign="top">0x80000000000</td>
</tr>
<tr>
<td valign="top"><strong>45</strong></td>
<td valign="top">17592186044416</td>
<td valign="top">0x100000000000</td>
</tr>
<tr>
<td valign="top"><strong>46</strong></td>
<td valign="top">35184372088832</td>
<td valign="top">0x200000000000</td>
</tr>
<tr>
<td valign="top"><strong>47</strong></td>
<td valign="top">70368744177664</td>
<td valign="top">0x400000000000</td>
</tr>
<tr>
<td valign="top"><strong>48</strong></td>
<td valign="top">140737488355328</td>
<td valign="top">0x800000000000</td>
</tr>
<tr>
<td valign="top"><strong>49</strong></td>
<td valign="top">281474976710656</td>
<td valign="top">0x1000000000000</td>
</tr>
<tr>
<td valign="top"><strong>50</strong></td>
<td valign="top">562949953421312</td>
<td valign="top">0x2000000000000</td>
</tr>
<tr>
<td valign="top"><strong>51</strong></td>
<td valign="top">1125899906842624</td>
<td valign="top">0x4000000000000</td>
</tr>
<tr>
<td valign="top"><strong>52</strong></td>
<td valign="top">2251799813685248</td>
<td valign="top">0x8000000000000</td>
</tr>
<tr>
<td valign="top"><strong>53</strong></td>
<td valign="top">4503599627370496</td>
<td valign="top">0x10000000000000</td>
</tr>
<tr>
<td valign="top"><strong>54</strong></td>
<td valign="top">9007199254740992</td>
<td valign="top">0x20000000000000</td>
</tr>
<tr>
<td valign="top"><strong>55</strong></td>
<td valign="top">18014398509481984</td>
<td valign="top">0x40000000000000</td>
</tr>
<tr>
<td valign="top"><strong>56</strong></td>
<td valign="top">36028797018963968</td>
<td valign="top">0x80000000000000</td>
</tr>
<tr>
<td valign="top"><strong>57</strong></td>
<td valign="top">72057594037927936</td>
<td valign="top">0x100000000000000</td>
</tr>
<tr>
<td valign="top"><strong>58</strong></td>
<td valign="top">144115188075855872</td>
<td valign="top">0x200000000000000</td>
</tr>
<tr>
<td valign="top"><strong>59</strong></td>
<td valign="top">288230376151711744</td>
<td valign="top">0x400000000000000</td>
</tr>
<tr>
<td valign="top"><strong>60</strong></td>
<td valign="top">576460752303423488</td>
<td valign="top">0x800000000000000</td>
</tr>
<tr>
<td valign="top"><strong>61</strong></td>
<td valign="top">1152921504606846976</td>
<td valign="top">0x1000000000000000</td>
</tr>
<tr>
<td valign="top"><strong>62</strong></td>
<td valign="top">2305843009213693952</td>
<td valign="top">0x2000000000000000</td>
</tr>
<tr>
<td valign="top"><strong>63</strong></td>
<td valign="top">4611686018427387904</td>
<td valign="top">0x4000000000000000</td>
</tr>
<tr>
<td valign="top"><strong>64</strong></td>
<td valign="top">9223372036854775808</td>
<td valign="top">0x8000000000000000</td>
</tr>
</tbody>
</table>
<p>Checking if a bit is set:</p>
<pre class="prettyprint lang-cs">
// AND: Only return 1 if both bits are 1
// 0011 &#038; 0100 = 0000
// 0111 &#038; 0100 = 0100
isSet = (value &#038; 0x4) == 0x4;
isSet = (value &#038; 0x4) > 0;
</pre>
<p>Setting a bit:</p>
<pre class="prettyprint lang-cs">
// OR: If either operand is 1, return 1.
// 0011 | 0100 = 0111
newvalue = oldvalue | 0x4; // |=
</pre>
<p>Unsetting a bit:</p>
<pre class="prettyprint lang-cs">
// NOT: Invert the Bits
// ~0100 = 1011
// AND: Return 1 if both bits are 1
// 0011 &#038; 1011 = 0011
// 0111 &#038; 1011 = 0011
newvalue = oldvalue &#038; (~0x4) // &#038;= ~0x4;
</pre>
<p>Toggling a bit:</p>
<pre class="prettyprint lang-cs">
// XOR: If both bits are equal, return 0
// 0111 ^ 0100 = 0011
// 0011 ^ 0100 = 0111
newvalue = oldvalue ^ 0x4; // ^=
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.stum.de/2010/08/02/64-bit-bitfield-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

