I had an interesting discussion with a coworker that revolved around how people interpret the use of properties and methods on an interface. For example, let's say we have a blog with posts in various statuses: Working and Published.
When in "Working" the author is still making changes and shouldn't be visible to readers. When "Published", well... it's published and end users can read it.
Let's also say we have an interface for the Blog, which I defined as:
public interface IBlog
{
IEnumerable<Post> Posts { get; }
IEnumerable<Post> PublishedPosts { get; }
}
My coworker was worried that users of this interface might interpret these two properties as separate collections of objects. I figured since you have a "Posts" property, and another property called "Published Posts" that returns the same kind of object that people would make the correct assumption that Posts
is one collection, and PublishedPosts
is a filtered view of the Posts
collection.
His suggestion was:
public interface IBlog
{
IEnumerable<Post> Posts { get; }
IEnumerable<Post> GetPublishedPosts();
}
Basically, replace the PublishedPosts property with a GetPublishPosts() method. He said this was more idiomatic for C#, because a method communicates to people using the interface that you are performing an operation on the "Posts" collection (filtering it by status). I haven't really seen any formal documentation for this, but that doesn't stop the C# community from leaning one direction or the other.
Is having one collection property, and then methods to filter the collection, or having two collections where the second filters the first idiomatic for C#? If so, is there formal documentation anywhere?
If so, is there formal documentation anywhere?
There's no formal documentation for what's idiomatic. There can't really be. What's idiomatic is just what's commonly done. Nobody can formally document that.