How to overcome too big Method-Signatures

16. October 2010

In software development, one is often tempted to have method-sginatures with ten or more paramters, which isn`t good at all and will make your method difficult to understand and maintain. Some programmers use global variables to avoid passing too many parameters around, but that`s bad design and very risky, if other parts of the program overrides the same variable! Please don`t do that, that`s really a NO-GO!!

Imagine that your code looks like this:

private Boolean InitializePreloader(Int32 id, String opCode, String logFile, String mbxPos, Int32 mbxType, String info, String foo, String bar, Byte sign)
	//do some work...
	return true;

All of these parameters relate to one purpose, to initialize an object, in this case a preloader! What you can do now is to write a class for example called PreloaderSettings.cs . In this class you define all the parameters of the InitializePreloader-Method as properties! For example:

public class PreloaderSettings
    public Int32 Id {get; set;}
    public String OpCode { get; set; }
    public String LogFile { get; set; }
    public String MbxPos { get; set; }
    public Int32 MbxType { get; set; }
    public String Info { get; set; }
    public String Foo { get; set; }
    public String Bar { get; set; }
    public Byte Sign { get; set; }

After that your new method signature could look like this!

private Boolean InitializePreloader(PreloaderSettings settings)
    //do some work...
    return true;

Doesn`t that makes your code a lot cleaner, easier to maintain and to understand?!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: