views:

31

answers:

2

I just bumped into some kind of code and got quite a bit of shock:

Code snippet 1:

Class ABC
    Private Event Event123()

    Private Function ParentMethod()
        RaiseEvent Event123()
    End function

    Private Function ChildMethod() Handles Event123
        ... code here
    End function
End class

(Note the functions and event are private, the code is used within the class only.) (Also, note that the code is in VB, but this pattern can be applied to other languages as well.)

Normally I would just directly call the ChildMethod from the ParentMethod, like below:

Code snippet 2:

Private Function ParentMethod()
    ChildMethod()
End function

I'm wondering, is the code snippet 1 some kind of best/recommended practice? Why?

+1  A: 

From my point of view it's just weird, nothing more, nothing less. I can't see any advantage but making the code look more complex in case all those members are private. If they were public (the event declaration at least) one could speculate about the intent to create an extensibility or notification point in the ParentMethod, which is then automatically subscribed by ChildMethod for some reason.

Ondrej Tucny
Yes, I agree with you. That's why I "got quite a bit of shock" when I saw the code. :)
Gan
+1  A: 

I suppose this code is a consequence of some non-implemented functionality. I guess developer first planned to provide public event in that class to handle parent method. But then he changed his mind and just made event private. From that point of view ChildMethod is some kind of simulation of user handling the event.
I don't see any advantages of using private events. Disadvantages like difficult to understand code and additional unnecessary objects generation are obvious.

MAKKAM
FYI, the functionality is implemented and got called. And Yes, I agreed with your points. :)
Gan