rubberduck-vba / Rubberduck

Every programmer needs a rubberduck. COM add-in for the VBA & VB6 IDE (VBE).
https://rubberduckvba.com
GNU General Public License v3.0
1.92k stars 302 forks source link

Flag Public event handlers and interface implementations #5730

Open retailcoder opened 3 years ago

retailcoder commented 3 years ago

What Code Quality inspection flagging Public members that are event handlers or interface implementations.

Why Making these members public puts them on the class' default interface, which means underscored members appear in the list of members of the containing class module when an object variable is declared with the class' default interface (i.e. the class name). Instead of being invoked directly, a dedicated (not underscored) public member on the class' default interface should be added; the implementation can be moved to the new public member and invoked from the private interface implementation.

Example This code should trigger the inspection:

Public Sub Worksheet_BeforeSave(Cancel As Boolean)
    '...do stuff...
End Sub

QuickFixes Should Rubberduck offer one or more quickfix(es) for this inspection? Describe them here (note: all inspections allow for IgnoreOnceQuickFix, unless explicitly specified):

  1. Move implementation to Public method

    Kind of an "extract method lite, where we just grab the whole entire method body and mirror the signature:

    Example code, after quickfix is applied:

    Public Sub DoStuff(Cancel As Boolean)
        '...do stuff...
    End Sub
    
    Private Sub Worksheet_BeforeSave(Cancel As Boolean)
        DoStuff Cancel
    End Sub

Resources Each inspection needs a number of resource strings - please provide a suggestion here:

MDoerner commented 3 years ago

The quickfix will be really easy once we have implemented extract method; it just needs to make the method private and then use the refactoring action.