When is a function name too long?

后端 未结 27 1380
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 18:45

I try to be rather descriptive with my function names, where possible. This occasionally results in function names in the twenty to thirty character range such as \"GetActionFr

相关标签:
27条回答
  • 2021-02-03 18:57

    Function and method names start getting too long when the developer forgets about the descriptive nature of arguments (assuming meaningful argument and variable names). For example:

     my $template = HTML::Template->new( filename => 'home.html'); 
     $template->param( title => 'Home Page' );
     $html = $template->output;
    

    is transparent in what it does even if you know no Perl and have never heard of HTML::Template.

    I have seen too often the temptation to name that output method something like RenderHTMLViewFromTemplateObject. If all the libraries used have such naming conventions, it becomes simply impossible for me to follow what is going on.

    0 讨论(0)
  • 2021-02-03 19:00

    I don't think a method name can be too long as long as it is descriptive. If a method name goes over 40 characters I will see if I can convey the same meaning in fewer words. If not I'll live with the long name in for the sake of clarity.

    0 讨论(0)
  • 2021-02-03 19:02

    If there's a shorter, but yet descriptive way to name the function, then the function name is too long.

    0 讨论(0)
  • 2021-02-03 19:03

    From Code Complete (1st Edition, page 188)

    "Gorla, Benander and Benander found that the effort required to debug a COBOL program was minimized when variables had names that averaged 10 to 16 characters (1990). Programs with names averaging 8 to 20 characters were almost as easy to debug."

    This constitutes the only empirical discussion of a reasonable guideline for variable name length I've ever seen. Everything else is down to opinion and comfort.

    0 讨论(0)
  • 2021-02-03 19:03

    As long as the functions actually do what their name suggest they do and you're not going for a code golf entry, I think it's a good thing.

    0 讨论(0)
  • 2021-02-03 19:03

    I think that this is especially important for public names - they should not be too long, but how long too long is is very subjective. Always have better a longer and descriptive name than a too short name.
    For private methods even very long names are really no problem in my opinion.

    0 讨论(0)
提交回复
热议问题