Naming convention for inner functions

  • Thread starter Thread starter Telmo Costa
  • Start date Start date
T

Telmo Costa

What should i call the functions named Factorial_ in this sample code:

class SomeClass {

...
public int Factorial(int f) {
if (f<0) throw SomeException("Some message.");
if (f==0 || f==1) return 1;
return Factorial_(f);
}

private int Factorial_(int f) {
if (f==1) return 1;
return f*Factorial_(f-1);
}
...
}

Calling it Factorial_ looks bad. Cannot call Factorial because it is the
name that the public method should have. What should it be?

telmo
 
Why do you need 2 functions? Can't you combine this into 1?

It is usually not an issue, since most of the time there wouldn't be a
conflict between the names like that. But when it does happen, I would guess
everyone has their own conventions.
 
I don't believe there is any real convention for how to name private
functions. In your example I would name it something like FactorialInternal.
 
Marina said:
Why do you need 2 functions? Can't you combine this into 1?

Factorial was just an example.
There are many situations where functions cannot be combine. In this
case is just because Factorial_ is a recursive function and Factorial
code should be executed only once.
 
Hi,

One recommendation is to name private elements of classes with names
starting with lowercase letter, so you could use 'factorial',
'factorialHelper', etc.

Regards - Octavio
 
Octavio Hernandez said:
One recommendation is to name private elements of classes with names
starting with lowercase letter, so you could use 'factorial',
'factorialHelper', etc.

I've never seen that, and it goes against the MS naming conventions,
which state that Pascal case should be used. There's nothing saying
that's only for public methods, and changing it just for private
methods introduces inconsistency in my view.
 
Telmo Costa said:
What should i call the functions named Factorial_ in this sample code:

I usually use something like InternalXxx if the method is non-virtual, and
something like XxxCore or XxxImpl if the method is virtual.
 
It's very rare that I write a private method that does _exactly the
same_ thing as a public method, and so run into such a naming conflict.
I would be tempted to call it something like CalculateFactorial().

I find it far more common that the private method does some _part_ of a
larger job, in which case I name it based on what it does without
running into a naming conflict.

Be careful that by creating an "internal method" naming convention you
don't encourage people to start blindly naming their methods
XxxInternal just because they're used from the Xxx public method,
without really considering what the internal method is doing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top