tag:blogger.com,1999:blog-84683213683600097942024-03-13T15:10:07.603+02:00self.brain.dump!a non-periodic memory dump about .NET, C#, ruby, rails and moreChristoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-8468321368360009794.post-48940371732240977752014-03-19T11:31:00.000+02:002014-03-19T11:31:29.438+02:00Gravity is quantum...<a href="http://physicsworld.com/cws/article/news/2014/mar/17/bicep2-finds-first-direct-evidence-of-cosmic-inflation">...and inflation happened!</a>Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-37713095199546358062008-10-03T08:07:00.009+02:002008-10-03T20:23:55.994+02:00Giles rulez<a href="http://gilesbowkett.blogspot.com/">Giles Bowkett</a> is one of the bloggers out there whose blog I regularly read and enjoy. This September he had a presentation on <a href="http://gilesbowkett.blogspot.com/2008/02/archaeopteryx-ruby-midi-generator.html">Archaeopteryx</a> at the <a href="http://www.rubyinside.com/the-rubyfringe-videos-vids-from-the-summers-hottest-conference-1214.html">RubyFringe</a> conference. Yesterday I watched his talk. Awesome! This guy rocks and I can only suggest that if you are interested in ruby, lambdas, music or just like to supercharge your senses <a href="http://www.infoq.com/presentations/archaeopteryx-bowkett">this talk</a> is for you!<br /><br />Hats off. And don't go back to your gas station Giles, we need you here!<br /><variable name="bgcolor" description="Page Background Color" type="color" default="#000"><variable name="textcolor" description="Text Color" type="color" default="#ccc"><variable name="linkcolor" description="Link Color" type="color" default="#9ad"><variable name="pagetitlecolor" description="Blog Title Color" type="color" default="#ccc"><variable name="descriptioncolor" description="Blog Description Color" type="color" default="#777"><variable name="titlecolor" description="Post Title Color" type="color" default="#ad9"><variable name="bordercolor" description="Border Color" type="color" default="#333"><variable name="sidebarcolor" description="Sidebar Title Color" type="color" default="#777"><variable name="sidebartextcolor" description="Sidebar Text Color" type="color" default="#999"><variable name="visitedlinkcolor" description="Visited Link Color" type="color" default="#a7a"><variable name="bodyfont" description="Text Font" type="font" default="normal normal 100% 'Trebuchet MS',Trebuchet,Verdana,Sans-serif"><variable name="headerfont" description="Sidebar Title Font" type="font" default="normal bold 78% 'Trebuchet MS',Trebuchet,Arial,Verdana,Sans-serif"><variable name="pagetitlefont" description="Blog Title Font" type="font" default="normal bold 200% 'Trebuchet MS',Trebuchet,Verdana,Sans-serif"><variable name="descriptionfont" description="Blog Description Font" type="font" default="normal normal 78% 'Trebuchet MS', Trebuchet, Verdana, Sans-serif"><variable name="postfooterfont" description="Post Footer Font" type="font" default="normal normal 78% 'Trebuchet MS', Trebuchet, Arial, Verdana, Sans-serif"></variable></variable></variable></variable></variable></variable></variable></variable></variable></variable></variable></variable></variable></variable></variable>Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-87579631935835999602008-05-18T20:31:00.004+03:002008-05-21T23:06:30.868+03:00CI and NCoverageOn our current project the continuous integration process demands that the code base has a minimal test coverage of 85%. If this condition is not met – guess what – the build fails. Initially we’ve started with a limit of 95%. We all knew that this was not quiet realistic but we thought there could be nothing wrong by starting with a noble attitude. As expected the effective coverage of the code was always slowly dropping to the configured limit. To the current point I am still satisfied with the result but there were some issues coming up during the daily stand up meetings:<br /><br /><span style="font-style: italic;">Who’s in charge fixing the build if the limit is not met and the build fails?</span><br /><br />From my point of view this is a no brainer. The person who actually checks in is responsible. But one member of my team had a different view. After some days off the project he joined us again, made a new class, checked it in and the build failed. From his point of view the coverage before his check in was already not sufficient because there should always be enough reserve for an empty class with a constructor to be checked in without the build failing. So he suggested we should all take some time to collectively increase the overall coverage.<br />I replied that if you are consequently do test first there should not even be such a class without an according test. Unfortunately until this point not every member of our team has come to the point where he is consequently doing test first. Also we did usually no coverage analysis on our local machine before checking in. This means, if the person which checks the code in is not a test-hardliner the build might. It means further that if you are close to the breaking limit of the test coverage your build will fail proportional to the number of members in your team which do not have enough experience in testing. This is really not a good option because a failing build means not only subsequent check ins will also fail if the build has not been fixed in the meantime, it means also more turbulence in the team.<br /><br />For me this means that even if I manage to convince the team that if the build fails it is the one checking in who's in charge, I have to live with the fact that there’s some flow getting lost if we are near the limit.<br /><br /><span style="font-style: italic;">Do we set the limit </span><span style="font-style: italic;">narrow </span><span style="font-style: italic;">enough to allow code be within the constraint which the team agrees does not have to be tested? (Like declarative code which when tested would only duplicate the declaration) Or do we tag the code with the according attributes to ignore it in the analysis and therefore are able to set the limit on a higher percentage?</span><br /><br />From my point of view I do not like my code to be cluttered with ignore attributes just to be able to meet a self determined limit in our build process. But there were other opinions. One team member suggested that if we all agree that this code does not need to be tested why should we not tag it with such an attribute. I thinks this is a legitimate argument. Although I would not like the additional typing if we could avoid it simply by lowering the coverage limit accordingly. Eventually this is why we are configuring such a limit: because there is code which we all agree cannot be efficiently or meaningful be tested.<br /><br />Bottom line we are still gathering experience how to properly handle the additional metrics of a coverage analysis in our development process.Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com3tag:blogger.com,1999:blog-8468321368360009794.post-55706653182836588312008-03-30T17:58:00.028+02:002008-04-02T19:52:00.083+02:00IQueryable<T> vs. IEnumerable<T>In one of our current projects we're using LINQ TO SQL to conquer the object-relational impedance mismatch. We all had some experience with LINQ and deferred execution. But it was getting obvious that we all needed to deeply internalize the difference between IEnumerable and IQueryable.<br /><br />EntitySet<T> and Table<T> both implement the IEnumerable<T> interface. However, if there would not be IQueryable all querying functionality - including filtering and sorting - would be executed on the client. To optimize a query for a specific data source we need a way to analyze a query and its definition. That’s where expression trees are coming in. As we know an expression tree represents the logical definition of a method call which can be manipulated and transformed.<br /><br />So we have a mutable logical definition of our query on one side and a queryable data source on the other. The Property Provider on IQueryable now returns an IQueryProvider which is exactly what we need here.<br /><br /><code> public interface IQueryProvider {<br /> IQueryable CreateQuery(Expression expression);<br /> IQueryable<TElement> CreateQuery<TElement>(Expression expression);<br /> object Execute(Expression expression);<br /> TResult Execute<TResult>(Expression expression);<br /> }<br /></code><br /><br />There are 2 interesting operations and their generic counterparts. The generic versions are used most of the time and they perform better because we can avoid using reflection for object instantiation. CreateQuery() does precisely what we are looking for. It takes an expression tree as argument and returns another IQueryable based on the logical definition of the tree. When the returned IQueryable is enumerated it will invoke the query provider which will then process this specific query expression.<br />The Execute() method now is used to actually executing your query expression. This explicit entry point – instead of just relying on IEnumerator.GetEnumerator() – allows executing ET’s which do not necessarily yield sequences of elements. (For example aggregate functions like count or sum.)<br /><br />We finally have our two worlds nicely connected together. The mutability of ET and the deferred execution of IEnumerable combined to a construct that can analyze an arbitrary mutated and extended query at the last possible moment and execute an optimized query against its data source. It’s not even too hard to implement your own IQueryProvider for your own data source. Maybe I’ll cover that in a later post. This is really nice work Eric Meijer and his team has done here.Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-11686175989869326752008-01-01T18:43:00.000+02:002008-01-01T22:52:23.363+02:00Release It! (Book Review)Today I finished the Book <a href="http://www.pragprog.com/titles/mnee">'Release It! Design and Deploy Production-Ready Software'</a> by Michael T. Nygard. Coincidentally it was just a few days ago when InfoQ has released an <a href="http://www.infoq.com/news/2007/12/review-release-it">article </a>to this book as well. But this was not preventing me from blogging my own personal two cents about the work of Nygard.<br /><br />The book is the 5the title published by the <a href="http://www.pragprog.com/">pragmatic programmers</a> in my bookshelf. All the titles were very interesting, qualitatively convincing and beautifully put together - don't underestimate the attraction of a beautiful book to a reader and aesthetically oriented guy like me. Now to this specific title.<br /><br />The author brings up a series of patterns and anti-patterns for creating software. Software which not only survives development but also survives in production - the environment of the real world. It provides general guidance as well as some very specific tips and tricks for development as well as operations. Although some parts are closely related to the Java world, there is almost always a matching example in the .NET world. The writer comes up with loads of interesting examples and case studies to fortify his statements and it's apparent that he brings a lot of experience out of the field.<br /><br />Generally the book is about large scale (web-)projects but there are plenty of interesting points worth to delve into, even if one's current projects are not (yet) in need of sophisticated load balancing technologies.<br /><br />Some catchwords from my personal things-to-remember-list of this title:<br /><ul><br /><li><span style="font-style: italic;">Circuit Breaker</span> (together with <span style="font-style: italic;">Timeouts</span>) is a very interesting pattern for dealing (even monitoring) with integration points of a system.</li><br /><li>Beware of <span style="font-style: italic;">Chain Reactions</span> in a system. Prevent with <a href="http://en.wikipedia.org/wiki/Bulkhead_%28barrier%29"><span style="font-style: italic;">Bulkheads </span></a>if necessary.</li><br /><li>Communicate transparently and often. <span style="font-style: italic;">"Good marketing can kill you at any time." (Paul Lord)</span></li><br /><li>Keep your SLA in mind while dealing with dependencies.</li><br /><li>"Data purging never makes it into the first release, but it should."</li><br /><li>Conway's Law: <span style="font-style: italic;">"Organizations which design systems are constrained to produce designs whose structure are copies of the communication structures of these organizations."</span></li><br /><li>Keep the session timeouts (and the dead time before that) in mind. (Not only when speaking of 'concurrent users'). Ideally it should be set to one standard deviation past the average think time.</li><br /><li>Consider involved threads when using resource pools - use a resource pool size equal to the number of threads to guarantee that every thread immediately gets the resource it needs.</li><br /><li>AJAX should be called AJAS because the <a href="http://en.wikipedia.org/wiki/JSON">JavaScript object notation (JSON)</a> format is much better suited (easier to parse and less chatty) to transfer data. (Yes, we knew this one before. Honestly.) However even JSON has a dark side. Its gates are <span style="font-style: italic;">eval()</span>.</li><br /><li>There is another reason behind using Cascading Style Sheets instead of HTML tables besides conciseness and code organization: bandwidth.</li><br /><li>Only starting a new thread is more expensive than creating a database connection.</li><br /><li>Make your application cache configurable.</li><br /><li>Use soft references. (Java has <a href="http://java.sun.com/javase/6/docs/api/java/lang/ref/SoftReference.html">it</a>. Does .NET?)</li><br /><li>Load-balanced clusters do not scale linear due to overhead in heartbeats and state synchronizations. Fully load-balanced farms almost do.</li><br /><li>The <a href="http://en.wikipedia.org/wiki/Zero_One_Infinity">ZOI</a> paradigm of software design should be considered in QA.</li><br /><li>Don't mix production configuration with basic plumbing (like <a href="http://en.wikipedia.org/wiki/Dependency_injection">DI</a>) in the same configuration file.</li><br /><li>There is a project called <a href="http://roc.cs.berkeley.edu/">Recovery-Oriented Computing (ROC)</a>.</li><br /><li>The 3 phases of <span style="font-style: italic;">Zero Downtime Deployment</span>: Expansion, rollout and cleanup.<br /><br /></li><li>The difference between white- and black-box (monitoring) technologies: The first are running within the system under observation while the second reside on the outside and are not known by the system itself. Naturally black-box technologies are lesser coupled to the system.</li><br /><li><a href="http://en.wikipedia.org/wiki/John_Boyd_%28military_strategist%29#The_OODA_Loop">Colonel John Boyd and the OODA-Loop</a> can be mapped to an iteration in agile development.</li><br /><li>The author brings up a nice definition of cohesive in software design: "...In other words, if a set of methods touches only a subset of the object's state but is unaffected by other aspects of the object's state, the object is not cohesive."</li><br /><li>...and many many more...<br /></li></ul><br /><br />I have read the book cover to cover and can recommend it to anyone interested in delving a little deeper in the subject.Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-37875136407465023332007-12-16T13:23:00.000+02:002007-12-18T22:16:45.975+02:00The lowest common denominatorThere's a lot of discussion about functional programming these days. Not everyone might think that functional additions to a language are such an essential benefit. But as you might know until now I think it really is. I think that a functional programming style can lead to a higher form of abstraction. And more abstraction in your code leads to lesser lines of code which leads to fewer bugs.<br /><br />In our current project we're working with C# 2.0. But some us are really looking forward taking advantage of the C# 3.0 features. Though we all agree, that not everyone might have the ambition, the time or the interest to make himself deeply familiar with the new features within a narrow time frame. This leads me to another currently much discussed topic in the blogosphere: <a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/12/12/my-pick-for-altnetconf-quote-of-the-day.aspx">the lowest common denominator</a>. Does a feature lead to better code if there are still a lot of people not understanding it?<br /><br />Well, it depends. Do you actively try to leverage programmers which are behind? Do you have a review process in your <a href="http://en.wikipedia.org/wiki/Systems_Development_Life_Cycle">SDLC</a>? Do you have a critical mass on programmers in your project already familiar with the technology or at least willing to adopt and master the new features? Do you have a culture in your company where fascination is getting nurtured?<br /><br />What do you think? Do you have own experience in adopting new technologies within a project lifetime?Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-68959057716879136792007-12-09T23:23:00.000+02:002007-12-10T23:28:14.489+02:00Ambition vs. LINQIf you would be given the opportunity to choose your data query abstraction language from those two examples:<br /><br /><a href="http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx">LINQ</a>:<br /><code>products.Where(p => p.Category == "Food").OrderBy(p => p.Price).Select(p => p.Name).Take(2);<br /></code><br /><br /><a href="http://errtheblog.com/posts/64-even-more-ambitious">Ambition</a>:<br /><code>Product.select {|p| p.category == "Food" }.sort_by { &:price }.map{ &:name }.first(2)<br /></code><br /><br />Which of these implementations would you prefer? I really like them both, though the second one is even more concise.Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com2tag:blogger.com,1999:blog-8468321368360009794.post-58733421655105186702007-11-28T22:53:00.000+02:002007-12-05T00:17:29.572+02:00String#to_proc<a href="http://weblog.raganwald.com/">Raganwald </a>is one of my favorite bloggers. Every entry he writes inspires me and makes me think. A while ago he <a href="http://weblog.raganwald.com/2007/10/stringtoproc.html">ported</a> String#to_proc from Oliver Steele's <a href="http://osteele.com/sources/javascript/functional/">functional javascript</a> library to ruby. You have to read his entry for yourself but one thing that caught my eye was his definition of the factorial function using String#to_proc:<br /><br /><code><br />factorial = "(1.._).inject &'*'".to_proc<br />factorial[5] # -> 120<br /></code><br /><br />I don't know about you, but I think <a href="http://raganwald.com/source/string_to_proc.rb.html">implementations </a>like this are showing us whats possible in this language.<br /><br />Never stop writing raganwald! Thanks.Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com2tag:blogger.com,1999:blog-8468321368360009794.post-77996847918284688102007-10-19T14:26:00.000+02:002007-10-19T14:43:59.385+02:00Currying und partielle Funktionsapplikation Part 2<a href="http://hulisblog.blogspot.com/2007/10/currying-und-partielle.html">Letztes Mal</a> habe ich Currying und partielle Funktionsapplikation in C#, Scheme und Ruby verglichen. Da ich aber funktionale Programmierung hauptsächlich in meiner Freizeit studiere und meine Brötchen immer noch vorwiegend mit .NET verdiene, ist es für mich - eine zugegebenermassen höchst angenehme - Pflicht, das ganze noch kurz in <a href="http://research.microsoft.com/fsharp/fsharp.aspx">F#</a> anzuschauen.<br /><br />Deshalb hier also nur ganz kurz als Ergänzung zum letzten Post das Beispiel in F#:<br /><pre class="codebox"><br />#light<br />let add a b = a + b<br />printf "result %i" ((add 2) 3)<br /><br />let inc = add 1<br />let result = inc 3<br />printf "\nresult %i" result<br /></pre><br />Huh!? Currying wird in F# (wie in Haskell auch) schon von der Sprache selbst unterstützt und es ist hier möglich, eine beliebige Funktion ohne Umweg partiell zu applizieren.<br /><br />Nice.Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-29364622397628550662007-10-17T12:54:00.000+02:002007-10-19T22:47:20.803+02:00Currying und partielle FunktionsapplikationVor einiger Zeit habe ich mich (<a href="http://blogs.msdn.com/wesdyer/archive/2007/01/29/currying-and-partial-function-application.aspx">hier</a>) mit <a href="http://de.wikipedia.org/wiki/Currying">Currying</a> und partieller Funktionsapplikation in C# 3.0 beschäftigt. Ich will in diesem Eintrag die Implementation von Wes aufgreifen und anschauen wie eine solche in Scheme und Ruby aussehen könnte.<br /><br />Grundlage ist die simple Addition:<br /><pre class="codebox"><br />Func<int,int,int> add = (x,y) => x + y;<br /></pre><br />Die Funktion Curry für eine generische Funktion mit zwei Argumenten kann in C# als <a href="http://msdn2.microsoft.com/en-us/library/ms364047%28vs.80%29.aspx#cs3spec_topic3">Extension Method</a> implementiert werden und sieht in der Implementation von Wes Dyer folgendermassen aus:<br /><pre class="codebox"><br />public static Func<A, Func<B, R>> Curry<A, B, R>(<br /> this Func<A, B, R> f)<br />{<br />return a => b => f(a, b);<br />}<br /></pre><br />Mit dieser Funktion ist es also möglich, die Argumente schrittweise zu binden wie wir in der Anwendung klar erkennen können:<br /><pre class="codebox"><br />Func<int,Func<int,int>> curriedAdd = add.Curry();<br />Console.WriteLine(curriedAdd(3)(5)); //8<br /></pre><br />Wie Wes weiter erklärt, kann dieses Konzept weiter generalisiert werden in ein Konzept mit dem Namen 'partielle Funktionsapplikation'. Mit diesem Konzept ist es möglich aus einer Funktion eine ganze Familie von Funktionen zu erzeugen. In unserem Beispiel wollen wir bei der Funktion add das erste Argument mit 1 binden und so eine triviale Inkrementierungs-Funktion erhalten.<br /><pre class="codebox"><br />Func<int, int> inc = add.Partial(1);<br /></pre><br />Die Funktion Partial zitiere ich auch wieder direkt von Wes. Sie sieht folgendermassen aus:<br /><pre class="codebox"><br />public static Func<B, R> Partial<A, B, R>(<br /> this Func<A, B, R> f, A a)<br />{<br />return b => f(a, b);<br />}<br /></pre><br />Thanks again, Wes!<br /><br />Wenn ich mir das so anschaue, erinnert es mich ein klein wenig an das <a href="http://de.wikipedia.org/wiki/%C3%9Cberladen">Überladen von Methoden</a> in der Objektorientierung. Mit beiden Konzepten ist es möglich, den Aufruf einer Funktion/Methode zu vereinfachen indem bestimmte alternative Signaturen angeboten werden. (Die Puristen unter euch mögen mir diesen profanen Vergleich verzeihen!)<br /><br />Als relativer Anfänger im Umgang mit Scheme habe ich mir angeschaut wie eine solche Implementation in diesem LISP-Dialekt aussehen könnte. Für die Curry-Funktion bin ich auf folgende Implementation gekommen.<br /><pre class="codebox"><br />(define add<br /> (lambda (a b)<br /> (+ a b)))<br /><br />(define curry<br /> (lambda (f)<br /> (lambda (a)<br /> (lambda (b)<br /> (f a b)))))<br /><br />(define curried-add (curry add))<br />((curried-add 3) 5) ;; => 8<br /></pre><br />Auffallend ist hier, dass ich kein funktionales Äquivalent für 'Extension Methods' gefunden habe. Erfahrenere Scheme Benutzer könnten hier vermutlich mit einer prägnanteren Implementation aufwarten.<br /><br />Trotzdem hier auch noch die Implementation von Partial:<br /><pre class="codebox"><br />(define partial<br /> (lambda (f a)<br /> (lambda (b)<br /> (f a b))))<br /><br />(define inc (partial add 1))<br />(inc 3) ;; => 4<br /></pre><br />Jetzt wollen wir aber auch noch sehen wie das ganze in Ruby aussehen könnte. Zuerst die Funktion Curry:<br /><pre class="codebox"><br />add = lambda{|a,b| a + b}<br />class << add<br /> def curry<br /> lambda{|a| lambda{|b| self.call(a,b)}}<br /> end<br />end<br /><br />curried_add = add.curry<br />curried_add.call(3).call(6) # => 9<br /></pre><br />Hier habe ich es vorgezogen, Curry auf dem Objekt add zu implementieren da mir die Anwendung so einfacher schien. Und schliesslich auch eine Ruby-Version von Partial:<br /><br /><pre class="codebox"><br />class << add<br /> def partial val<br /> lambda{|a| self.call(a,val)}<br /> end<br />end<br /><br />inc = add.partial 1<br />inc 4 # => 5<br /></pre><br />Wenn ich die verschiedenen Implementation miteinander vergleiche, fällt mir persönlich zuerst einmal der etwas grössere Programmieraufwand für C# auf. Es wird wieder einmal die Tatsache deutlich, dass es sich um eine statisch typisierte Sprache handelt und wir zugunsten einer Typenprüfung beim Kompilieren etwas mehr schreiben müssen. Bei der Ruby-Version fällt vor allem der explizite Aufruf von Proc#call auf.<br /><br />Als nächstes gäbe es dann wohl den <a href="http://en.wikipedia.org/wiki/Y_combinator">Y-Combinator</a> in der verschiedenen Sprachen zu vergleichen.Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-90241183049394312072007-10-14T10:46:00.000+02:002007-10-14T11:34:27.955+02:00Mixins und KlassenmethodenAls ich zum ersten Mal dem Prinzip der Objektorientierung begegnete welches besagt 'Bevorzuge Komposition der Vererbung' fand ich dieses auf Anhieb einleuchtend. Obwohl Polymorphismus im Fall von einer 'is a'-Beziehung und einer noch nicht zu tiefen Klassenhierarchie sicher nahe liegend ist und eine perfekte Lösung darstellt, gibt es trotzdem viele Fälle wo eine solche Kopplung zu weit geht. (Und wir womöglich sogar das <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">LSP</a> verletzen würden!)<br /><a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html">Module</a> in Ruby sind für solche Situationen natürlich geradezu prädestiniert da sie uns erlauben, solche Beziehungen mit Mixins abzubilden. Mit Modulen kann in Ruby zudem auch das Konzept der Mehrfachvererbung implementiert werden.<br /><br />Nun hatte ich aber diese Woche die Anforderung, Klassen über Module mit Methoden zu erweitern und nicht Instanzen. Aufgrund der Art wie Ruby das Schlüsselwortes 'self' handhabt, lassen sich Klassenmethoden nicht ganz so einfach wie Instanzmethoden 'hineinmixen'.<br /><br />Aber wie immer mit Ruby war Hilfe nicht weit und die Dokumentation führte zu der Methode '<a href="http://www.ruby-doc.org/core/classes/Module.html#M001683">included(othermod)</a>'. Mit diesem Callback können nun auch Klassen mit Methoden erweitert werden ohne dass die Syntax vom Client her angepasst werden müsste:<br /><br /><pre class="codebox"><br />module Mixin<br /> def self.included other_module<br /> other_module.extend ClassMethods<br /> end<br /><br /> module ClassMethods<br /> def bar?<br /> true<br /> end<br /> end<br />end<br /><br />class Foo<br /> include Mixin<br />end<br /><br />puts Foo.bar? #=> true<br /></pre>Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-85240622893469899842007-05-30T23:55:00.000+03:002007-05-31T23:47:30.498+03:00Linq und MemoizationGestern habe ich mir ein <a href="http://blogs.msdn.com/wesdyer/archive/2007/01/25/video-on-linq-queries-and-delayed-evaluation.aspx">Video von Wes Dyer</a> angeschaut. In diesem Interview beschreibt Wes (ein Mitarbeiter des C#-Kompiler-Teams und ein ziemlich smarter Typ) die Vereinfachung eines <a href="http://www.dofactory.com/Patterns/PatternSingleton.aspx">Singleton-Pattern</a> mittels Extension Methods. Da mir das ganz gut gefallen hat, beschloss ich das ganze mit der Beta von 'Orcas' auszuprobieren und hier nachzuvollziehen. Nachfolgend also dieses Beispiel.<br /><br />Eine typische Implementation des Singleton-Pattern sieht meist etwa so aus:<br /><pre class="codebox"><br /> public class Foo<br /> {<br /> private static Foo _instance = null;<br /><br /> public static Foo GetInstance()<br /> {<br /> if (_instance == null)<br /> _instance = CreateFoo();<br /><br /> return _instance;<br /> }<br /><br /> private static Foo CreateFoo()<br /> {<br /> //some interesting stuff<br /> return new Foo();<br /> }<br /> }<br /></pre><br />Das ist soweit trivial und sollte keinem von uns Kopfzerbrechen bereiten. (Auf den Sinn und die Notwendigkeit des Patterns und allfällige <a href="http://pragmaticintegration.blogspot.com/2006/02/singletonitis.html">Singletonitis </a>wollen wir hier nicht eingehen) In einigen Sprachen ist dieses Pattern bereits sogar in die Standart-Bibiliothek integriert worden. (e.g <a href="http://www.ruby-doc.org/stdlib/libdoc/singleton/rdoc/files/singleton_rb.html">Ruby</a>)<br /><br />Etwas – zugegebenermassen nur sehr wenig – komplizierter wird es hier, wenn wir Gruppen von Instanzen verwalten wollen. Zum Beispiel stellen wir uns für das gegebene Beispiel vor, dass wir pro Zahl eine eigene Instanz verwalten wollen. Ein erster Versuch könnte uns zum Beispiel zu folgender Implementation führen:<br /><pre class="codebox"><br /> public class ComplicatedFoo<br /> {<br /> private static Dictionary<int,ComplicatedFoo> <br /> _dictionary = new Dictionary<int,ComplicatedFoo>();<br /> <br /> public static ComplicatedFoo GetInstance(int n)<br /> {<br /> ComplicatedFoo foo = null;<br /> if (!_dictionary.TryGetValue(n, out foo))<br /> {<br /> foo = CreateComplicatedFoo(n);<br /> _dictionary.Add(n, foo);<br /> }<br /> return foo;<br /> }<br /> <br /> private static ComplicatedFoo <br /> CreateComplicatedFoo(int n)<br /> {<br /> //some interesting stuff<br /> return new ComplicatedFoo();<br /> }<br /> }<br /></pre><br />Wir nehmen also erstmal ein Dictionary zu Hilfe um diese Struktur abzubilden. Wenn wir dies aber genauer anschauen, stellen wir fest, dass es sich hier um die Verbindung von einer Funktion (inkl. Parameter) und einem Resultat handelt. Mit anderen Worten: <a href="http://en.wikipedia.org/wiki/Memoization">Memoization</a>.<br />Memoization implementieren wir in C# 3.0 mittels einer ‚Extension Method’. Im unserem Fall mit einer Methode mit einem Argument und einem Resultat könnte diese Methode so aus:<br /><pre class="codebox"><br /> static class FunctionExtensions<br /> {<br /> public static Func<A, R> <br /> Memoize<A, R>(this Func<A, R> f)<br /> {<br /> var map = new Dictionary<A, R>();<br /> return a =><br /> {<br /> R value;<br /> if (map.TryGetValue(a, out value))<br /> return value;<br /> value = f(a);<br /> map.Add(a, value);<br /> return value;<br /> };<br /> }<br /> }<br /></pre><br />Wenn wir nun diese Memoization-Funktion auf unsere vorige Implementation anwenden, so führt uns das zu folgendem Code:<br /><pre class="codebox"><br /> public class FunctionalFoo<br /> {<br /> private static Func<int, FunctionalFoo> <br /> getInstance = ((Func<int, FunctionalFoo>)<br /> (n => CreateFunctionalFoo(n))).Memoize();<br /><br /> public static FunctionalFoo GetInstance(int n)<br /> {<br /> return getInstanceMemoized(n);<br /> }<br /><br /> private static FunctionalFoo <br /> CreateFunctionalFoo(int n)<br /> {<br /> //some interesting stuff<br /> return new FunctionalFoo();<br /> }<br /> }<br /></pre><br />Der Vorteil dieser Implementation ist natürlich, dass wir unsere Memoize-Funktion beliebig wiederverwenden können. (Gesetzt wir haben die entsprechende Signatur in unsere CreateFoo-Methode.)<br /><br />Ziemlich cool IMHO. Thanks Wes!Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-85339816175742731782007-05-20T12:18:00.000+03:002007-05-20T23:03:56.831+03:00Mocks vs StubsEin Blog welches ich seit einiger Zeit - mit der mir verfügbaren Sorgfalt - verfolge, ist jenes von <a href="http://blog.jayfields.com/">Jay Fields</a>. Ein weiterer ThoughtWorker der stets mit qualitativ hochwertigen Posts aufwartet. <br /><br />Den Unterschied zwischen Mocks und Stubs haben wir ja bereits <a href="http://www.martinfowler.com/articles/mocksArentStubs.html">hier</a> definiert. Bei meinen eigenen TDD-Streifzügen bin ich jedoch zur Überzeugung gekommen, dass auch hier wieder keines von beiden für sich allein genommen die silberne Kugel ausmacht. In einem bereits etwas älterem Post von Jay habe ich dann diesen, für mich sehr sinn-stiftenden Abschnitt gefunden, den ich gerne mit euch teile möchte:<br /><br /><blockquote><i><br />Using a mock to verify the class under test interacts in an expected manner with a dependency is natural. However, tests should verify behavior of a dependency independently of other dependencies. For all dependencies who's behavior is not currently under test a stub is preferred. By using stubs in this manner you convey the message that only the interaction of the class under test and the mocked dependency is important.<br /></i></blockquote><br /><br />Eine etwas mehr 'Absicht-getriebene' Definition. Und da ich versuche, Code zu schreiben, welcher die dahinter liegenden Absichten bestmöglich kommuniziert, gefällt mir diese stützende Beschreibung besonders gut.Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-21047694255505979382007-04-18T13:59:00.000+02:002007-04-19T21:10:20.378+02:00Python und .NET - IronPython<span style=";font-family:verdana;font-size:85%;">Da ich meine Tantiemen bekanntlich mit .NET verdiene habe ich letztens ein wenig mit IronPython - einer Python Implementation für die CLR - experimentiert. Richtig, in Sachen dynamische Sprachen ist mein Favorit Ruby. Aber schliesslich ist IronPython ja der ‚proof-of-concept’ einer dynamischen Sprache für die CLR, ist komplett in C# geschrieben und läuft auf dem .NET Framework 2.0 und auf Mono. Ausserdem wird eine mühelose, bidirektionale Integration mit ‚managed code’ versprochen. Gründe genug also meine Hirnzellen mit etwas syntaktischen Neuerungen bemühen zu dürfen.<br /><br />Die Python-Syntax ist ein wenig gewöhnungsbedürftig. So sind zum Beispiel die Einrückungen von Bedeutung. Sie ersetzen die geschweiften Klammern die wir zum Beispiel von C# her kennen.<br /><br />Eine Methode kann also zum Beispiel folgendermassen definiert werden:<br /></span><code><span style="color: rgb(51, 255, 51);"><br />>>> def addOne(x):<br />... return x+1<br />...<br />>>> print addOne(2)<br />3<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Eine triviale Klasse so:<br /></span><code><span style="color: rgb(51, 255, 51);"><br />>>> class MyClass(object):<br />... def doSomething():<br />... return 'in doSomething'<br />...<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Wir können die Methoden einer Klasse mit der Methode ‚dir’ inspizieren. Als Rubyisten werden wir mit Reflection-Features natürlich geradezu verwöhnt.<br /></span><code><span style="color: rgb(51, 255, 51);"><br />>>> t = MyClass()<br />>>> dir(t)<br />['__class__', '__dict__', '__doc__', '__init__', '__module__', '__new__', '__red<br />uce__', '__reduce_ex__', '__repr__', '__weakref__', 'doSomething']<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Meine Methode war also korrekt eingerückt. Vererbung ist natürlich auch möglich:<br /></span><code><span style="color: rgb(51, 255, 51);"><br />>>> class MySecondClass(MyClass):<br />... pass<br />...<br />>>> dir(MySecondClass)<br />['__class__', '__dict__', '__dict__', '__doc__', '__init__', '__module__', '__ne<br />w__', '__reduce__', '__reduce_ex__', '__repr__', '__weakref__', 'doSomething']<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Aber wir wollen jetzt etwas von .NET sehen. Fangen wir an mit Datenstrukturen. Am besten mit einem Hash. Genauer: Einer .NET-Hashtable in IronPython. Nach einigen Gehversuchen sieht das dann so aus:</span><br /><code><span style="color: rgb(51, 255, 51);"><br />C:\Program Files\IronPython-1.0.1>ipy.exe<br />IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42<br />Copyright (c) Microsoft Corporation. All rights reserved.<br />>>> import System<br />>>> from System.Collections import *<br />>>> h = Hashtable()<br />>>> h["a"] = "Ruby"<br />>>> h["b"] = "Smalltalk"<br />>>> h["c"] = "IronPython"<br />>>> for e in h: print e.Key, ":", e.Value<br />...<br />a : Ruby<br />b : Smalltalk<br />c : IronPython<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Das ist doch schon mal etwas. .NET-Datentypen und Python-Syntax. Eine aus sozialer Sicht durchwegs tolerante Hochzeit. Um weitere .NET-Bibliotheken benutzen zu können, müssen wir aber das CLR-Modul laden und die einzelnen Referenzen hinzufügen. Im Moment geschieht also noch folgendes:</span><br /><code><span style="color: rgb(51, 255, 51);"><br />>>> d = XmlDocument()<br />Traceback (most recent call last):<br /> File , line 0, in <stdin>##11<br />NameError: name 'XmlDocument' not defined<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Wenn wir versuchen den Namespace zu importieren geschieht auch noch nicht viel mehr:<br /></span><code><span style="color: rgb(51, 255, 51);"><br />>>> from System.Xml import *<br />Traceback (most recent call last):<br /> File , line 0, in <stdin>##12<br /> File , line 0, in __import__##4<br />ImportError: No module named Xml<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Aha. Das Modul, resp. die Referenz muss also erst hinzugefügt werden. So wie wir es in .NET-Code auch tun würden. Allerdings müssen wir hier erst noch das CLR-Modul laden damit wir diese Referenzen richtig hinzufügen können:</span><br /><code><span style="color: rgb(51, 255, 51);"><br />>>> import clr<br />>>> clr.AddReference("System.Xml")<br />>>> from System.Xml import *<br />>>> d = XmlDocument()<br />>>> d.ToString()<br />'System.Xml.XmlDocument'<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Eigene .NET-Bibliotheken lassen sich übrigens ganz ähnlich importieren:</span><br /><code><span style="color: rgb(51, 255, 51);"><br />>>> import clr<br />>>> clr.AddReferenceToFile("mylibrary.dll")<br /></code></span><span style=";font-family:verdana;font-size:85%;"><br />Um jedoch auch WinForms-Komponenten benutzen zu können und zum Beispiel ein Form anzuzeigen müssen wir dafür sorgen dass der ‚Message-Loop’ richtig behandelt wird. Dazu finden wir nach der Installation von IronPython ein Batch-File ‚winforms.py’ im Unterverzeichnis ‚Tutorial’ welches einen entsprechenden Thread eröffnet und dies sicherstellt.<br /><br />Alles in allem lässt sich sagen, dass IronPython sicher ein interessantes Projekt ist. Es zeigt dass die CLR auch eine mächtige Plattform für dynamische Sprachen sein kann. Natürlich gefällt mir die Syntax und der Stil von Ruby besser. Das wird sich auch nicht ändern. Aber zumindest als kleine Versuchsumgebung für .NET werde ich sicher in Zukunft das eine oder andere Mal IronPython zu Rate ziehen.<br /></span></span>Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com1tag:blogger.com,1999:blog-8468321368360009794.post-57462976831079450422007-01-17T17:25:00.000+02:002007-01-17T19:45:45.197+02:00self.memory.get_chapter(:ferret).mark!<span style=";font-family:verdana;font-size:85%;" >Some private projects were keeping my quiet busy in the last weeks. One issue which was absorbing some of my time was the ruby port of lucene: ferret. To be specific it was not even ferret itself. It was my ignorance. But I am smarter now.<br />I had problems with our swiss special character set. If trying to save an entry in rails I always the same error:<br /><code><br /><span style="color: rgb(51, 255, 51);">Exception (: Error occured at </span><analysis.c><span style="color: rgb(51, 255, 51);">:704</span><br /><span style="color: rgb(51, 255, 51);">Error: exception 2 not handled: Error decoding input string.</span></analysis.c></code><br /><br />Yes. That one was quiet obvious. There had to be a problem with my locales. Well, unfortunately I was searching in the wrong corners and my first solution was setting the locales in my rails environment so I added<br /><code><br /><span style="color: rgb(51, 255, 51);">ENV['LANG'] = 'de_DE.UTF-8'</span><br /></code><br />to my environment.rb. But nothing changed. Still the same error. Okay, lets ask one of my close friends: Googlix. He revealed that maybe another addition in my environment file would work:<br /><br /><code style="color: rgb(51, 255, 51);">$KCODE = 'u'<br />require 'jcode'</code><br /><br />Okay. But nope. That wasn't it either. I should have taken a nap. I should have put myself an hour in my samadhi tank. (If I only had one of those!).<br />Finally I decided that the Issue must be not one of my rails setup instead I got suspicious about my operating system. Maybe I misconfigured it. Okay so fire some configuration up:<br /><br /><code style="color: rgb(51, 255, 51);">dpkg-reconfigure locales</code><br /><br />Hell no! I didn't have the corresponding package installed for 'de_DE.UTF-8'!! I must have been asleep while setting up my new debian sarge. Well, don't blame yourself. Just don't make the same mistake twice!</span>Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-32671957711194582222006-11-21T22:11:00.000+02:002006-11-21T23:03:24.985+02:00RSpec on Rails<span style="font-family:verdana;"><span style="font-size:85%;">I have played with RSpec before and I liked it. But yesterday I was trying to bring RSpec on<br />Rails. The installation process was meant to be quiet easy:<br /><br />1. Installing rspec gem<br />2. Installing the corresponding version of the plugin<br />3. Generating rspec and moving with rocket speed ahead.<br /><br />This 3 Steps where easy. Writing the first spec for my model as well. Running rake spec<br />however turned out to be not as easy as expected. Instead I got:</span><br /></span><br /><br /><pre><span style="font-family:courier new;"><span style="font-size:85%;color:#33ff33;">c:/ruby/lib/ruby/gems/1.8/gems/ZenTest-3.4.2/test/test_help.rb:5:<br />superclass mismatch for class TestSession (TypeError) from<br />c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'</span></span></pre><pre><br /><span style="font-family:verdana;font-size:85%;">Superclass mismatch. Ouchh! Errors like that are usually showing up when running migrations<br />and having multiple definitions of the same class (A second definition in the migration file for<br />example). But TestSession was definitely not a class of my domain model.<br /><br />Analyzing the listed source didn't bring me further either. Then my next assumption brought my<br />to realize that I had to install the dependency ZenTest for successfully installing the gem<br />rspec (0.7.2). I knew I had the correct version of the plugin and the gem. So I rolled my ZenTest<br /><br />gem back from version 3.4.2 to 3.4.1:</span></pre><pre><br /><span style="font-family:courier new;font-size:85%;color:#33ff33;">c:\>gem install zentest -v 3.4.1</span></pre><pre><span style="font-family:georgia;"><span style="font-family:verdana;font-size:85%;">Then again running 'rake spec' and guess what:</span></pre></span><p><span style="font-size:85%;color:#33ff33;">.....<br /><br /><span style="font-family:courier new;">Finished in 0.16 seconds</span> </span><span style="font-size:85%;color:#33ff33;"><br /><span style="font-family:courier new;">1 specifications, 1 failures</span> </span><span style="font-size:85%;color:#33ff33;"><br /></p></span><p><br /><span style="font-family:verdana;"><span style="font-size:85%;">And this was exactly the output I was looking for since I do TDD and a failing test/spec is the start point for my implementation.<br /><br /><br />Version issues. I hopefully will not be troubled by these the next...couple of... ...years. ;)</span><br /><br /></span><span style="FONT-STYLE: italic"></span></p>Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0tag:blogger.com,1999:blog-8468321368360009794.post-40985853810015994322006-09-29T13:57:00.000+02:002006-11-22T09:42:59.020+02:00swiss_people.select{ |x | x.interests.include?("ruby")}<span style="font-size:85%;"><span style="font-family:verdana;">Yesterday we had our first swissrug-meeting. A bunch of ruby people talking together and having a dinner in zuerich.</span><br /><br /><span style="font-family:verdana;">Was quiet nice and there's more to come. I hope next time there will be some talks or presentations but for now thats a good start.</span></span>Christoph Hiltyhttp://www.blogger.com/profile/18370057020040313052noreply@blogger.com0