We had a recent thread internally that resulted in me updating the guidelines below… Please let me know if you have any questions or comments.
Do provide overloads for methods with optional arguments. If a method takes some arguments that are not required provide overloads of the method without the optional arguments. See section X.Y on method overloading. That is if you have this method here geometry is optional, provide an overload without it as well.public void DrawGeometry(Brush brush, Pen pen, Geometry geometry);public void DrawGeometry(Brush brush, Pen pen);
Do allow null to be passed for optional arguments. If a method takes optional arguments that are reference types, allow null to be passed to indicate the default value should be used. This avoids the problem of having to check for null before calling an API as shown below: if (brush == null) DrawGeometry(pen, geometry); else DrawGeometry(pen, geometry, brush);
Annotation (Adam Smith): It sure seems that we're providing a less startling API if we define reasonable, expected behavior when "null" is passed for a param. If that's really impossible, sure, throw an exception, but to throw when there's a consistent, non-crashing thing we could have done seems unhelpful.