I recently came across a piece of code something like this (roughly C#):
public bool ValidateStuff(ref ArrayList listOfErrors, Stuff thingsToValidate)
{
if (!thingsToValidate.isValid() )
{
errors.add("New error!");
}
}
ArrayList errors = [];
bool valid = ValidateStuff(ref errors, stuffToValidate);
The key thing is the "ref" keyword - there is no need to use that keyword given the functionality here, and when I asked why it was there I was told that it had only been added as a kind of warning that the errors parameter is manipulated by the method, it's not purely used as input.
This immediately seemed wrong to me (ref has a purpose, and here it is not being used according to its purpose), but the idea of forcing (since you have to use ref when calling the method) a reminder of slightly counter-intuitive usage seemed reasonable, and I couldn't immediately think of a better way, apart from liberal use of comments. What do you think?
Extra credit: how could this method be better? Perhaps by simply returning a new List of errors which is added to the main List in the calling code? By providing two methods, one for the test and one for the errors? Or is it fine as it is?