Nov
19
2010

Using Resharper Search with Pattern to protect you from your code

Today I faced a nasty bug, a null pointer exception in a property databound to our WPF application. This had some nasty side effects, and for sure this part of the code didn’t had any unit test, too bad! For sure now it has.

The line of code was really simple

Values.Where(model => model.IsSelected).FirstOrDefault().Refresh();

I guess you see the issue! If not here is the MSDN documentation:

public static TSource FirstOrDefault< TSource >(this [NotNull] IEnumerable<TSource> source)
in class Enumerable

Summary:

Returns the first element of a sequence, or a default value if the sequence contains no elements.

Parameters:

source:
The IEnumerable<out T> to return the first element of.

Type Parameters:

TSource:
The type of the elements of source.

Returns:

default(TSource) if source is empty; otherwise, the first element in source.

Exceptions:

ArgumentNullException:
source is null.

Yeah, FirstOrDefault might returns null, so if you chain a call to another method it just crash!

For sure we know that! We use this method for that purpose, but an error can happen that fast!

So I decided that I wanted to be protected by my tooling. So I went back to read the post “Introducing ReSharper 5.0: Structural Search and Replace

After several trial and some help from Ilya (Thanks!) I finally found the correct way to express what I wanted. My goal was to find all code which uses FirstOrDefault() method followed by a call to another method. Exactly like my issue.

$enumerable$.FirstOrDefault().$method$()

enumerable is an Expression of type System.Collections.IEnumerable or derived type

method is a identifier placeholder with an empty indentifier name regexp

I then verified that this error was found. And also luckily that this was the only error of that kind in our application.

Then I finally added it to the Resharper Pattern Catalog, to show a warning.

Now when ever I will type this stupid thing, I will have my preferred tool Resharper, telling me how stupid I am to even try this!

BY the way you might download a sample Pattern Catalog from this blog post “Sample SSR Pattern Catalog Available for Download”. There are some cool stuff in there and it might help writing your owns.

Jun
16
2010

Power of IntelliJ in video

Today I was listening and some time watching (it helps to have two monitors :) the following video that I got from Jetbrains blog post, Vaclav Pech Talking at JAX 2010

I learned several tips and I think this video shows why I like more and more IntelliJ.

If you missed Vaclav Pech speech at JAX 2010, here’s your chance to hear it right now. In this presentation he’s discussing common coding pitfalls such as possible NPE, unreachable code, and explains how they can be easily avoided.

Vaclav Pech at JAX 2010 from Egor Malyshev on Vimeo.

May
7
2010

IntelliJ 9 and changeset

Day after day I find little gems in IntelliJ 9 that just make me more productive and give me more time to deal with the real interesting things.

Today for example I had to change a web.xml file which I was said that I should take care not to commit because if this file would go to our customer than we would have a problem.

We are currently using JIRA and Greenhopper and I use the excellent plugin Atlassian Connector for IntelliJ IDEA

My working process, which is not rocket science but needs a bit of discipline, is the following:

  1. Before starting a change I check that there is a task in our JIRA
  2. Then I get this task and create a changeset in IntelliJ
  3. Starting from now, everything I change is logged into that changeset
  4. If I need to work shortly on another task I get another task from JIRA and create another changeset and start to log my work on that new changeset
  5. When it is time to commit I just commit the changeset. Done

Back to my web.xml issue, I had to take care not to commit it. I knew it from the start that I will have an issue if I commit that file.

So immediately, having the information,  I created a new changeset named “Do not commit” and added the web.xml change to that changeset.

4583794252_6d251ef393_o[1]

I was then on the safe side! Why? Because after working almost the whole morning and changing hundred of files I didn’t had to remember about that possible issue because the tool will remind me that. What a mind refresher!

If I had not done that then I would have to first remind that I don’t have to commit that file and then I would have to browse the hundreds of file searching for the one I have to commit and the other one.

So help yourself work on your toolset and become a more productive developer!

May
6
2010

IntelliJ 9 CSS editor

Today at Innoveo Solutions  I work on CSS and XHTML for our Skye® product.

I am using JetBrains  IntelliJ 9 since a couple of weeks now and I enjoy it very much.

I am always searching for tools which make me more productive in my daily work. IntelliJ 9 is such a tool. And today I have used a feature that helped me a lot.

I started with a CSS I received with that definition

4581559488_dbe2f66d3d_o[1]

If you take care you will see the underline under margin and background, when you place the cursor over you get the following info

4580933991_875d77222b_o[1]

Then you can press ALT-Enter to get an action that you can execute; “optimize margin properties”

4580947043_53b7dd6c0d_o[1]

So when you execute the action for margin and background you get the following optimized result

4581578768_3002940edb_o[1]

You might read more about the CSS capabilities of JetBrains IntelliJ 9 on the following blog.

About Laurent

Laurent Kempé

Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

He is currently employed by Innoveo Solutions since 10/2007 as a Senior Solution Architect and certified Scrum Master.

Founder, owner and Managing Partner of Jobping, which provides a unique and efficient platform for connecting Microsoft skilled job seekers with employers using Microsoft technologies.

Laurent is awarded by Microsoft since Avril 2002: Most Valuable Professional (MVP).

MVP
Certified ScrumMaster
JetBrains Academy Member

My status

Twitter

Flickr

www.flickr.com
This is a Flickr badge showing public photos and videos from Laurent Kempé. Make your own badge here.

Month List

Page List