Return True? That's It? WTF?

I posted some code the other day at TheDailyWTF.com that used some pretty intense error handling for a function that simply returned "True". Of course, the way to do it would have been:

Private Function CheckOperation() As Boolean
Return True
End Function


I noticed in the comments that a few people thought it odd that there existed a function (in production code, no less) that did nothing but return a static value. But there's nothing out of the ordinary there. I've developed numerous classes with methods that do just that: return a static value. And no, they're not stubs.

Here's an example from something I recently developed. The system allowed importing through various data sources. To accomodate this, I built an interface:

Public Interface IImportable
  ReadOnly Property SupportsAsynchronousTransfer() As Boolean
  ReadOnly Property ReadyState() As ImportReadyState
  Sub Import(ByVal conduit As DataConduit)
End Interface

As you can see, some data sources can support asynch transfer, others cannot. And now for the class that implements this:

Public Class CsvDataSource
  Inherits DataSource
  Implements IImportable

  'Snipped non-IImportable code

  Public Sub Import(ByVal conduit As DataConduit) _
        Implements IImportable.Import
    'Snipped code for method
  End Sub

  Public ReadOnly Property ReadyState() As ImportReadyState _
        Implements IImportable.ReadyState
    Get
      Throw New NotSupportedException()
    End Get
  End Property

  Public ReadOnly Property SupportsAsynchronousTransfer() As Boolean _
        Implements IImportable.SupportsAsynchronousTransfer
    Get
      Return False
    End Get
  End Property
End Class

Sure, it's a property in this case, but I think you get the idea ...

2 Comments

  • I think this is a totally valid example of methods returning static values.

  • Yup, I agree too. The fact that your class Inherits some other class which means that the property may be set to true some other place and be variable at another.



    I'm sure you wouldn't put try...catch handling on that Get though. :p

Comments have been disabled for this content.