views:

91

answers:

3

I often need to use a function which performs and action X is condition Y is set. What is the best way to name such a function?

I don't want to repeat if statements, since they could be complex.

For instance, if I want to trim a string if a property is set, function could be named:

  • void TrimIfOptionSet(string) -- too unwieldy, especially if condition is complex
  • bool TryTrim(string) -- does not mention an external condition, I'd expect it to only take the argument into account.
  • void ConditionalTrim(string) -- bit verbose

Are there any conventions for this situation in C#/.Net, or any similar language?

+4  A: 

Try something like:

if(IsComplexCondition(complexData))
{
    DoThing(otherData);
}

You generally don't want to couple the condition with the operation because you're making a single function capture too much semantic information at that point. It's "doing more than one thing." Instead, if you have a complex condition, capture that condition in a function to encapsulate it.

If you're referring to a much more common situation, such as parameter validation at the top of functions, consider something like fluent parameter validation. If you're not doing something like parameter validation, then I might question why it's at the top of every function and not captured in a common location or performed once at a system boundary.

I don't think that there is a good answer for naming the general ActionIfSomething() case simply because it's not generally a good solution to a problem. I'd probably just say make the function call Action() and document it, perhaps in <remarks>, to only perform the action when Something is true. If the Action belongs with the condition in the function, then it only makes sense in the context of that condition, so re-specifying it in the function name is redundant.

Greg D
Good argument, and I agree in principle, but not practical if 4 lines are repeated *very* often (e.g. on top of every public function). The condition+action coupling smells bad, but *if* I had to encapsulate it, what would be the best name for it?
dbkk
Ah, if you're doing it at the top of every function, check out the code contracts in .Net 4. I assume you're talking about parm validation. Another option is the use of "fluent interfaces" to make the parameter checking more readable.
Greg D
+2  A: 

Could this be solved through the use of accessors?

public class MyObject
{
     private string _content = string.Empty;

     public bool Trim { get; set; }

     public string Content
     {
          get
          {
               return this.Trim ? _content.Trim() : _content;
          }
          internal set
          {
               if (string.IsNullOrEmpty(value))
                    _content = string.Empty; 
               else
                    _content = value;
          }
     }
}

This will take the action determined by the Trim boolean whenever the Content is accessed. I've protected the set accessor, since it might be somewhat ambigious what happens when the value of Content is set while Trim is true, and checked for the special case of trying to set the content to null. Good documentation should cover these cases.

Scott Jackson
Works for a specific case (String.Trim), but not in the general ActionIfCondition case (e.g. if I wanted to modify the argument and not return a value. Thanks and welcome to SO :)
dbkk
In the no-return-value case, I believe I'd name things according to intent. This is harder for me to generalize, but something along the lines of "void Normalize();", where it's understood that Normalize may evaluate certain (variable?) conditions to determine just what "Normal" might be.This is especially true for publicly exposed functionality. The "ActionIfOption" style of naming may be exposing too much implementation detail to an external consumer.Internally, I'd not be afraid to be quite verbose. It's potentially clearer to those who come after me.And thank you, glad to be here.
Scott Jackson
+2  A: 

Given the constraints, I'd pick TrimIfOptionSet or TrimIfNeeded.

  • TryTrim feels like it'll always run a trim operation (in a try block), which isn't the same as running it only if needed
  • ConditionalTrim is too long -- the reader's eyes stay on "conditional" and never get to "trim"
Anirvan