Results 1 to 8 of 8

Thread: Mixing class and module - your thoughts?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Lightbulb Mixing class and module - your thoughts?

    Hi

    In VB.Net you are technically allowed to have a class and a module with the same name (as a module is just the VB.Net equivalent to a C# static class) but as a developer would you be happy with that?
    I am using it to allow the static creation of generic classes e.g.:-

    Code:
        Public NotInheritable Class FileProjectionSnapshotReader(Of TAggregate As CQRSAzure.EventSourcing.IAggregationIdentifier,
                                                                     TAggregationKey,
                                                                     TProjection As IProjection)
            Inherits FileProjectionSnapshotBase(Of TAggregate, TAggregationKey, TProjection)
            Implements IProjectionSnapshotReader(Of TAggregate, TAggregationKey, TProjection)
    
            Public Function GetSnapshot(key As TAggregationKey, Optional OnOrBeforeSequence As UInteger = 0) As IProjectionSnapshot(Of TAggregate, TAggregationKey) Implements IProjectionSnapshotReader(Of TAggregate, TAggregationKey, TProjection).GetSnapshot
                Throw New NotImplementedException()
            End Function
    
            Public Function GetLatestSnapshotSequence(key As TAggregationKey, Optional OnOrBeforeSequence As UInteger = 0) As UInteger Implements IProjectionSnapshotReader(Of TAggregate, TAggregationKey, TProjection).GetLatestSnapshotSequence
                Throw New NotImplementedException()
            End Function
    
            Private Sub New(ByVal AggregateDomainName As String,
            ByVal AggregateKey As TAggregationKey,
            Optional ByVal settings As IFileStreamSettings = Nothing)
    
                MyBase.New(AggregateDomainName,
                           AggregateKey,
                           writeAccess:=False,
                           connectionStringName:=GetReadConnectionStringName("", settings),
                           settings:=settings)
    
            End Sub
    
    #Region "Factory methods"
    
            ''' <summary>
            ''' Creates an azure file storage based event stream reader for the given aggregate
            ''' </summary>
            ''' <param name="instance">
            ''' The instance of the aggregate for which we want to read the event stream
            ''' </param>
            ''' <param name="projection">
            ''' </param>
            Public Shared Function Create(ByVal instance As CQRSAzure.EventSourcing.IAggregationIdentifier(Of TAggregationKey),
                                          ByVal projection As TProjection,
                                          Optional ByVal settings As IFileStreamSettings = Nothing) As IProjectionSnapshotReader(Of TAggregate, TAggregationKey, TProjection)
    
                Dim domainName As String = DomainNameAttribute.GetDomainName(instance)
                If settings IsNot Nothing Then
                    If Not String.IsNullOrWhiteSpace(settings.DomainName) Then
                        domainName = settings.DomainName
                    End If
                End If
                Return New FileProjectionSnapshotReader(Of TAggregate, TAggregationKey, TProjection)(domainName, instance.GetKey(), settings)
    
            End Function
    
    
    #End Region
    
        End Class
    With the module thus:-

    Code:
        Public Module FileProjectionSnapshotReader
    
            ''' <summary>
            ''' Creates an azure file storage based event stream reader for the given aggregate
            ''' </summary>
            ''' <param name="instance">
            ''' The instance of the aggregate for which we want to read the event stream
            ''' </param>
            ''' <param name="projection">
            ''' </param>
            Public Function Create(Of TAggregate As CQRSAzure.EventSourcing.IAggregationIdentifier,
                                                                     TAggregationKey,
                                                                     TProjection As IProjection)(ByVal instance As TAggregate,
                                                                                                 ByVal key As TAggregationKey,
                                          ByVal projection As TProjection,
                                          Optional ByVal settings As IFileStreamSettings = Nothing) As IProjectionSnapshotReader(Of TAggregate, TAggregationKey, TProjection)
    
                Dim domainName As String = DomainNameAttribute.GetDomainName(instance)
                If settings IsNot Nothing Then
                    If Not String.IsNullOrWhiteSpace(settings.DomainName) Then
                        domainName = settings.DomainName
                    End If
                End If
                Return FileProjectionSnapshotReader(Of TAggregate, TAggregationKey, TProjection).Create(instance, projection, settings)
    
            End Function
    
        End Module
    Would this be OK in you ropinion, or should I rename the module FileProjectionSnapshotReaderFactory?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,897

    Re: Mixing class and module - your thoughts?

    No, I would not be happy with that.

    Any time you give two different things the same name you are inviting confusion. I don't even like having names that are particularly close to each other if they are going to be used in the same code. For example, if I have a single variable called Bird, then I wouldn't create a List of them called Birds. I want all my elements to be unequivocally distinct, even if it is technically possible to do otherwise.
    My usual boring signature: Nothing

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,578

    Re: Mixing class and module - your thoughts?

    Personally I wouldn't use the module in the first place... secondly... the two create methods are the same... so I'm not sure what you're gaining here.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,578

    Re: Mixing class and module - your thoughts?

    Quote Originally Posted by Shaggy Hiker View Post
    ...example, if I have a single variable called Bird, then I wouldn't create a List of them called Birds.
    Just out of curiosity, what would you call it? Due to force of habit I'd end up calling it BirdList ...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,897

    Re: Mixing class and module - your thoughts?

    I think I'd use exactly that, except that it would be birdList rather than BirdList. I only capitalize the first letter for class names and method names.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Mixing class and module - your thoughts?

    Quote Originally Posted by techgnome View Post
    the two create methods are the same... so I'm not sure what you're gaining here.
    Dynamic type inference - the module version dynamically infers the type for creating an instance of the class (which is type-safe thereafter)

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Mixing class and module - your thoughts?

    I wouldn't do this, and I'd say I think it's gross that VB allows it, but I think if we peeked behind the curtains we'd find it's not a feature of VB but an artifact of how Modules are implemented. Either way, I don't want two types with the same name in my project outside of some very oddball scenarios.

    In terms of naming, I use English grammar rules I've already had to learn to guide me.

    First, if I had a thing called "Bird", it would have to be a class or a property of a class, because capital letters Mean Something. If I had a variable named "bird", I'd expect just that: a variable, because capital letters Mean Something, and if they aren't there I know I have a local variable of some sort.

    It's a singular noun. That makes me expect it to be a singular object, not an array or other collection. Because it's a noun, I expect the variable to refer to an object that is likely of type 'Bird'. What else would 'bird' be? I can create contexts where it makes sense as an Integer, but when I write code that requires me to remember context I tend to make more mistakes, so I'd name an Integer 'birdIndex' or 'birdCount' or something else that has a sense of 'number' to it.

    Is it confusing that "bird" is a "Bird"? No. Because Capital Letters Mean Something. I don't care that VB is not case sensitive. I'm NoT WrItiNg CoDe like ThIs, so why follow rules that assume I will? So long as EVERY time I make a class I give it a PascalCase name I don't get mixed up. VB allows this syntax and I don't think it's confusing to anyone with a few months of VB under their belt:
    Code:
    For Each bird As Bird in birds
    So what about "Birds" or "birds"?

    Well, in this case none of those are likely to be a class name. The only time I give a class a plural name is if it is a collection type. It's really rare that I make my own collection type like that. When I do, it's usually because I'm adding some special behavior to the list, so I'd rather pick a name like "SortedBirds" or "BirdTree".

    So if it's PascalCase I expect it to be a property. If it's lowercase, a local variable. It's plural, so it must be a collection of some type. I like to name lists/arrays like this, and use words like 'lookup' or 'queue' when using non-list structures. So whether it's "Birds" or "birds", I expect a collection of Bird objects.

    But all of that only works because I do it every time. I could, instead, name things like ShaggyHiker and insist that "a variable with a Bird in it" be named "theBird" or "currentBird", etc. If I did that every time, it'd be just as illustrative and it's not really much more clutter/work to do things that way.

    It's when we break our rules that things get confusing. So figure out how you want to name variables in this case, and do it that way every time!
    Last edited by Sitten Spynne; Jul 15th, 2016 at 12:44 PM.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Mixing class and module - your thoughts?

    I have decided not to use the same name - although I would so do in C#.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width