naming abstracted methods

When a public method mostly just calls a private method to do it's work; say the private method is recursive; what is a good naming convention?

example,

1
2
3
4
5
6
7
8
9
10
11
12
private type fooRec(type stuff) 
{
    if (baseCase)
        return stuff;

    return fooRec(stuff); 
}

public type foo(type stuff) 
{
    return fooRec(stuff);
} 
Last edited on
Normally you should be able to give them the same name because the parameter lists should be different. If the parameter lists are not different, you may want to reconsider why you have a separate public and private version for them.
In my case, they each take and return strings. The public method firsts calls a private method which ensures that the string is valid, then it calls the private method that does the evaluation recursively.
Last edited on
I... what? Why even bother having two functions if one of them just calls the other and returns its value? Just put the body of fooRec into the body of foo.
I... what? Why even bother having two functions if one of them just calls the other and returns its value? Just put the body of fooRec into the body of foo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private type validate(type stuff) 
{
    //...
    return errorMssg; 
}

private type fooRec(type stuff) 
{
    if (baseCase)
        return stuff;

    return fooRec(stuff); 
}

public type foo(type stuff) 
{
    type mssg = validate(stuff);
    if (mssg.size())
        return mssg;

    return fooRec(stuff);
} 

This is what I have going on. I don't want to do the validation call from the recursive method for obvious reasons.
Last edited on
Perhaps you should consider whether or not this code should be responsible for validating it at this point - it may be that validation is not beneficial here.
I'd probably do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private type validate(type stuff) 
{
    static type last_stuff;
    static bool was_valid;
    if (stuff == last_stuff)
        return (was_valid) ? "" : errorMssg;
    //...
    return errorMssg; 
}

public type foo(type stuff) 
{
    type mssg = validate(stuff);
    if (mssg.size())
        return mssg;
    else if (baseCase)
        return stuff;
    return foo(stuff); 
}
Last edited on
There is no need to keep calling `validate()', only the first time is relevant.
I know, but if you call it multiple times it will just return the static versions. At least that way it won't do all the processing multiple times. It's not a very good solution though, since it's still slightly slower and it's also non-re-entrant.
Topic archived. No new replies allowed.