Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return empty array instead of null

Tags:

c#

I created Settings class which I use to edit my application by .ini file. My Settings.ini file looks like this:

[ACCOUNT]
login=xyz
password=xyz
locations=1,2,5,8

Now I am getting theese values like this:

class Settings {
    public static IniFile Config = new IniFile(Directory.GetCurrentDirectory() + @"\Settings.ini");
    public static string Login { get { return Config.Read("login", "ACCOUNT"); } set { Config.Write("login", "ACCOUNT"); } }
    public static string Password { get { return Config.Read("password", "ACCOUNT"); } set { Config.Write("password", "ACCOUNT"); } }
    public static int[] Locations { get { return Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s)); } set { Config.Write("locations", "ACCOUNT"); } }
}

The problem is, when my Settings.ini file has empty locations:

locations=

My variable Settings.Locations return null instead of empty array. I've tried doing something like this:

public static int[] Locations 
{ 
    get { return new int[] {Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s))}; } 
    set { Config.Write("locations", "ACCOUNT"); } 
}

But that is just not working. I cant convert int[] to int. Do you have any ideas how can I return empty array?

like image 287
Brak Danych Avatar asked May 04 '17 11:05

Brak Danych


People also ask

How do you return an empty array?

Return Empty Array - With new int[0] or new Emp[0] When you want to return an array with a size of 0 then you just need to return new int[0]. Here, new int[0] returns an integer type array with zero values and size is 0.

Should I return null or empty array?

It is best practice to return empty values rather than null ones. Especially when you return a collection, enumerable, or an object, you should avoid returning null. Returning null is okay if your code handles the returning null value. But developers may forget to write a null check.

Is null and empty array the same in Java?

null array—-when the size of array is not declared than the array is known as null array. EMPTY ARRAY——-if an array having the size but not values than it's known as empty array.

How do you pass an empty array in Java?

Just make: this. warenkorb = new PizzaV0[10]; This will initialize an empty array with 10 nulls.

What happens if you return a null in an array?

Return an empty array or collection instead of a null value for methods that return an array or collection Some APIs intentionally return a null reference to indicate that instances are unavailable. This practice can lead to denial-of-service vulnerabilities when the client code fails to explicitly handle the null return value case.

Why do we have to return an empty array?

We sometimes have to return an empty array for a few reasons, like when the array is coming from an API, and it returns null; in this case, we might want to return an array without any element, instead of null. Every array has a fixed size that we can specify when we create the array.

What are some alternatives to return a null value in JavaScript?

For methods that return a set of values using an array or collection, returning an empty array or collection is an excellent alternative to returning a null value, as most callers are better equipped to handle an empty set than a null value.

What is prefer empty collection to null in Java?

Prefer empty collection to null is a basic java principle that stems from Effective java Item 43: return empty arrays or collections, not nulls. There are a few reasons to support it:


2 Answers

You can do it explicitly like this:

public static int[] Locations
{
    get
    {
        string locations = Config.Read("locations", "ACCOUNT");
        if (locations == null)
        {
            return new int[0];
        }
        return locations
                .Split(',')         // split the locations separated by a comma
                .Select(int.Parse)  // transform each string into the corresponding integer
                .ToArray();         // put the resulting sequence (IEnumerable<int>) into an array of integers
    }
    set
    {
        Config.Write("locations", "ACCOUNT");
    }
}
like image 142
Tao Gómez Gil Avatar answered Oct 19 '22 05:10

Tao Gómez Gil


First of all, you're jamming too much into one line so it makes it really hard to read let alone troubleshoot. What you need is something like this:

public static int[] Locations 
{ 
    get 
    { 
        int[] values = Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), 
            s => int.Parse(s)) ?? new int[] { };
        return values; 
    } 
    set 
    { 
        Config.Write("locations", "ACCOUNT"); 
    } 
}

Notice I have added ?? new int[] { } to the end of the first statement, which is called the null coalescing operator. It will return an empty array if the other array is null.

It's a matter of preference, but the reason I separated the getter into two lines is so I can debug and break before it returns to observe the return value. You could also break on the last bracket instead and observe the return value in the Locals window.

like image 44
rory.ap Avatar answered Oct 19 '22 03:10

rory.ap