Click here to Skip to main content
11,641,668 members (58,006 online)
Click here to Skip to main content

Good Practice in Method Design – TIP (Beginner/Intermediate)

, 20 Oct 2011 LGPL3 12.5K 4
Rate this:
Please Sign up or sign in to vote.
Dear Reader, In this post, i shall show you a small good practice that i follow and even many books suggests while designing our generic methods which we daily write. So today i was refactoring a code @my work place and due to privacy rules i will not post the exact code but similar kind [...]

Dear Reader,

In this post, I shall show you a small good practice that I follow and even many books suggests while designing our generic methods which we daily write.

So today I was refactoring a code at my work place and due to privacy rules I will not post the exact code but similar kind below:

public IList<T> GetList<T>()
{
IList<T>  IMyList = new List<T>();

return IMyList;
}

public void DoSomeWorkOnMyCollection<T>(List<T>myList)
{
//Some collection operations done.
}

For sure there is no flaw in this methods, but there is a slight problem with their design w.r.t extensibility. Upon further refactoring process I happen to see similar method DoSomeWorkOnMyCollection() implemented again somewhere else but this time it was for handling HashSet<T> type.

W.r.t to first method i.e  GetList<T>() the return type is more generic or more base type, which at the caller side i might have some problem.

So upon reading some books and blog i came to a conclusion that its better to have a return type of method to most derived type as shown below:

public List<T> GetList()
{
return new List<T>();
}

Such a way I do get flexibility in getting the return value to List<T> or IList<T> (more stronger) at the caller side. So its all matter of being flexible or choosy here.

Similarly w.r.t to the second method accepting more derived parameter type, this method is tightly coupled to the List<T> typed arguments, i wont be able to pass arrays or other types at all to this method, such way forcing me to write other methods or overloaded methods. So to have more of a generic code, i can go for:

public void DoSomeWorkOnMyCollection<T>(IList<T> myList)
{
//Some collection operations done.
}

Now I can pass Arrays, other types which implements IList<T> interface, thus again making the code more flexible.

Thanks

P.S: Any better practice, do let me know. Your valuable suggestions/comments/votes are appreciated

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

zenwalker1985
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 20 Oct 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid