Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the parameter order when doing the null checks #7

Open
tugberkugurlu opened this issue Nov 18, 2014 · 8 comments
Open

Preserve the parameter order when doing the null checks #7

tugberkugurlu opened this issue Nov 18, 2014 · 8 comments

Comments

@tugberkugurlu
Copy link
Contributor

Current implementation doesn't preserve the parameter order for the added if statements:

null-check-param-order-repro

@AdamSpeight2008
Copy link
Contributor

Potential Bug: What if the implement of the Interface is a structure?

@tugberkugurlu
Copy link
Contributor Author

It is already covered with this, isn't it?

@tugberkugurlu
Copy link
Contributor Author

@AdamSpeight2008 if you are talking about the below situation, it's safe AFAIK.

var bar = new FooBar(new Foo());

public class FooBar
{
    public FooBar(IFoo foo)
    {
        if (foo == null)
        {
            throw new ArgumentNullException(nameof(foo));
        }
    }
}

public interface IFoo
{
}

public struct Foo : IFoo
{
}

@AdamSpeight2008
Copy link
Contributor

I'm gonna have a look at this, as I think I have Idea how to implements. (I'll Branch the build)

@tugberkugurlu
Copy link
Contributor Author

This is also applicable for VB implementation. @AdamSpeight2008 can you take a look at VB implementation for this, too?

@AdamSpeight2008
Copy link
Contributor

This is the basic idea was thinking of implementing.

Dim Guards As New List( )( Parameters.Count )

For Each ExistingGuard In ExistingGuards
  Dim Index = FindParameterIndexOfGuard( ExistingGuard ) 
  If Index.HasValue Then Guards( Index.Value ) = ExistingGuard
Next 
' Remove Existing Guards

' Add new Parameter Guard
  Dim NewGuard = CreateSingleLineIfGuard( parameter )
  Dim Index = FindParameterIndexOfGuard( NewGuard )
  If Index.HasValue Then Guards( Index.Value )

For Each Guard In Guards
  ' Insert Guard in method
Next

I'll try and get a working prototype later one.

@sharwell
Copy link
Member

I lean towards this instead:

addNullCheck(x):
    insertionIndex = 0
    for i = 0...(x - 1)
        if isGuardForParameter(i, statements[insertionIndex])
            insertionIndex++
    statements.insert(insertionIndex, createGuard(x))

@AdamSpeight2008
Copy link
Contributor

Implemented for VB.net

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants