HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Checking if an array is initialized and allocated in VB.NET

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arraycheckingnetandallocatedinitialized

Problem

I am creating an add-in for a piece of software our company uses, so I'm working pretty extensively with that program's API. There are many COM object members which return or contain an array that I need to work with. Sometimes functions will return an uninitialized array (Null or "Nothing" return) or sometimes they may return an initialized array that is not allocated. My goal is to eliminate redundant code in the form of If/Else blocks before using each array, or Try/Catch blocks to handle exceptions thrown by the array being un-initialized or un-allocated.

Note: I'm not trying to validate any data here - that can be done in the calling code or in a function or whatever - I'm simply looking to determine if an array is in a usable state (e.g. initialized and allocated with at least one member)

I've created a member extension for arrays to check if the array is both initialized and allocated.

I'd like to get some input on whether or not this is a well designed solution or if it's total bupkis, if I'm missing anything else I should be checking for, and if improvements can be made in terms of robustness, efficiency, or general best practices; I would like to make it as fast as possible for dealing with large arrays.

Imports System.Runtime.CompilerServices

Module Module1
#Region "Extension Methods"
     
    Public Function IsAllocated(ByVal inArray As System.Array) As Boolean
        Dim FlagEx As Boolean = True
        Try
            If inArray Is Nothing Then
                FlagEx = False
            ElseIf inArray.Length <= 0 Then
                FlagEx = False
            ElseIf inArray(0) Is Nothing Then
                FlagEx = False
            End If
        Catch ex As Exception
            FlagEx = False
        End Try
        Return FlagEx
    End Function

#End Region
End Module

Solution

-
Option Strict

First thing first: Enable Option Strict please read : whats-an-option-strict-and-explicit

This will make your code less error prone. If you have enabled Option Strict you will see a warning on the line ElseIf inArray(0) Is Nothing Then that late binding is not allowed if using Option Strict On.

-
Regions

Please read are-regions-an-antipattern-or-code-smell


Is there a good use for regions?


No. There was a legacy use: generated code. Still, code generation
tools just have to use partial classes instead. If C# has regions
support, it's mostly because this legacy use, and because now that too
many people used regions in their code, it would be impossible to
remove them without breaking existent codebases.


Think about it as about goto. The fact that the language or the IDE
supports a feature doesn't mean that it should be used daily. StyleCop
SA1124 rule is clear: you should not use regions. Never.

-
Hungarian notation

The parameter name inArray is a sign, that you are misusing the original intention. Please read this: what-is-the-benefit-of-not-using-hungarian-notation

And don't forget to read the reffered links.

That beeing said let us check how we could enhance this method by grouping the conditions together.

By using OrElse or AndAlsowe can short circuit conditions, which means that if a conditionA is true the conditionB won't be evaluated. like

If conditionA OrElse conditionB Then  

End If


If we invert the conditions we can simplify the whole method to

Module Module2

    
    Public Function IsAllocated(ByVal inArray As System.Array) As Boolean

        Return Not ((inArray Is Nothing) OrElse (inArray.Length = 0) OrElse (inArray.GetValue(0) Is Nothing))

    End Function

End Module

Code Snippets

If conditionA OrElse conditionB Then  

End If
Module Module2

    <Extension()>
    Public Function IsAllocated(ByVal inArray As System.Array) As Boolean

        Return Not ((inArray Is Nothing) OrElse (inArray.Length = 0) OrElse (inArray.GetValue(0) Is Nothing))

    End Function

End Module

Context

StackExchange Code Review Q#95234, answer score: 5

Revisions (0)

No revisions yet.