Get the value of and with using the null-coalescing operator

Posted by Jonas Elfström Fri, 15 May 2009 22:38:00 GMT

I like the ?? operator that surfaced in C# 2.0. I'm often in environments where null values florish and ?? is a great way to handle them, especially for presentation. A while ago I found a use for the ?? operator in a way I'd never used it before.

The problem at hand was to return an account number from data that could be described as somewhat inconsistent.

The application had left room for the users to enter accounts in two by two ways. A customer could have multiple subsidiaries and in each subsidiary, by misconception, two different fields had been used as the account number. It was also possible to connect an account to all subsidairies of a customer at the same time as a specific subsidiary of that customer had an account defined.

Only if a unique account could be found it should be returned. All other cases should return a cause of failure so that the users could use that information to clean up the mess.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private static Account GetCustomerAccount(Customer customer)
{
  bool hasAccount = !String.IsNullOrEmpty(customer.AccountNo);
  bool hasAccountNoExtra = !String.IsNullOrEmpty(customer.AccountNoExtra);
  bool hasAllSubsAccount = !String.IsNullOrEmpty(customer.AccountNoAllSubs);
  bool hasAllSubsAccountNoExtra = 
       !String.IsNullOrEmpty(customer.AccountNoExtraAllSubs);
  var account = new Account();

  if ((hasAccount || hasAccountNoExtra) &&
      (hasAllSubsAccount || hasAllSubsAccountNoExtra)) 
  {
    account.Status = account.HasBothAllSubsAndSpecific;
    return account;
  }
  if ((hasAccount && hasAccountNoExtra) ||   
      (hasAllSubsAccount && hasAllSubsAccountNoExtra)) 
  {
    account.Status = account.HasBothAccountAndAccountExtra;
    return account;
  }

  account.AccountNo = customer.AccountNo ??
                      customer.AccountNoExtra ??
                      customer.AccountNoAllSubs ??
                      customer.AccountNoExtraAllSubs;

  if (account.AccountNo==null)
    account.Status=account.HasNoAccountDefined;

  return account;
}


Notice how AccountNo is set to the first non null value of the four. Imagine that with plain ifs. Here I believe the ??-operator both makes the code clear and saves us from a bunch of nested ifs.

Posted in  | no comments

Breaking good

Posted by Jonas Elfström Sun, 03 May 2009 21:24:00 GMT

Check out Chris Eng's post on how he broke the code on the cover of the 2009 Verizon Data Breach Investigations Report. He makes it seem so simple and without making a big deal of it he also shows the tools and commands he used.

Posted in  | no comments

Predicate matches me

Posted by Jonas Elfström Thu, 29 Jan 2009 20:29:00 GMT

I really like the Predicate(T) delegates that were added to the generic collections and lists in .NET 2.0. With the later addition of lambda expressions came cleanliness and readability.

Today we faced a quite simple problem that were made even simpler by the dear predicates. We had a kind of event log and wanted to filter it client side (Windows Forms) using a list of criterias. We began by implementing to filter by a number of categories. It ended up being only one row (in Visual Studio, for obvious reasons not here):

1
2
3
4
5
6
private List<Events> FilterEventsByCategory(List<Events> events,
                                        List<Category> categories) 
{
  return events.FindAll(event => 
      categories.Exists(category => category.CategoryId==event.CategoryId)); 
}


Neat!

Posted in  | 1 comment

Swedish hackers

Posted by Jonas Elfström Tue, 30 Dec 2008 23:29:00 GMT

The incident I wrote of in Hello this is special agent Brian were covered in a Swedish radio documentary a little more than a month ago. It's all in Swedish. You can find out more here and you can download the documentary from here.

I also would like to add that we did not press charges like the police officers says 37 minutes into the documentary. Not because we thought it wasn't a big deal but we knew that a couple of big players already had so we thought we could spend our time better.

Posted in  | no comments

Death from lack of content

Posted by Jonas Elfström Sat, 15 Nov 2008 01:21:00 GMT

I'm sorry to admit that this blog has died from lack of content and I have absolutely no guarantees to give you that it will ever come alive again.

At least I'm still alive and last night I had some fun with C, Code::Blocks and SDL.

Posted in  | no comments

Older posts: 1 2 3 4 5 ... 8