What is the best practice for abstract classes with overriding methods that return nothing

What is the best practice for makeoverride / overridable methods in an abstract class that return nothing. The particular class that this abstract class inherits from can decide whether it requires functionality and can cancel as needed.

Now I understand that this is a function of the interface, but in the abstract class there is a lot of plumbing code that had to be “unclosed” to implement this.

My question is is it a bad design to have these methods in your project?

Example 1 The Concrete class requires implementation, but is forced to override this method.

(annotation)

Friend MustOverride Function GetTitle() As String 

(concrete)

 Friend Overrides Function GetTitle() As String Return nothing End Function 

Example 2 The Concrete class can override this method if required, but the abstract class contains a method that returns nothing.

(annotation)

 Friend Overridable Function GetTitle() As String Return nothing End Function 

(concrete)

 Friend Overrides Function GetTitle() As String Return "Title" End Function 
+4
source share
1 answer

It’s up to you to decide how you say “I don’t know.” But returning Nothing from an abstract base class is a rather paradoxical habit. It can throw a NullReferenceException in client code when either the programmer obtained from your class or the programmer who writes the client code does not understand that Nothing is a possible value. NRE is a shocking exception for diagnostics, the CLR is not able to give any meaningful hint of this. He cannot name a variable that stores zero, for example, he knows nothing.

This becomes especially difficult to diagnose because zero got at least two levels from where it goes wrong. A user who is burdened with a failure may have to speak with three programmers to understand the essence of the problem. And I will not be happy to finally get you on the phone.

If you cannot provide meaningful implementation of a member, then feel free to declare it abstract. This ensures that you will not talk to an angry user. Return String.Empty is an alternative. Perhaps this makes sense for your design, I can’t guess about it.

+3
source

Source: https://habr.com/ru/post/1488470/


All Articles