编写高质量代码三—函数的命名规则

作者: 归零者 分类: 编程规范 发布时间: 2018-09-01 13:21

本篇总结函数的命名规则 思维导图: 函数命名规则.png

范例

using System;

public class SetupTeardownIncluder

{

      private PageData pageData;

      private boolean isSuite;

      private WikiPage testPage;

      private StringBuffer newPageContent;

      private PageCrawler pageCrawler;

public static String render(PageData pageData) throws Exception
{
    return render(pageData, false);
}
public static String render(PageData pageData, boolean isSuite)throws Exception
{
    return new SetupTeardownIncluder(pageData).render(isSuite);
}
private SetupTeardownIncluder(PageData pageData)
{
    this.pageData = pageData;
    testPage = pageData.getWikiPage();
    pageCrawler = testPage.getPageCrawler();
    newPageContent = new StringBuffer();
}

private String render(boolean isSuite) throws Exception
{
    this.isSuite = isSuite;
    if (isTestPage())
    includeSetupAndTeardownPages();
    return pageData.getHtml();
}

private boolean isTestPage() throws Exception
{
    return pageData.hasAttribute("Test");
}

private void includeSetupAndTeardownPages() throws Exception
{
    includeSetupPages();
    includePageContent();
    includeTeardownPages();
    updatePageContent();
}

private void includeSetupPages() throws Exception
{
    if (isSuite)
        includeSuiteSetupPage();
    includeSetupPage();
}

private void includeSuiteSetupPage() throws Exception
{
    include(SuiteResponder.SUITE_SETUP_NAME, "-setup");
}

private void includeSetupPage() throws Exception
{
    include("SetUp", "-setup");
}

private void includePageContent() throws Exception
{
    newPageContent.append(pageData.getContent());
}

private void includeTeardownPages() throws Exception
{
    includeTeardownPage();
    if (isSuite)
    includeSuiteTeardownPage();
}

private void includeTeardownPage() throws Exception
{
    include("TearDown", "-teardown");
}

private void includeSuiteTeardownPage() throws Exception
{
    include(SuiteResponder.SUITE_TEARDOWN_NAME, "-teardown");
}

private void updatePageContent() throws Exception
{
    pageData.setContent(newPageContent.toString());
}

private void include(String pageName, String arg) throws Exception
{
    WikiPage inheritedPage = findInheritedPage(pageName);
    if (inheritedPage != null) 
    {
        String pagePathName = getPathNameForPage(inheritedPage);
        buildIncludeDirective(pagePathName, arg);
    }
}

private WikiPage findInheritedPage(String pageName) throws Exception
{
    return PageCrawlerImpl.getInheritedPage(pageName, testPage);
}

private String getPathNameForPage(WikiPage page) throws Exception
{
    WikiPagePath pagePath = pageCrawler.getFullPath(page);
    return PathParser.render(pagePath);
}

private void buildIncludeDirective(String pagePathName, String arg)
{
    newPageContent
    .append("\n!include ")
    .append(arg)
    .append(" .")
    .append(pagePathName)
    .append("\n");
}
}

 

上面这段代码,满足了函数书写短小、单一职责、命名合适、参数尽可能少、不重复啰嗦这几条准则。整洁的函数代码大致如此。

总结: 1.短小。若没有特殊情况,最好将单个函数控制在十行以内(这个问题比较两极,主要看个人喜好)。 2.单一职责。函数应该只做一件事情。只做一件事,做好这件事。 3.命名合适且具描述性。长而具有描述性的名称,比短而令人费解的名称好。当然,如果短的名称已经足够说明问题,还是越短越好。 4.参数尽可能少。最理想的函数参数形态是零参数,其次是单参数,再次是双参数,应尽量避免三参数及以上参数的函数,有足够的理由才能用三个以上参数。 5.尽力避免重复。重复的代码会导致模块的臃肿,整个模块的可读性可能会应该重复的消除而得到提升。

本文非原创,看原文请关注此大神:https://my.csdn.net/zhmxy555

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

说点什么

avatar
  Subscribe  
提醒
跳至工具栏