When are predicates appropriate and what is the best pattern for usage

Posted by Maxim Gershkovich on Stack Overflow See other posts from Stack Overflow or by Maxim Gershkovich
Published on 2011-01-11T03:19:40Z Indexed on 2011/01/11 4:53 UTC
Read the original article Hit count: 183

Filed under:
|
|
|

When are predicates appropriate and what is the best pattern for usage? What are the advantages of predicates?

It seems to me like most cases where a predicate can be employed a tight loop would accomplish the same functionality?

I don’t see a reusability argument given you will probably only implement a predicate in one method right?

They look and feel nice but besides that they seem like you would only employ them when you need a quick hack on the collection classes?

UPDATE

But why would you be rewriting the tight loop again and again?

In my mind/code when it comes to collections I always end up with something like

Class Person
End Class

Class PersonList
    Inherits List(Of Person)

    Function FindByName(Name) as Person
         tight loop....
    End Function
End Class

@Ani

By that same logic I could implement the method as such

Class PersonList
    Inherits List(Of Person)

    Function FindByName(Name) as PersonList
    End Function

    Function FindByAge(Age) as PersonList
    End Function

    Function FindBySocialSecurityNumber(SocialSecurityNumber) as PersonList
    End Function
End Class

And call it as such

Dim res as PersonList = MyList.FindByName("Max").FindByAge(25).FindBySocialSecurityNumber(1234)

and the result along with the amount of code and its reusability is largely the same, no?

I am not arguing just trying to understand.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET