Code guidelines- Returning multiple parameters

by Marius Gheorghe 22. January 2007 22:10
I'm currently reading "Framework Design Guidelines" and enjoying a lot. The book is tight packed with great information for designing solid APIs. While reading the book it happens that a lot of times i nod my head while thinking "yeah....yeah...that's exactly how it should be implemented". Some other times i don't really agree with the framework designers. For instance here is a quote about ref and out parameters in the context of returning multiple results from a function.

Anders Hejlsberg As a rule, I am not too crazy about ref and out parameters in APIs. A such APIs compose very poorly, forcing you to declare temporary variables. I much prefer functional designs that convey the entire result in the return value

Unfortunately i can't agree here because this recommendation conflicts with one of the most important guideline in software development : less code. . Here is an example. Compare :

public struct ResultInfo
{
private string name;
private int age;

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
ResultInfo info = this.GetMyStuff();


to this :

string name = string.Empty;
int age= 0;
this.GetMyStuff(ref name, ref age);


Less code = simpler = better solution from my point of view.

Tags:

dotNET

Comments are closed

marius gheorghe

developer, dad, gamer

Contact me

My Resume

Favourite Tools