This blog contains reflections and thoughts on my work as a software engineer

mandag den 8. november 2010

Visual Studio likes to break my lines

I have always been irritated by Visual Studio for having automatically breaking lines whenever I completed a statement. For some reason I have lived with this for eons but suddenly today it got to me and I (mentally, for the sake of my co-workers) cried “DAMN YOU!!!” and started searching the Tools –> Options menu. After a mere two minutes I found this:

image

I could probably have spared quite a few hours of my life re-assembling lines of code being torn to pieces by Visual Studio after completing a line of code – lesson learned, don’t grow accostumed to irritation in your development environment for long periods of time because the solution could be no more than two minutes away

torsdag den 17. juni 2010

Norwegian Developer Conference – Day 2

I’m impressed… I’ve attended two different .NET debugging sessions today with Ingo Rammer. I kid you not – he is coding faster than Ayende himself. I don’t know the average amount of characters he was able to punch during a given timeframe but it was probably twice my own and I’m not exactly a slow writer. It was impressive – just like his sessions of debugging. I’ve got to know a load of features I had no idea existed in both Visual Studio and WinDbg. How do you debug a Windows Service which fails during startup? I know now… It includes using the geekiest tools available to you from Microsoft but it CAN be done.

I also attended two sessions with Jon Skeet. Today he shared with us his thoughts and wishes for C# 5 and how he would like the language to evolve. I can’t say I agree on much of it but he had some good thoughts about reducing boilerplate code when creating new instances of objects in various situations. But having polymorphic method overload in interfaces? I think not…

I’ve been a fan of DDD for quite some time (not ever succeeding with it on a project but even though you suck at football you can still be a fan, right?). I went to see Eric Evans about a talk reflecting upon his experiences and learnings after he wrote his much famous DDD book. Not the best performance but it was interesting to hear about i.e. domain events and their importance for a successful DDD experience if you take them into account and use them wisely. And I know a lot more about the .NET Service Bus and how it is the most important invention since Windows NT back in 1993. Don’t take my word for it – I believe Judal Lowy’s word weights a lot more than mine. He believes in it too so you should definately go for him instead of me  :o)

I’m off again – there’s a geek party planned about 9pm which is some 20 minutes from now. We’re leaving tomorrow so this is probably the last post for this week. I’ll recap the conference in a few days – see you then  :o)

onsdag den 16. juni 2010

Norwegian Developer Conference – Day 1

I’m attending the NDC 2010 and wow… The fatigue is settling by now after a nice dinner at a pizza-shop. I dare you: Have either of you ever paid 180 Norwegian Kroner ~ 28 US Dollars for a pizza without beverages? I knew that Norway was bloody expensive but I honestly didn’t see that one coming.

I attended seven sessions today Wednesday including the keynote kicking off the conference. I think I gave three or four green cards and two yellows… No red cards yet.   Sadly the two F# sessions this morning were cancelled so I watched Kevlin Henney at first talking about architecture and then Steve Strong walking us through the new features in .NET 4 System.Treading namespace. Quite interesting – I have a feeling that the usage of System.Threading is limited considering the day-to-day problems we face back at work but it’s good to know about the new stuff Microsoft has made available to us.

After that it was time for probably the best session for me today which was Scott Allen doing a talk about Modern Javascript. I know so little about the strengths and possibilities in Javascript so it was packed with information about features and core concepts that I had no clue existed. As a C# programmer hearing things like “functions as constructors” and “replacing the ‘this’ keyword with another object" rocked the boat quite a bit… I have decided to get to know Javascript better because my ignorance towards dynamic languages comes part from ignorance and part from the fear of the unknown, I guess. The only lasting solution to that problem is to take a deep dive into it and I was impressed even though I have to spend some time getting familiar with the concepts.

The session by Jon Skeet about Noda was interesting. I don’t think I share his passion for dates and times and I’ll probably sleep like a baby tonight despite that… I for one don’t really get a kick out  of a bug found in the New Zealand way of handling timezone restrictions but he managed to convince me that programming dates and times into an application is just as complicated as anything else which is the feeling I’ve had earlier. Earlier I almost felt a bit ashamed to not be able to figure out “simple” things with dates and times but the thing is: Dates and times in software development is hard, period. Jon says so, I’ve felt it on my own body – talk to the hand!

Until tomorrow…

tirsdag den 15. juni 2010

Norwegian Developer Conference 2010

I’m currently sitting in my hotelroom watching Brazil scoring an insane 1-0 goal against North Korea – I’ve spent the last 15 minutes planning the day tomorrow where I’ll be attending NDC 2010. I’ve never been to the conference before but a collegue of mine were here last year and I was a little envious when I learned the speakers attending the conference last year… This year it seems to be more focused on upcoming Microsoft-related technologies such as Windows Azure, F# and a bunch of C# stuff but there’s also plenty of room for i.e. MonoRails and lots of other stuff built outside of Seattle.

I have decided to plan a bit ahead. The other conferences I’ve attended earlier I wen to without much planning ahead and afterwards I had a sensation that I should have been elsewhere instead of attending sessions which I ought to have known couldn’t teach me very much. What’s the idea of attending a TDD 1-1 session when you’ve reached past that stage years ago? So tomorrow I’ll be switching tracks a bit. One of the worst things about such a conference is all the stuff you DON’T get to see live… There are podcasts and webcasts afterwards but it doesn’t beat the sensation of being there yourself of course. I’ll be following two sessions of F#, delving into Javascript with Scott Allen and I’m looking forward to seeing Jon Skeet presenting a ported version of a Java DateTime framework… I decided to go to his session for three reasons: Jon Skeet is there, I never questioned the System.DateTime namespace in .NET and Jon Skeet is there… I haven’t got the faintest clue how another framework could attack issues regarding date-time stuff so it’ll probably be either an eyeopener or time wasted I guess.

…and now Brazil is leading 2 against 0… I’ll bet that if North Korea against all odds gets away with one or even three points the national TV-station will broadcast the North Korean goals in a loop 24-7 the next five years or so   :o)

onsdag den 28. april 2010

MSTest results on CruiseControl using .NET 4

We’ve decided to upgrade our Visual Studio 2008 solutions to VS2010 and I had a few issues updating our buildserver – one of the most annoying problems was that the XSLT rendering the .NET 4 build output didn’t include our test results. My skills in regard of XSLT are – well, mediocre on a very good day - but I finally figured out that the namespace in (CCNET Installation folder)\webdashboard\xsl\MSTest9Report.xslt was wrong - the namespace was http://microsoft.com/schemas/VisualStudio/TeamTest/2006 and had to be changed to http://microsoft.com/schemas/VisualStudio/TeamTest/2010. Then my dear tests results were back to normal again. It caused me a bit of a headache because the XML was wellformed and the XPath was correct so it was really weird for a XSLT-n00b like me. It reminded me of the time where I used to debug Javascript by inserting “alert(‘123’)” into the code to see which if-clause got hit this time… What a great way to spend a few days at work that was  :o)

After changing the namespace everything is A-OK even though it annoys me a bit having to install Visual Studio 2010 on our buildserver in order to execute our MSTests. I haven't figured out a way to simply reference the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll assemblies - it just won't work for a number of reasons such as odd files missing, MSBuild errors and other various issues I’ve encountered while trying to hack my way through. Have anyone ever made MSTests run on a buildserver with no Visual Studio installed? Please let me know and you’ll be my friend for the day.

tirsdag den 13. april 2010

Refactoring

The software development community contains a huge amount of litterature with advices on how to “do” things. Do’s and dont’s are littered across the Internet – to be true I’ve also given birth to a few posts myself on various topics from time to time. Browsing the stuff one has written in the past can be quite an eyeopener in terms of “I-must-have-been-drunk-writing-that-piece-of-insanity-and-publishing-it-to-an-audience”… Any blogger with a decent record of blogposts across time probably has similar emotions towards their own blogposts otherwise you’re doing something wrong, I believe. You’re not learning anything and you’re definately not making enough mistakes on a daily basis. Only if you (such as myself) are doing regular f***ups on production environments and is the proud owner of www.iwanttoshootmyselfwithaslingshotforhavingreleasedthat.com – only then you’re in a position to discover new things about coding and yourself – and only then will you be able to reflect quitely upon the fact that truth is relative. What you believe today might not be what you believe in after another day at work tomorrow.

So – with that in mind it’s important for me to elaborate a bit from time to time over what I believe are rock-solid facts right now. Not tomorrow because I’ll probably screw up two or three times tonight during release and spend a few hours firefighting something which could have been avoided had I decided otherwise somewhere along the line. Then in a few years I’ll be able to look back on this post and think “Did I write that? Man, what a n00b…”.

As for now I want to tell you a bit about my thoughts on refactoring, how I think it should be done and the pitfalls I’ve fallen into while refactoring. Martin Fowler has written an almost mythological piece on refactoring and his Refactoring website is a good place to visit because there’s always new things to learn. Here goes

1: As always: Learn the basics, in this case about OO. If you don’t know the very basics such as “High coupling is bad” and don’t know when your code could benefit from an interface you won’t be able to recognize bad code from good code. Refactoring isn’t supposed to shape the code into something you alone like – that’s simply a waste of time. The overall goal is to improve overall readability and lift the quality of the code in terms that can be measured by tools such as FxCop and NDepend.

