One method with many behaviours or many methods

Posted by Krowar on Programmers See other posts from Programmers or by Krowar
Published on 2014-05-26T13:23:22Z Indexed on 2014/05/26 22:00 UTC
Read the original article Hit count: 336

This question is quite general and not related to a specific language, but more to coding best practices.

Recently, I've been developing a feature for my app that is requested in many cases with slightly different behaviours.

This function send emails , but to different receivers, or with different texts according to the parameters. The method signature is something like

public static sendMail (t_message message = null  , t_user receiver = null , stream attachedPiece = null)

And then there are many condition inside the method, like

if(attachedPiece != null)
{
}

I've made the choice to do it this way (with a single method) because it prevents me to rewrite the (nearly) same method 10 times, but I'm not sure that it's a good practice.

What should I have done? Write 10 sendMail method with different parameters?

Are there obvious pros and cons for these different ways of programming?

Thanks a lot.

© Programmers or respective owner

Related posts about programming-practices

Related posts about patterns-and-practices