1

I'm in search for some code-improvement. I currently have the following piece of code:

        if (pMyDocAction.s_locatie_st != null)
        {
            String[] myLocaties = Globals.GlobalTools.DeserializeValueToStringArray(pMyDocAction.s_locatie_st);
            if (myLocaties != null)
                if (myLocaties.Length > 0)
                    row.Locatie = myLocaties[0];
                else
                    row.Locatie = String.Empty;
            else
                row.Locatie = String.Empty;
        }
        else
            row.Locatie = String.Empty;

Mylocaties is a Array of String and this cannot change. How can i shorten this piece of code (or how can i combine the != null and .length > 0?

Thnx

1
  • Thnx everyone for the quick answers! If learned lots of new ways to tackle the mentionned problem. +1 to everyone! I will give the credits to selman though as he was to quickest poster and i'm using a variant of his code. Also a great thnx to CodeCaster for pointing out that this code might be better suited in my Globals class! Commented Mar 17, 2014 at 14:58

4 Answers 4

2

You can use conditional operator and write that statement like this:

row.Locatie =  (myLocaties != null && 
                myLocaties.Length > 0) ? myLocaties[0] : String.Empty
Sign up to request clarification or add additional context in comments.

1 Comment

The null-coalescing operator is ??, not ?.
2

I would suggest you to create a small extension method:

public static class ArrayExtension{
    public static bool HasContent<T>(Array<T> array) {
         return array != null && array.Length > 0;
    }
}

Then you can check :

int[] x = null;

x.HasContent(); // false

string[] strs = new string[] {};

strs.HasContent(); // false

string[] strs2 = new string[] {"foo", "bar" };

strs.HasContent(); // true

This can be extended to simplify your syntax:

public static class ArrayExtension{
    public static T FirstValueOrDefault<T>(Array<T> array, T @default) {
         if( array != null && array.Length >0 ){
              return array[0];
         }
         else {
              return @default;
         }
    }
}


int[] x = null;

int y = x.FirstValueOrDefault(42); // 42

string[] strs = new string[] {};

string some = strs.FirstValueOrDefault("default"); // default

string[] strs2 = new string[] {"foo", "bar" };

 string some2 = strs.FirstValueOrDefault("default"); // foo

1 Comment

I like this semantic. I would name it a little different, but nothing comes to mind right away :)
1

Use && operator on two conditions, it will do short-circuit evaluation and if first condition is false, it will not evaluate the second condition.

if (myLocaties != null && myLocaties.Length > 0)
{
    row.Locatie = myLocaties[0];
}
else
{
   row.Locatie = String.Empty;
}

Comments

1

Since all other answers seem to ignore the if (pMyDocAction.s_locatie_st != null), something like this seems to be the most reusable:

row.Locatie = DeserializeLocation(pMyDocAction.s_locatie_st);

string DeserializeLocation(string locationString)
{
    var result = "";

    if (!string.IsNullOrEmpty(locationString))
    {
        String[] deserializedLocations = 
            Globals.GlobalTools.DeserializeValueToStringArray(locationString);

        if (deserializedLocations != null && deserializedLocations.Any())
        {
            result = deserializedLocations[0];
        }
    }   

    return result;
}

You might even consider putting this method in your "GlobalTools" class, so you can call it from anywhere were you need to deserialize a potentially null-bearing serialized location string into a location string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.