2: Refactoring is something you always do. Renaming a variable is refactoring – you don’t have to extract duplicate code into a method to qualify what you’re doing as “refactoring”. That’s also why agile tells us that “refactoring” tasks on the Sprint board is an antipattern. You should be refactoring 50% of the time while coding otherwise you’re doing something wrong.

3: Iteration, selection and statements – every line of code you’ve ever written has either been an iteration over something, a predicate or a statement. You can be aggressive refactoring iterations and statements because that’s often not very dangerous. Iterations and statements are changed when we rename and change visibility of methods, put duplicate code in base classes, replace magic numbers, consider recursion etc. That’s often not something to be afraid of. Changing behaviour however is a whole other story. You consider changing behaviour when you look at a monster of if-elseif-elseif-elseif…[snip]…else something and decide to introduce i.e. a Strategy Pattern. You also might go for refactoring that hideous substring-replace hell someone before you introduced to parse HTML strings because you know your Regular Expressions to the fingernails… I urge you to go for it but take your time writing those unittests. The reason you’re refactoring selections is that they are too complex for the average programmer to understand which is a proof to the fact that you probably don’t fully understand what the code is doing. How much of a moron are you if you actually think you can rewrite 100 lines of code you don’t understand into something smaller, prettier and still with the same behaviour intact? You can’t unless you really know what you’re doing (which you don’t) – and it is just a pain to have to explain to people on the floor why invoices are suddenly being sent twice to the wrong address because you missed something during your rewrite.

4: If you’re a C# programmer and have a pile of public methods you think nobody is using anymore – you can do three things: Delete, check in and pray. Or you could leave the methods as-is. Neither option is very good – you should deprecate the method instead and see if warnings start to pop up in other solutions. The [Obsolete] tag in C# is a tremendous efficient way of figuring out if public properties / methods / classes are being referenced anywhere outside your code. It’s perfectly safe and you can always go back and delete whatever you marked as Obsolete when you have made certain that there are no warnings appearing around your codebase.

5: Use tools. You’re not smart enough anyway. I know myself well enough to not trust myself at (almost) anything regarding code. Tools don’t lie – you ask, they respond. Ask WinGrep “I want to find .cs and .aspx files which contains ‘SomeNamespace.IWant.ToFind'” and it will find them for you. If you’re busting your brain to figure out assemblies where a piece of code could be used you should be using a tool to help you. The human brain is by far the most unreliable computer there is. You and all of the human race suck at being 100% accurate – that’s why man has built computers with software for you to use, damn it… I’ve only scratched the surface of NDepend but it has helped me already by clarifying some assumptions I had about our current codebase at work.

6: Don’t forget code readability. It’s not something which will improve your Cyclomatic Complexity level but it’s so important to be able to read your code. Really read – like a book. Your goal while programming should be to become the new Stephen King - in code. Or if a big guy with a beard from Hell is what turns you on you could always go for Martin Fowler. I’ll leave the details up to you. Beautiful code is easy to read and reveals intent. A fair amount of the refactorings suggested on Fowlers www.refactoring.com does indeed push for simple things such as renaming because readability in code is vastly underestimated as a code quality metric.

7: Know when to quit. You can keep on refactoring the same code because code isn’t perfect and can always be improved. Your choice and plans of attack changes over time because you change and mature as a person (hopefully). You can easily refactor the same piece of code over and over again over the years without improving it very much if you don’t look out. Ask yourself if it’s worth the effort. If it’s not ask yourself if you could learn something new by refactoring this piece of code. If not – ask someone if you should go for it. If that someone doesn’t nod his or her head – focus your energy elsewhere.

Until later…

Visual Studio 2010 released

VS2010 has arrived – check out this blogpost from The Hanselman  :o)

tirsdag den 16. marts 2010

Gotcha: .NET 2.0 Mainstream support expires in 2011

I was watching this InfoQ webcast yesterday (about refactoring legacy systems – good one actually, you should go see it) and along the way the support expiredate of .NET 2.0 was mentioned… I was a little surprised to know that the date has been set to 2011 so I doublechecked today and it’s not just for fun: Mainstream support expires in April 2011 with extended support until 2016.

What does it mean to us developers? Well… Nothing much really in the short run but it is a disaster waiting to happen for the enormous amount of mission critical .NET 1.1 and .NET 2.0 code in production around the globe. When Microsoft decides to deprecate a version there will be no service packs for one. That means that if i.e. new security vulnerabilities are discovered along the way Microsoft has every right to vow against a hotfix or a security update – because you knew (or should have known) well in advance that you should have planned for an upgrade to a newer framework.

