11

C# has a few naming conventions for commonly-seen method types:

  • BeginFoo() / EndFoo() for async methods
  • TryGet() / TryParse() that return false instead of throwing an exception
  • FooOrDefault() for methods that return default(T) instead of throwing an exception
  • IsFoo for boolean flags

I was wondering, is there one for recursive inner methods? e.g. in this example from another Stack Overflow question:

public int CalculateSomethingRecursively(int someNumber)
{
    return doSomethingRecursively(someNumber, 0);
}

// What to call this? 
private int doSomethingRecursively(int someNumber, int level)
{
    if (level >= MAX_LEVEL || !shouldKeepCalculating(someNumber))
        return someNumber;
    return doSomethingRecursively(someNumber, level + 1);
}

In C I have seen people use foo(...) + foo_r(...) as a convention. But how about in .NET?

Community
  • 1
  • 1
Richard Dingwall
  • 2,692
  • 1
  • 31
  • 32
  • 2
    +1 Good question - I remember some of my college classes used "husk" and "kernel". Made me feel like a farmer. – Paul Bellora Oct 18 '11 at 21:39
  • I have prefixed private methods I intend to call recursively with "Inner", so I may have a method called Add() and then an InnerAdd(). – Dave Ziegler Oct 18 '11 at 21:48
  • @DaveZiegler `InnerAdd` is good naming. I use it too. MS has an [InnerAdd](http://msdn.microsoft.com/en-us/library/hh860431(v=pandp.51).aspx) method as well (though not dealing with recursion). – nawfal May 28 '13 at 12:22
  • This is very common in Haskell, where one popular convention is "go": https://kowainik.github.io/posts/haskell-mini-patterns#recursive-go An interesting archaeology here: https://stackoverflow.com/a/5844850/3697870 – Heath Raftery May 30 '23 at 10:48

2 Answers2

9

Personally I'd probably call it CalculateSomethingRecursivelyImpl - I tend to use Impl as a suffix for "the private method which actually does the bulk of the work of the method which has the same name but without the suffix." The fact that it's recursive wouldn't change that for me - but it's only a personal choice.

To be honest though, such a method would presumably always be private - so it doesn't matter nearly as much as for public / protected methods. Just work out a convention with your other team members.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Honestly, I would say the same C# naming conventions that apply for the non recursive function also apply for the recursive one. Try naming it something descriptive but relatively short. And if you really can't think of anything different than the non-recursive one, then you will have to experiment with suffixes. I prefer "Helper", though I try avoiding suffix use.

ctrlc-root
  • 1,049
  • 1
  • 15
  • 22