Promoting common sense.
Alternatively: Promoting uncommon sense and exposing common nonsense
Saturday, December 26, 2009
Challenge yourself
The assignment for my first contribution was to make an extension method for one of the new types in .net 4.0. What I made was an extension method that would return a text presentation of a BigInteger in whatever base you want (16 being the maximum as I don't know any standard for higher bases).
The second assignment was to implement group functionality for collections that would take several properties to group by. I chose to make an extension method that would take strings as input and group the collection by the values of the properties identifier by the strings. The result is a collection that contains a collection for each group. This was my first time for using Linq together with reflection.
Friday, December 18, 2009
The design flaw of foreach
I do realize that it makes sense to throw an exception when no input is provided. The problem is that I have never seen an application that expects an exception from the foreach contruct.
With foreach you must know that the collection is set, or wrap the whole thing in a check for null. This adds an extra codeblock to your code.
I have created an extension method for collections so I can avoid the extra codeblock and do something like this:
IList<SomeObject> list = GetObjects(); foreach(SomeObject o in list.NullChecked()){ //DoStuff }
Even if the GetObjects() method returns null your code wont fail. It just doesn't execute the loop.
public static IEnumerable<T> NullChecked<T>(this IEnumerable<T> input){ if(input!=null){ return input; }else{ return new List<T>(); } }
Tuesday, December 08, 2009
The Gartner Report
Here's my vision for the future:
In the future things will be different.If you want this elaborated you have to buy my report which I will have to make up. :)
Monday, December 07, 2009
Easy loops
//do something 3 times 3.Times().Do(i=>Console.WriteLine("Sometext")); //count an integer from 3 to 8 (both included) and do some action for each value. 3.To(8).Do(i=>Console.WriteLine("The number is: " + i ))
I know this is sort of thing is common in languages like Ruby or F# but that's not gonna stop me from doing it in C# :)
Saturday, November 14, 2009
Wrapping objects with interfaces
Tuesday, November 10, 2009
Dynamic duck typing in .Net 3.5
Friday, November 06, 2009
Extensions methods FTW
Here is a collection of my favourite homemade extension methods. Most have been included in the plain framework.
#1: My extension method for making !string.IsNullOrEmpty(somestring) more readable.
#3: Execute an operation on a collection. I use this all the time and I also have a version that can return the result of an operation as a new collection. That is basically equal to what the .Select extension from System.Linq does. The functional approach of F# inspired me to this.
#4: Get a typesafe value from a web request
#5: Gets all the controls of a certain type from a form. Originally I also had a version that operated on any type of object. I soon found out that searching an object structure without any constraints is a performance disaster. Being limited to finding controls on a form is much better as it's a well defined hierarchy that easy and fast to traverse. The idea springs from looking at JQuery. Some examples of usage can be found in my blogspot about oneliners in .net
#6 Save an object. This operates on types not defined yet. If you implement the IBusinessEntity<> interface you will automatically get a Save() method. It gives a feeling and ease of use as that of the Active Record pattern, without the object knowing anything about persistence. The plain framework also contains extension methods for Delete() and Validate().
Thursday, November 05, 2009
Flank your problems
When I saw them playing I noticed a pattern I have seen in software development. All the kids were chasing the ball at once. This made it a somewhat confusing match to watch. The problem was not that the girls lacked the technical skills to play football or that they didn't understand what the goal of the game was. The problem was that they were so entutiastic about getting the ball to the other end of the field that they all rushed to the ball to help the team get the ball to the other end.
I think this anti pattern should be called something like "Beeing too specialized" or "Follow the leader". The pattern to overcome this would be the "Think before you act" or "Flank your problems".
Thursday, September 10, 2009
One liners in .Net
Just wanted to show a few one liners from a project I'm working on. I get a lot of data from a third party as XML. In my business logic I want to work with objects so I have made a class that wraps on entity in the XML.
var orgs = doc.SelectNodes("//Organisation").Cast<xmlnode>().Do<xmlnode,wrappedorganisation>(x => new WrappedOrganisation(x));
So in this line all the organisations in the xml are located and a list of WrappedOrganisation is created from it. With the next one we had to hide a lot of textboxes on a page if a related hidden field had the value "0".
Page.findControl<placeholder>().Do(x => x.Visible = x.findControl<hiddenfield>().Count(h => h.Value != "0") != 0);
The next one retrieves a list of custom objects from some textboxes on a UI.
IList<customvalues> list = UserControl.findControl<textbox>().Do(x => timeSpecs.Add(new CustomValues() { Amount = int.Parse(x.Text), Month = StartDate.Value.AddMonths(i++) }));
Finally a line that identifies TextBoxes on a UI with a value that is not an integer and marking them.
UserControl.findControl<textbox>().Where(x => !int.TryParse(x.Text, out i)).Do(x => x.CssClass = "borderRed").Do(x => messages.Add("Not a valid amount"));
Friday, August 28, 2009
Labour is not just labour
Monday, August 17, 2009
Going to Jaoo
If its going to be anything like the last two times I've been there my head will be exploding with new ideas when I get back.
Saturday, August 15, 2009
Defining knowledge
I don't think knowledge is something you can measure as such. You can take a lot of courses but if you are not able to put it in context you cannot use it for much. To me knowledge comes from exact facts combined with experience. In our line of business we sell our creativity which is based on our knowledge... Measuring creativity is a bit hard.
Thursday, August 13, 2009
Learning to read
Wednesday, August 12, 2009
Is EF an ORM
I have previously discarded the framework for the fact that it didn't support POCO. They have tried to recover by implementing IPOCO but its a workaround that doesn't really fit the issue of not supporting POCO. Not supporting POCO means that Entity Framework introduces dependencies to your domain model of Entity Framework that are not necessary and forces you to use your domain model in a certain way.
So why is this bad? A few extra lines of code in an entire application isn't too bad. It really isn't, but the purpose of the ORM is to create a persistent ignorant model. If we take plain as an example plain would have to be changed to support EF. Suddenly the nice layering would be broken because persistance logic would have to be applied directly in the domain logic or the UI layer.
As far as I understand version 4.0 of Entity Framework supports persistent ignorance better.
Tuesday, August 11, 2009
Software Factories are Evil
A resent pod cast on .net Rocks and an article in version2.dk reminded me of what I wanted to write.
I think code generation is a sign of bad design. Code generation is a sign of a need for repetitive code. Repeating code is the certain path to errors.
The worst kind is the one where you are supposed to edit some of the resulting code. The purpose of the process is to let you specify something in an abstract manner and have the details of the system taken care of by the generator. Once you edit the code you brake the abstraction and you will no longer be able to make changes to your application using the high abstraction. Examples of this could be the Windows Forms editor in visual studio. When you drag components on to your form, a lot of code is generated behind the scene. If you have to change anything in the generated code you might loose it later if some components are moved around on the design surface.
Another way of generating code is to generate static code that doesn't need to be changed by the user. This can be done to create a structure that the user can utilize in his/her program. I'm suggesting that you avoid this as well if possible. The bad impact on your program is not as big as on the previous method though. The reason for applying this could be to work as a shortcut to make something compile together.
An example of this could be the designer file for aspx files in visual studio. When visual studio parses an aspx file an C# file is created so it can be compiled as part of a C# project. The problem with this file is that it adds no new information to the project, but adds complexity in terms of extra files. Once in a while this file will be out of sync and you have to restart visual studio.
Initializing an object migth be so compex that you need a sniplet to assist you every time. If this is the case you should create a simpler way to initialize your object rather than create a sniplet for it.
My recomendation is therefore that if you are in need of code generation in your project you should reconsider your application design.
Thursday, August 06, 2009
World domination: Google vs. Microsoft
The two companies have different market strategies. As for google product I use them because they are cheap and good. With MS its much more that there are no alternatives. This makes me reluctant every time I get a new product to use by MS. Its not that the products are not great. Its the question of what other products I will be forced to use. I think that if MS products were not so sticky and so dependant on other MS products I would probably still be using them because some of them are quite good. I would do so even more happily than today. I would be able to use other vendors for just those part where MS products didn't fit.
Wednesday, August 05, 2009
Documents versus contextual information
A document is basically a black box where you can dump your data. You can find your data by a folderpath and a filename. Thats basically what metadata you get for your document. For me I might as well put the document straight in the trashcan because I know I won't be able to find the document when I need it in a month when there are several versions of the document and a lot of documents have been written since.
The contextual information is different. I'm in particullar thinking of wikis. Here information is normalised to its 3rd degree, not being repeated. When you need the information somewhere you just make a reference to it.
It's an uneven match. Is your information to be used or are they just to be archived.
Monday, August 03, 2009
DataSource, To bind it all.
We have found a way to bind a lot of the stuff contained in Plain together in an easy manner. When designing Plain we focused on few dependencies, high extendability, flexibility and ease of use. The ease of use is the one that can be hard to align with the other.
Plain is quite easy to use but very flexible. Code was however still needed to hook things together. Its no longer so. By using a Plain DataSource for ASP.Net, you are no longer needed to write any code to hook things together. Just place a DataSource on your page and define what domain class it should operate on and point it to the query ( defined in your business layer). The beauty of using a datasource is that a lot of standard .net usercontrols know how to use it to retreive and send data. DataSource objects also contain information about paging and sorting, and your UserControls will be able to take advantage of that just by enabling them to do so.
Wednesday, July 08, 2009
The good and bad of recent .Net
- Extension methods
I must admit that I can't get enough of extension methods. They make it much easier to create powerfull structures that are very easy to use, without compromising your good architecture. - Lambda expressions
These are not much of a revolution, but I like having a more compact way of writing code. - WCF
WCF scores high on the simplicity and extendability. I'm not sure I would have designed it the same way, but it does the job. It's coolest feature is that you can start out simple and add complexity a long the way. - F#
F# brings functional programming to .net. The functional paragime brought to .net is very exciting and it will hopefully inspire a lot of people out there.
The Bad:
- Silverlight
This framework has been hyped a lot. Although it has a lot of cool features, its lacking the most important one: Interoperability. If I choose silverlight I'm still forcing all my user to windows. - WorkFlow Foundation
I had high hopes for this framework. I have previously worked on workflow systems and was looking forward to having a framework that would ease the creation of workflow systems. I think the problem is that workflow foundation wants to do too much. Anything is possible and that makes it closer to a generic than to a dsl for workflows. I would have preferred a more specialized framework. - Entity Framework
The good thing is that more .net developers have now heard of ORM. The bad thing is that the ideas with EF are quite bad and I would choose another ORM anyday. Too bad this means that more and more will use EF and at some point we are probably all going to be forced into using it. - MSTest
This product doesn't really make a difference. It doesn't provide anything that was not out there already. My preference is still NUnit. - TFS
If you haven't tried this, then don't mind. It is way too ambitious and too complex to use. - Monodevelop
Mono has come a long way. The big problem is and will always be that Mono will always be behind ms .net. Mono has serious problems in being compliant with .net because .net is so closed.
Tuesday, June 09, 2009
Sales people explained
It is common knowledge that communicating across continents can be a challenge because of cultural differences. It's the same when communicating with sales people. These are the main differences to "normal" people as I see it.
- A more liberal approach to the truth or maybe just a more ignorant attitude towards facts. They are often only interested in the good story. I you want to hear about the perfect world or solution, do contact a sales person.
- A low signal to noise ratio.
The goal for communication for a sales person is the communication itself. Whether any useful information is communicated is often not important. The entropy of a conversation can therefore be very low. Often it's just not worth the communication. - Have no interest in, and no knowledge of what they speak of. The best feature of a sales person is his ability to small talk. Most of the time he won't have the slightest idea about what he is selling.
Friday, May 22, 2009
Bulk calling of methods
Sunday, May 17, 2009
When did things stop being natural
I'm just a bit confused with a word. To begin with everything was natural. When you apply a natural process to a natural substance, it will remain natural. So when and how did some things become unnatural?
Thursday, May 07, 2009
Creating a bug report
- Get to the point. It’s about a bug, and that’s what you should be writing about. It is of no in-terest why this bug is so important to you.
- Explain the context. Al though you might think the context is obvious, explain it anyway. Explain how you can reproduce it.
- Be polite. The people that are to fix the bug are probably very enthusiastic about fixing your problem. By calling them names they will be less so. They don't necessarily have anything to do with the bug in the first place.
- Give it a priority.
Wednesday, May 06, 2009
Validations in Plain
Although we were not aware of the NHIbernate Validation (and I guess the other way around) the way the validations are defined are very similar. The biggest difference is that Plain validation is focused on the domain model and the UI. NHibernate is focused on the domain model and the data layer. Plain Validation has (or it is supposed to have) translations for displaying validation errors directly in the UI.
As the two validation frameworks have very similar capabilities, we might make a mapping for you.
Programs I'll lay out for a fresh install
I have thought of this as well, and the conclusion is that the only program I need to be bothered installing is apt-get. When I have apt-get installed and it is pointed to the repositories I want, any other program is an ease to install and won't take long. I can wait untill I need that program to install it.
This of course only goes when I'm on Linux. There is no apt-get on Windows, and installing a fresh machine will take a looong time. Keeping track of licenses makes the process even longer. I won't go into that. :)
Monday, May 04, 2009
Being pragmatic
I'm a big fan of being pragmatic. A big mistake I see is that people think that if you are pragmatic you should not be concerned with doing things smart. This is wrong. I believe pragmatic is about spending the least amount of time creating a thing that fulfills a need.
If the need is to be able to maintain it, the pragmatic solution won't be to hardcode everything.
The pragmativc solution is not:
- Ignore common sense
- Start coding before you think
- Avoid design
Saturday, April 25, 2009
Configuring Sharpdevelop for web projects
Luckily my good friend Kristian Gundry has fixed this for me.
By providing the right parameters you can start a webserver. Either you can start the mono xsp2 webserver or you can use the one that comes with Visual Studio Express.
Happy happy joy joy! :)
Thursday, April 23, 2009
Puzzle
I provide you with a .net 3.5 dll that implements the class SomeClass plus some operations that operate on this class. The Isvalid method has no side effects, will throw no exception and always return true.
You run the following code:
Sunday, April 12, 2009
Note to self
Monday, April 06, 2009
Friday, April 03, 2009
Misunderstanding: The path to new ideas
At one point I though that all the problems people have communicating were holding us back. Even though a person has a brilliant idea, he might be the only one who truly understands it. A lot is lost in translation. Wouldn't the world just be magnificent if you just had to tell people things once and they would understand. There is just one little problem with this illusion. If we all just understood each other there would be little reason to interpret what you hear and come up with new ideas. Perception has a big role here.
So, the next time you are annoyed with somebody because they just don't get what you are saying to them or by people that can't explain anything in an easy manner, remember it is for a better cause. EVOLUTION! Even the simplest things can be misunderstood and will likely be. It is the reason we have come so far.
I have many times wished that I instantly could grasp the ideas of some of the great thinkers. Instead I have to read a lot of books and spend a lot of time interpreting what is written. My main source of inspiration comes from information that I don't understand.
My theory is that most ideas spring from existing ones that were misunderstood.
Friday, March 27, 2009
Alt.Net FTW!
It doesn't really matter if there is a movement. Alt.net is more about concept and a way of thinking than it is about a bunch of people. As long as people develop tools and components to use with .net and don't just wait for the next release from Microsoft, the spirit of alt.net will live.
So if you are a .net developer and your primary source of ideas comes from Microsoft then you don't have to care about what alt.net is. If you on the other hand keep looking out for better ways to do stuff, an alt.net meeting might be for you.
Saturday, March 14, 2009
Working on collections and searching for objects
What I'm striving for is the possibility to query an object structure for objects. When the objects are returned, it should be possible to perform actions on all these objects in one statement. LINQ might be usefull for querying but at the moment I'm guessing that it won't fulfill my needs.
Lets me give an example of what is currently in progress. This will find all TextBox objects that contain the value '0' and set the background colour on it:
form.find<textbox>("[.Text='0']").Do(t=>t.BackColor= Color.Azure);
The .find method is an extension method on object, which means you can use it for any object. The .Do method is an extension method to all IEnumerable<> that lets you perform the lambda function on all instances contained in the list.
Another example is by using the Map method.
IList<string> textboxValues = form.find<textbox>().Map<string>(t=>x.Name +": " + x.Text);
This will produce a list of strings with that name and value of all textboxes somewhere on the form. The Map method is currently not an extension method as I haven't figured out how to implement the syntax of just just providing one generic parameter to an extension method working on a generic type :(
Actually there is a Map extension method, but it requires you to provide the generic type of the collection as well. In this case it would be .Map<textbox,string>(...)
The one thing that is missing is the syntax to search for objects. Will it be LINQ or will it be a homegrown dsl?
If you want to use the .Do method here it is:
public delegate void OneParemeterNoReturn(T x);
public static void Do<t>(this IEnumerable<t> o,OneParemeterNoReturn<t> func){
foreach(T t in o){
func.Invoke(t);
}
}
otherwise you can wait for it to apear in the plain framework :)
Tuesday, February 10, 2009
Creating overloaded methods with extension methods
I want the interface to be simple to implement so it basically contains one method that has a number of parameters.
I want the interface to be simple to use, so I have implemented a number of extension methods that will work as overloads for the method specified in the interface.
So far so good. My next wish is to make the interface generic. This is where the everything falls apart. Apparently you are not able to make extension methods on a generic type. Every time i come across such mismatch between part of the .Net framework I am very sad. I wish the next version could just be a little coherent. :(