Freigeben über


Reusability is the reason to have Queries.

Programmers do not like writing the same code over and over again. There are many reasons for this. First of all it is not productive. Secondly it is often hard to maintain as similar code pieces often require same changes. Copying code within a big project is plain evil. This is the primary way of creating those annoying bugs that just do not want to get away – you fix them here, then there, then you find them somewhere again…

 

There are many ways by which languages can help with code reuse already – procedural programming, Object Oriented Programming etc… However there are still too many cases where you have to write code like this:

        Dim numbers As New List(Of Integer)

        numbers.Add(42)

        Dim squares As New List(Of Integer)

        For Each current_number As String In numbers

            squares.Add(current_number* current_number)

        Next

 

As you can see this is a very simple pattern where you iterate over a collection and perform some action with each element. The only “smart” part of this code is the expression inside the loop; the rest is just the way to get elements one-by-one and to apply the expression. You may already intuitively feel that it would be great if you could write the iterating part once and then just supply the “smart” part of the loop while reusing “iterating” part

    squares = IterateAndApply(numbers, _

                 "squares.Add(current_number * current_number)")

This code looks way simpler!!!
The hypothetical function IterateAndApply does the iterating, applies the expression to the elements, collects results and returns the resulting collection.

 

Obviously somebody has to implement IterateAndApply, but it can be implemented only once and called everywhere.

 

A possible implementation of IterateAndApply could look like this:

' source - the collection that is being iterated

' code - user supplied code to apply to each element

' returns a collection of results when user's code is applied

' to all elements.

  Function IterateAndApply(ByVal source As List(Of Integer), _

                              ByVal code As String) As List(Of Integer)

        Dim result As New List(Of Integer)

        'iterate the source

        For Each item In source

            'apply the code that user has provided to each element

            result.Add(Apply(item, code))

        Next

        Return result

    End Function

' hypothetical function that applies given code to the given object

' and returns the result

    Function ApplyCode(ByVal item As Integer, _

                    ByVal code As String) As Integer

'todo: code gets “magically” applied to the item and result is returned

    End Function

You may notice that the resulting code is somewhat bigger than For Each loop. However since this code is going to stay in one instance the overall number of lines in the project could be drastically reduced if you use this function a lot. And as we know writing less code is good for so many reasons.

Comments