Programming by side effect is a practice I generally dislike with and tend to avoid when there’s a choice. What I mean is creating functions that take no parameters and return no value. The only way for these functions to accomplish anything is through side-effect. I will almost invariably choose the more explicit option, especially for public functions. In the example below, I would definitely use the GetGreenishColors function. I feel that the first function, LoadGreenishColors, is too ambiguous. Sure, you can reasonably expect it to do what its name suggests, but in order to conclusively determine its true effect – what it will load its results into – you actually have to go read the code. Alternativley, GetGreenishColors could be called much more explicitly like this.greens = this.GetGreenishColors(this.colors);
private
List<Color> colors = new List<Color>();
private List<Color> greens;
private void LoadGreenishColors()
{
this.greens = this.colors.FindAll(delegate(Color c)
{ return c.G > 128 ? true : false; }) ?? new List<Color>();
}
private List<Color> GetGreenishColors(List<Color> colorList)
{
return colorList.FindAll(delegate(Color c)
{ return c.G > 128 ? true : false; }) ?? new List<Color>();
}
Of course, side-effect functions are necessary. Take, for example, the InitilizeComponent function that is so familiar to Windows Forms developers. In fact, most initialization code of this sort is done by side-effect functions.
Oh, and on an unrelated note, check out the ?? operator. Pretty slick, eh?