Check data before using as array element

0

In chat was asked something about data entry validation to avoid catch error out of range allowed. I found the question interesting and pity that was not posted here, I am posting:

public static T[] ReplaceRange<T>(this T[]input, T[] replacement, int start) {
    for (int i = start; i < input.Length; i++) {
        T replacementItem = replacement[i - start];
        input[i] = replacementItem;
    }
    return input;
}

How to avoid the error?

    
asked by anonymous 13.07.2018 / 17:29

1 answer

4

Points of interest:

  • Contracts
  • Programming errors are solved by fixing them
  • Some reorganizations
  • There is something ready that works best

There are a few ways, I've enjoyed using contracts . It has some drawbacks, but overall it helps. If you do not like it, just switch to if and throw an exception. Does something change? Not much, just that the error is more specific.

Another possibility is to use a Assert() that can be guaranteed to be off, which can be a good one, after all the Out of Bounds error is a programming error and the only possible solution is to fix the error, the only thing we can do is ensure that a well-tested code gives the error at development time and if you have any errors in the call have the problem solved. If you are sure that the error does not happen the Assert() does not go to production, Requires() can go, but at the same time it can be more secure.

In the current version of Visual Studio it is not automatic, you need to install and configure the use of the contracts.

Parameter names are not good, they are misleading.

Another possibility is to do nothing, after all programming error if it solves and does not try to get around, and I have opted for it. But you can do something like this:

using static System.Console;
using static System.Diagnostics.Contracts.Contract;

public class Program {
    public static void Main() {
        foreach (var item in ReplaceRange(new int[] {0, 1, 2, 3, 4}, new int[] {5, 6, 7, 8, 9}, 2)) WriteLine(item);
    }
    public static T[] ReplaceRange<T>(T[] source, T[] destination, int start) {
        Requires(start >= 0, "O índice de início não pode ser menor que zero");
        Requires(start < source.Length, "O índice de início não pode ser maior que o fim do array");
        Requires(destination.Length >= source.Length - start, "O índice de início não pode ser maior que o que cabe");
        for (int i = start, j = 0; i < source.Length; i++, j++) destination[i] = source[j];
        return destination;
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

I'm not going to put online because these environments are limited, the great advantage is when the tool helps, Visual Studio for example can report problems during development, or have tools that generate tests for you based on the contract found within the method (I need to upgrade myself because it has changed since the last time I used it).

But you can simplify this and use Array.Copy() which already exists in .NET. If you just want to make a simple wrap to consider only the beginning of the font and it will fill in the beginning of the destination and the size of the copy alone.

I've put more to show that there are alternatives, some that few know, have ways to solve without doing anything in the code and there are ways to use what already has ready that will be much faster because it will make a copy blittable , as far as it goes.

    
13.07.2018 / 17:29