For the majority of systems around it’s a small change to alter the “Target Framework” setting to a newer version of the framework and get rid of the warnings it generates. Heck: If you’ve stuck with .NET 2.0 and are still doing “foreach” every time you need to iterate over something you better get started anyway or find someone ready to pay you to do so I just need to raise the flag because if I didn’t see it coming lots of other developers haven’t seen it either. Spread the word  :o)

Related link: Microsoft Support Lifecycle Policy FAQ

torsdag den 4. februar 2010

MSTest TestCategory and System.Runtime.Caching - two .NET 4.0 lesser-known features

We've been fiddling with Visual Studio 2010 Beta 2 and .NET 4.0 for the past month at work. Along the way I've been peeking a bit in the released features and I'd thought I'd mention the two I've found here which caught me offguard because I didn't know they existed:

MSTest improvements

it seems that MSTest has adopted the Category attribute in NUnit - in the Microsoft.VisualStudio.TestTools.UnitTesting namespace in .NET 4.0 there is a TestCategory attribute for you to use if you have long-running integrationtests you don't need or want to run on your developer machine. The equivelant NUnit Category-attribute has been around for years so to the guys in Seattle: You have seen the light at last. Thank you. Up until now you were stuck with Test Lists which - well - I've never met anyone liking them and not experiencing friction when using them and my mother always told me never to speak badly in public about anyone I didn't like so the issue here is: TestCategory in MSTest are available in .NET 4.0   :o)

System.Runtime.Caching

There's a new namespace in .NET 4.0 which is System.Runtime.Caching. It basicly encapsulates the good ol' ASP.NET Cache from System.Web so you don't have to include System.Web in non-web assemblies if you want caching out of the box. I've always been irritated by having to either roll my own caching solution or including an assembly called "System.Web" in an assembly consisting of data-access only... It just looked too ugly and incoherent to me.

The namespace contains an abstract ObjectCache with a single implementation called MemoryCache. If you've ever worked with the ASP.NET cache you'll feel fine - Microsoft has for once gone with Principle of Least Surprise during this refactoring. There are CacheItem, CacheDependency, Absolute and Sliding expiration and so forth without the look and feel of an ASP.NET Web-application.... Nice one, Microsoft - I really mean it.

Feel free to put additional lesser-known features in the comments below if you have stumbled upon them and haven't heard or read about them anywhere.

onsdag den 27. januar 2010

.NET 4.0 Framework Client Profile in VS2010 causing “The type or namespace could not be found”

…that has got to be in the top 3 of long headlines on this blog for sure…

Currently at work we’re doing some work on new projects – for the fun of it we’re trying the new Visual Studio 2010 to get experiences with primarily .NET 4.0 and WCF 4.0

Today I encountered a problem which almost by half an inch drove me totally mad. I was for some reason unable to reference one of our .NET 3.5 assemblies. I got the good ol’ "The type or namespace name (insert assembly) could not be found (are you missing a using directive or an assembly reference?)” error thrown at me. Intellisense worked, but the class in question wasn’t caught by the color-coding… It seemed rather odd. I did what most sane people do: I recompiled the .NET 3.5 assembly. I deleted it and recompiled. I did a restart of VS 2010. I did a reboot of my entire machine. I went to a meeting leaving the machine to think about the trouble it was causing me - without any improvement when I returned. Finally I checked the code into our source repository and had someone else check out the flawed source and make a build just to make certain it wasn’t just me who had a problem going… It turned out the problem was consistent across machine boundaries which probably is the only reason I still have a small glitch of sanity left.

After fiddling with various options and project settings my eyes suddenly fell on the Target Framework setting. It was set to “.Net 4.0 Beta Client Profile”… What the hey – I never heard about Client Profiles before…???  I changed the target to “.NET 4.0 Beta” instead and voila – I was able to build succesfully. Lots of hair lost on the way but problem solved.

I’m posting this to spread the word - maybe I’m not able to take full advantage of Google in situations like this (I do feel I’m able to find what I am looking for in other situations though) - in this particular case all I could find was a bunch of newbie-Q&A’s on pages related to “are you missing a using directive or an assembly reference” etc. I really hope that the VS 2010 guys could be able to provide a better errorcontext since I was lead in a complete wrong direction by the user. I had my new SolidState Disc, Visual Studio 2010, Windows 7, Osama Bin Laden, the Man on the Moon and just about everyone in the office under suspicion for doing hanky-panky with my machine because the error made absolutely no sense what so ever.

Read more about Client Profiles here – it seems like you have to explicitly reference assemblies in configuration but I haven’t dug into it so please correct me if I’m way off on that one.