picoe / Eto

Cross platform GUI framework for desktop and mobile applications in .NET
Other
3.67k stars 333 forks source link

Use regular methods instead of Extension methods wherever possible #92

Closed corliss closed 10 years ago

corliss commented 11 years ago

Eto has a few places where methods have been implemented as extensions that could been implemented as regular methods. From the .net Framework Design Guidelines: If you do own source code of a type, consider using regular instance methods instead.

One example of this is the (now obsolete) AddDockedControl(this DockContainer, Control). When typing container. the method does not show in Intellisense if the extension's namespace has not been imported. Worse, the developer is left wondering whether something else is wrong - it isn't easy to remember that a given method is an extension. This particular extension method has cost many cumulative minutes of productivity.

Here's a list of methods in the Eto assembly that are not implemented on interfaces. I haven't scanned these to determine which really need to be extensions. Some are implemented as extensions to allow the "this" variable to be null without crashing. I suggest that those methods have the Safe suffix to indicate this.

  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(23):      public static DualBinding Bind(this Widget widget, string widgetPropertyName, object source, string sourcePropertyName, DualBindingMode mode = DualBindingMode.TwoWay)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(44):      public static DualBinding Bind(this Widget widget, string widgetPropertyName, DirectBinding sourceBinding, DualBindingMode mode = DualBindingMode.TwoWay)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(70):      public static DualBinding Bind(this InstanceWidget widget, string widgetPropertyName, string dataContextPropertyName, DualBindingMode mode = DualBindingMode.TwoWay, object defaultWidgetValue = null, object defaultContextValue = null)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(77):      public static DualBinding Bind<W,WP,S,SP>(this W widget, Expression<Func<W,WP>> widgetProperty, S source, Expression<Func<S, SP>> sourceProperty, DualBindingMode mode = DualBindingMode.TwoWay)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(85):      public static DualBinding Bind<W, WP, SP, DC>(this W widget, Expression<Func<W, WP>> widgetProperty, Expression<Func<DC, SP>> sourceProperty, DualBindingMode mode = DualBindingMode.TwoWay, object defaultWidgetValue = null, object defaultContextValue = null)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(93):      public static DualBinding Bind(this InstanceWidget widget, IndirectBinding widgetBinding, DirectBinding valueBinding, DualBindingMode mode = DualBindingMode.TwoWay)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(98):      public static DualBinding Bind(this InstanceWidget widget, IndirectBinding widgetBinding, object objectValue, IndirectBinding objectBinding, DualBindingMode mode = DualBindingMode.TwoWay, object defaultWidgetValue = null, object defaultContextValue = null)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(107):     public static DualBinding Bind(this InstanceWidget widget, IndirectBinding widgetBinding, IndirectBinding dataContextBinding, DualBindingMode mode = DualBindingMode.TwoWay, object defaultWidgetValue = null, object defaultContextValue = null)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(112):     public static DualBinding Bind(this ObjectBinding widgetBinding, DirectBinding valueBinding, DualBindingMode mode = DualBindingMode.TwoWay)
  C:\dev\Mindnote\Eto\Source\Eto\Binding\BindingExtensions.cs(125):     public static DualBinding Bind(this ObjectBinding widgetBinding, IndirectBinding dataContextBinding, DualBindingMode mode = DualBindingMode.TwoWay, object defaultWidgetValue = null, object defaultContextValue = null)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\ButtonAction.cs(12):     public static ButtonAction AddButton(this ActionCollection actions, string id, string text)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\ButtonAction.cs(19):     public static ButtonAction AddButton(this ActionCollection actions, string id, string text, string iconResource, EventHandler<EventArgs> activated, params Key[] accelerators)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\ButtonAction.cs(29):     public static ButtonAction AddButton(this ActionCollection actions, string id, string text, string iconResource, EventHandler<EventArgs> activated)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\ButtonAction.cs(38):     public static ButtonAction AddButton(this ActionCollection actions, string id, string text, Icon icon, EventHandler<EventArgs> activated)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\ButtonAction.cs(46):     public static ButtonAction AddButton(this ActionCollection actions, string id, string text, EventHandler<EventArgs> activated, params Key[] accelerators)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\ButtonAction.cs(54):     public static ButtonAction AddButton(this ActionCollection actions, string id, string text, EventHandler<EventArgs> activated)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\CheckAction.cs(12):      public static CheckAction AddCheck(this ActionCollection actions, string id, string text, string iconResource, EventHandler<EventArgs> activated)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\CheckAction.cs(17):      public static CheckAction AddCheck(this ActionCollection actions, string id, string text, EventHandler<EventArgs> activated)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\CheckAction.cs(22):      public static CheckAction AddCheck(this ActionCollection actions, string id, string text, string iconResource, EventHandler<EventArgs> activated, params Key[] accelerators)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\CheckAction.cs(32):      public static bool RemoveCheckHandler(this ActionCollection actions, string actionID, EventHandler<EventArgs> checkChangedHandler)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\RadioAction.cs(9):       public static RadioAction AddRadio(this ActionCollection actions, RadioAction controller, string id, string text)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Actions\RadioAction.cs(14):      public static RadioAction AddRadio(this ActionCollection actions, RadioAction controller, string id, string text, string iconResource, EventHandler<EventArgs> activated, params Key[] accelerators)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Key.cs(253):     public static string ToShortcutString(this Key key, string separator = "+")
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Layout\DockLayout.cs(35):        public static Container AddDockedControl (this DockContainer container, Control control, Padding? padding = null)
  C:\dev\Mindnote\Eto\Source\Eto\Forms\Menu\ImageMenuItem.cs(11):       public static ImageMenuItem Add (this MenuItemCollection items, string text)
  C:\dev\Mindnote\Eto\Source\Eto\Generator.cs(52):      public static T Create<T> (this Generator generator)
  C:\dev\Mindnote\Eto\Source\Eto\Generator.cs(68):      public static T CreateShared<T> (this Generator generator)
  C:\dev\Mindnote\Eto\Source\Eto\Generator.cs(78):      public static Func<T> Find<T> (this Generator generator)
  C:\dev\Mindnote\Eto\Source\Eto\Generator.cs(84):      public static Dictionary<K, V> Cache<K, V> (this Generator generator, object cacheKey)
cwensley commented 10 years ago

Bulk of these were in actions, which are now gone. Only ones left are ToShortcutString which is on an enum (so no other choice), and the generator-based ones. These may be going away soon when implementing #161.