#5 – Technology

One of the great things about working in software is the sheer variety and availability of tools to make doing things easier. The only problem, of course, is figuring out what they are and how to use them.

A good example is a C# library, LINQ. Using LINQ, you can query various data structures (lists, arrays, dictionaries, etc), like you would a traditional database. If you need to find an object with a specific property value, select a subset of a list, or perform a merge operation, you can do it in a single, readable statement rather than via clunky loops. Amazingly, some operations, like finding a maximum or sorting a list, are performed more efficiently via LINQ methods than they could be with conventional code.

LINQ actually came up during a pair programming session for my A-Life simulation project. One of my teammates encountered a strange bug in one of her methods. The method was supposed to return the index of an object which matched a given object, and return null if a match was not found. Her implementation was what you’d expect – a double loop with comparison functions – but it was encountering strange out of bounds exceptions. I helped solve her problem by replacing the entire block of code with a single LINQ statement:

organisms.FindIndex(o => o.genome == genome)

Not only did it simplify the code, it fixed the issue entirely. It was also a great teaching moment – my teammate learned about the power of LINQ, and will surely be able to use it in the future.

Print Friendly, PDF & Email

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *