如何消除空参数而不使用冗余代码

发布于 2024-12-12 05:24:36 字数 392 浏览 0 评论 0原文

例如:

getBooks(author, title)
  • 如果允许作者为空,将返回具有特定标题的所有书籍
  • 如果允许标题为空,将返回特定作者的所有书籍
  • 如果允许两者都为空,则将返回所有书籍,无论标题或作者

如何 消除this,具有以下功能:

getBooks(author) 
getBooks(title)  
getBooks(author, title)  
getBooks()

在新函数中,可能存在冗余代码,或者如果我们将这些冗余代码分组到一个函数中,我们仍然会进入一个具有空参数的函数。有什么更好的方法来处理这个问题——没有冗余代码,也没有空参数?

For example:

getBooks(author, title)
  • If allowing author to be null, would return all books with specific title
  • If allowing title to be null, would return all books for the specific author
  • If allowing both to be null, would return all books regardless of title or author

To eliminate this, have the following functions:

getBooks(author) 
getBooks(title)  
getBooks(author, title)  
getBooks()

In the new functions, there might be redundant codes or if we group those redundant codes into a function, we will still get into a function having null parameters. What's a better way to handle this - no redundant code and no null parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

活雷疯 2024-12-19 05:24:36

不要过载太多:

getBooksByAuthor(author) 
getBooksByTitle(title)  
getBooksByAuthorAndTitle(author, title)  
getBooks()

请注意,这不会减少代码重用:这些方法可以重用/共享它们在实现中需要的任何代码

Don't overload so much:

getBooksByAuthor(author) 
getBooksByTitle(title)  
getBooksByAuthorAndTitle(author, title)  
getBooks()

Note that this will not reduce code reuse: These methods could reuse/share whatever code they needed to in their implementations

风筝在阴天搁浅。 2024-12-19 05:24:36

您可以使用常量来表示要执行的搜索类型,并检查是否传递了参数(未经测试且已检查错误):

public static final int R_AUTH = 1;
public static final int R_BOOK = 2;
public static final int R_ALL = 3;

public bookSearch( int searchType, String... search )
{
    switch( searchType )
    {
        case R_AUTH:
          // search based off of (String)search[0].stringValue();
        break;
        case R_ALL:
          // load all
        break;
    }
}

bookSearch(R_ALL);
bookSearch(R_AUTH, "Poe");

You could use a constant to denote what type of search to do, and check to see if a param was passed (very untested and error checked):

public static final int R_AUTH = 1;
public static final int R_BOOK = 2;
public static final int R_ALL = 3;

public bookSearch( int searchType, String... search )
{
    switch( searchType )
    {
        case R_AUTH:
          // search based off of (String)search[0].stringValue();
        break;
        case R_ALL:
          // load all
        break;
    }
}

bookSearch(R_ALL);
bookSearch(R_AUTH, "Poe");
陪我终i 2024-12-19 05:24:36

假设作者和标题是字符串,您可以执行以下操作:

public List getBooks(String params ...) {
    if (params.length == 0) { //search 
     //do search all books regardless of title or author
    } else if (params.length == 2 && "cte_author".equals(params[1])) {
     //do search  by author
    } else if (params.length == 2 && "cte_title".equals(params[1])) {
     //do search by title
    } else if (params.length == 2){
     //do search by title and book
    } else {
     //throw exception
    }
}

因此您可以按如下方式使用此方法:

getBooks();
getBooks("Gabriel Garcia Marquez", "cte_author");
getBooks("Cien anios de soledad", "cte_title");
getBooks("Gabriel Garcia Marquez","Cien anios de soledad");

Assuming that author and title are Strings you can do the following:

public List getBooks(String params ...) {
    if (params.length == 0) { //search 
     //do search all books regardless of title or author
    } else if (params.length == 2 && "cte_author".equals(params[1])) {
     //do search  by author
    } else if (params.length == 2 && "cte_title".equals(params[1])) {
     //do search by title
    } else if (params.length == 2){
     //do search by title and book
    } else {
     //throw exception
    }
}

So you can use this method as following:

getBooks();
getBooks("Gabriel Garcia Marquez", "cte_author");
getBooks("Cien anios de soledad", "cte_title");
getBooks("Gabriel Garcia Marquez","Cien anios de soledad");
π浅易 2024-12-19 05:24:36

尝试这种方法:

getAllBooks()            {return getBooks("", "");}
getBooksByAuthor(author) {return getBooks(author, "");}
getBooksByTitle(title)   {return getBooks("", title);}
getBooksByAuthorAndTitle(author, title)
{
    if(author.equals("") && title.equals(""))
    {
        return //your book list
    }
    else if(author.equals(""))
    {
        return //a list of books with the given title
    }
    else if(title.equals(""))
    {
        return //a list of books with given author
    }
    else
    {
        return //a list of books with the given title and author
    }
}

编辑
更新以避免歧义。

Try this approach:

getAllBooks()            {return getBooks("", "");}
getBooksByAuthor(author) {return getBooks(author, "");}
getBooksByTitle(title)   {return getBooks("", title);}
getBooksByAuthorAndTitle(author, title)
{
    if(author.equals("") && title.equals(""))
    {
        return //your book list
    }
    else if(author.equals(""))
    {
        return //a list of books with the given title
    }
    else if(title.equals(""))
    {
        return //a list of books with given author
    }
    else
    {
        return //a list of books with the given title and author
    }
}

Edit:
Updated to circumvent ambiguity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文