繁体   English   中英

为什么这个变量在初始化为空字符串时设置为空字符串?

[英]Why is this variable set to empty string when it is already initialized to an empty string?

我从内容提供商的开发者指南的第5个片段中获取了以下代码段

令人困惑的是,在第一个语句String[] mSelectionArgs = {""}; mSelectionArgs[0]设置为""

然后,如果mSearchString为空( TextUtils.isEmpty(mSearchString) ),则再次为mSelectionArgs[0]分配""

所以问题是,当它已经初始化为空字符串时,为什么将它设置为空字符串?

/*
 * This defines a one-element String array to contain the selection argument.
 */
String[] mSelectionArgs = {""};

// Gets a word from the UI
mSearchString = mSearchWord.getText().toString();

// Remember to insert code here to check for invalid or malicious input.

// If the word is the empty string, gets everything
if (TextUtils.isEmpty(mSearchString)) {
    // Setting the selection clause to null will return all words
    mSelectionClause = null;
    mSelectionArgs[0] = "";

} else {
    // Constructs a selection clause that matches the word that the user entered.
    mSelectionClause = UserDictionary.Words.WORD + " = ?";

    // Moves the user's input string to the selection arguments.
    mSelectionArgs[0] = mSearchString;

}
...

我喜欢它,因为它是对称的

if something
    var = x
else
    var = y

很清楚每个条件下的var是什么,而不需要返回并访问其初始值。

除了额外的清晰度和代码可读性之外,如另一个答案中所述,这种编码风格使得更容易出错的代码更容易维护。

这样,如果更改了mSelectionArgs的初始值,或者添加了在执行if-else块之前覆盖此值的新代码,则此块的代码仍将正确执行。 如果没有这种“基本”的分配,如上所述的变化可能会导致一个很难追查的错误。

作为旁注:

这个特定的代码片段不是那么好(是的,我知道它来自Android Developers网站......) - 如果你将null作为selection参数传递给query() ,那么最好还将null作为selectionArgs参数传递。 我将此示例修改为类似的内容(将selectionselectionArgs都设置为null):

// Gets a word from the UI

mSearchString = mSearchWord.getText().toString();

// Remember to insert code here to check for invalid or malicious input.

String[] mSelectionArgs = null;

// If the word is the empty string, gets everything
if (TextUtils.isEmpty(mSearchString)) {
    // Setting the selection clause to null will return all words
    mSelectionClause = null;
    mSelectionArgs = null;

} else {
    // Constructs a selection clause that matches the word that the user entered.
    mSelectionClause = UserDictionary.Words.WORD + " = ?";

    // Moves the user's input string to the selection arguments.
    mSelectionArgs = new String[] {mSearchString};

}

编辑:为什么上面的代码片段比原来的更好?

将null作为selection传递并且将非空值作为selectionArgs传递并不是错误。 此数组将传递给您正在寻址的特定ContentProvider ,并且根本不应该使用,因为selection不包含任何内容? 占位符。 违反此假设的任何ContentProvider都是错误的。 虽然不是错误,但它看起来很奇怪 - 为什么你传递一个应该被忽略的对象? 这也有性能成本(如果ContentProvider在不同的进程中运行,则会更高),这与传递的对象的大小成比例。

编辑2:为什么上面的代码片段比原来的代码片段好多了? 事实证明,我上面所说的可能会产生误导。 我找到了困难的方法:

 Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 3 because the index is out of range.  The statement has 1 parameters.
        at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
        at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
        at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)

抛出上面的异常是因为我试图传递selectionArgs ,其中包含的元素数多于? 占位符在selection

来自SQLiteProgram.java这两个方法是对这个异常的“责备”:

public void bindAllArgsAsStrings(String[] bindArgs) {
    if (bindArgs != null) {
        for (int i = bindArgs.length; i != 0; i--) {
            bindString(i, bindArgs[i - 1]);
        }
    }
}

private void bind(int index, Object value) {
    if (index < 1 || index > mNumParameters) {
        throw new IllegalArgumentException("Cannot bind argument at index "
                + index + " because the index is out of range.  "
                + "The statement has " + mNumParameters + " parameters.");
    }
    mBindArgs[index - 1] = value;
}

现在,当我发现这种行为时,我认为Android开发者网站的代码示例不仅效率低下,而且完全是废话!

底线:如果你通过nullselection ,通过nullselectionArgs为好。 如果selection不为null并包含? 占位符 - 确保selectionArgs数组的长度等于? 占位符在selection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM