dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
288 stars 66 forks source link

[Proposal] Less verbose null checks #578

Open VBAndCs opened 4 years ago

VBAndCs commented 4 years ago

Can't we writ this statement:

If pop IsNot Nothing Then pop.IsOpen = False

just as:

pop?.IsOpen = False

Or at least:

  If pop Then pop.IsOpen = False

I am using option strict of, so, again, why can't an object converted to a Boolean in a condition context?

Or at least:

  If pop? Then pop.IsOpen = False

and of course, I prefer pop?.IsOpen = False the most.

zspitz commented 4 years ago
If If(pgp?.IsOpen, False) Then

You can't elide the If ... Then; it's extremely unclear what the code is trying to do without it.

VBAndCs commented 4 years ago

If If !! This can't be readable!.. My first goal is the beauty and eloquence of the language. On the other hand, I see pop?.IsOpen = False as a very clear syntax. A question mark asks for something (agree?), and we know from the other uses of ? that this question is: Has pop a value? So, it translates directly to: If pop IsNot Nothing Then Even with the current usage of ?, the expression evaluates either to pop.IsOpen = False or to Nothing = false, which we can ask the compiler to ignore, as it has no meaning (do nothing).

VBAndCs commented 4 years ago

Also, the expression If If(pop?.IsOpen, False) Then will not assign False to pop.IsOpen.

Echo-8-ERA commented 4 years ago

Or you could just define the IsTrue and IsFalse operators in your class:

Option Strict On

Imports System

Public MustInherit Class MyBaseClass

    Public Shared Operator IsTrue(ByVal obj As MyBaseClass) As Boolean
        Return obj IsNot Nothing
    End Operator

    Public Shared Operator IsFalse(ByVal obj As MyBaseClass) As Boolean
        Return obj is Nothing
    End Operator

End Class

Public Class MyChildClass
    Inherits MyBaseClass

    Public Sub New()
    End Sub

End Class

Public Class Program

    Public Shared Sub Main(ByVal ParamArray Args As String())
        Dim Foo, Bar As MyChildClass

        Foo = New MyChildClass()
        Bar = Nothing

        If Foo Then
            Console.WriteLine("Foo is Not Nothing!")
        Else
            Console.WriteLine("Foo is Nothing!")
        End If

        If Bar Then
            Console.WriteLine("Bar is Not Nothing!")
        Else
            Console.WriteLine("Bar is Nothing!")
        End If

    End Sub

End Class
VBAndCs commented 4 years ago

@Echo-8-ERA This will make us write billions of lines of code in millions classes to do what the compiler can do for us, but this still will not solve the issue with the framework classes that we can't modify. My basic role is: The compiler exists to serve us, not the opposite. Thanks.

Happypig375 commented 4 years ago

The compiler must be edited and maintained by people. You cannot expect one team to serve the world.

gilfusion commented 4 years ago

While I occasionally like the idea of a pop?.IsOpen = False shorthand (I have VS installed on a Surface Go, so I like it when my code actually fits on that smaller screen), I almost always come around to thinking that I do not really need it and that If pop IsNot Nothing Then pop.IsOpen = False is more readable anyway.

As for implicitly converting null references to False, it feels too "JavaScript" to me, making the code a little less readable, and just saving the words IsNot Nothing (which takes moments to type, especially with autocomplete), again, does not feel like it's worth the change.

I would rather look for ways to prevent null references from getting into my code in the first place (looking jealously at C#'s new nullable reference types and that proposed shorthand for throwing an exception when an argument is null) than try to deal with those null references every time I access an object.

VBAndCs commented 4 years ago

I would rather look for ways to prevent null references from getting into my code in the first place

You can't, and you shouldn't. We even make value types nullable, as null provides a very useful state, a flag that we need most of the time. In fact I hate C# nullable references and never used them, will never do.

More important: readability is not only about English words, but also about the bulk of the code you see. All human languages care most for eloquence of the sentence, not just being in the correct syntax or just give a meaning. I can write 100 words to give the same meaning of a 3 words. Besides, we passed the stage of arguing about the using of "?" in VB. They took that decision years ago, and we all know what it means. I agree it is a bit symbolic, but it is also a famous universal punctuation used by all human bearings, and sometimes takes place of words like how, why, when, what, Is....etc. So, we used it in VB as IsNot Nothing ?. I didn't invent that, and the language must generalize this conscept every where possible to be consistence. So, VB is less readable until allow it. It is less natural than any human language until it gives you what you expect to get. This is what readability means to me.

VBAndCs commented 4 years ago

@Happypig375

The compiler must be edited and maintained by people. You cannot expect one team to serve the world.

They are paid for that. And a compiler will not be used by all the globe, unless it services them well. In fact you show a practical experiment about what happened when C# tllo all the attention and VB got neglected over years! Companies asked for C# developers, and most of VB developers had to switch to it. You can't depend on a compiler that doesn't fulfil your needs as soon as you need them. So, the cycle of killing a popular language is as this easy: Ignore developers, flush them out, and shut down.

jrmoreno1 commented 4 years ago

Note that the C# equivalent proposal, https://github.com/dotnet/csharplang/issues/2883, seems to have a bit of traction, but given the “no new development” strategy, I don’t have much hope for this in VB. If C# doesn’t get it, then, “it’s not needed”, and if C# does get it, then, “it’s an advanced feature, use c#”.

VBAndCs commented 4 years ago

@jrmoreno1
Thanks for this info. We are past hope to get anything new in VB.NET, so, I am sharing these ideas to be a part of any new implementation of VB.NET. I hope someday I can fork VB and implement them myself.

tverweij commented 4 years ago

"I am sharing these ideas to be a part of any new implementation of VB.NET" Wrong place for that.

"I hope someday I can fork VB and implement them myself." For that, this is the right place.

VBAndCs commented 4 years ago

@tverweij Thanks for the embedded invitation, but this is the mother land of the language, and all new implementations should talk it as a reference, at least, because the huge number of users. I also advice you to track all the issues in the user voice site, as most users send their reports there via VS.NET. Hope you all the best.

tverweij commented 4 years ago

My last message here, to be complete. This site and VS.Net are the sites for MICROSOFT Visual Basic - that won't evolve anymore.

Our implementation will evolve, but we don't use these sites anymore - we looked at the existing issues when we started, took the best out of that and now we are evolving the language our own way, separate from Microsoft, even evolving away from .Net to native AOT on each platform (while .Net (Core) stays 100% supported)

VBAndCs commented 4 years ago

@tverweij I am aware of that, and it amazing. It will hopefully track VB developers and companies. Long live VB :)

vbcodec commented 4 years ago

vb is verbose language. Don't make it as another cryptic C#