C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,406 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,641 people online right now. Registration is fast and FREE... Join Now!




If a method has more than 2 parameters is it bad?

 

If a method has more than 2 parameters is it bad?

papuccino1

1 Jul, 2009 - 03:37 PM
Post #1

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Read a post on Martyrs blog today and he mentioned that if any method you have has more than 2 parameters thats a red flag that you have to refactor your code. Is this true?

On one of my programs I have a method that has 5 parameters, is this a bad programming practice?

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: If A Method Has More Than 2 Parameters Is It Bad?

1 Jul, 2009 - 03:39 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,245



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
If more than 2 of the OUT OR REF parameters you should CONSIDER refactoring.

There are of course multiple reasons to have more than 2 parameters.

wink2.gif

Edit: The reason is that if you find yourself passing it more than two out or ref parameters (which are designed to then be modified or used as return values your function) it is probably a bit too complex to be contained in a single function.

But as for regular function parameters, having more than 2 is certainly common and nothing wrong with that. Look at the .NET framework, you will find tons of methods which have 2 or more. But there is a limit on those as well. If you find your functions taking upwards of 5 or 6 parameters then again you should look at refactoring the method because it is obviously doing more than it probably should.

This post has been edited by Martyr2: 1 Jul, 2009 - 03:44 PM
User is online!Profile CardPM
+Quote Post

papuccino1

RE: If A Method Has More Than 2 Parameters Is It Bad?

1 Jul, 2009 - 03:53 PM
Post #3

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Thanks for the hasty answer.

Another question!

For example, I have a method that receives the URL of a movie for the IMDB website, inside that method I filter out whatever it is I'm trying to find of the movie: Title, Release Year, Genre, etc.

That method is inside the Movie.cs class.

In my Form1.cs WinForm class I call that method and pass (textBox1, textBox2, etc).

Am I using method properly or merely doing it wrong. tongue.gif
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: If A Method Has More Than 2 Parameters Is It Bad?

1 Jul, 2009 - 05:52 PM
Post #4

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(papuccino1 @ 1 Jul, 2009 - 05:53 PM) *

Thanks for the hasty answer.

Another question!

For example, I have a method that receives the URL of a movie for the IMDB website, inside that method I filter out whatever it is I'm trying to find of the movie: Title, Release Year, Genre, etc.

That method is inside the Movie.cs class.

In my Form1.cs WinForm class I call that method and pass (textBox1, textBox2, etc).

Am I using method properly or merely doing it wrong. tongue.gif


I would make a class that would hold the Tile, release, year, etc and pass that class the URL as a parameter.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:51PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month