Question

I'm working on an "perfect" Android CRUD, with as many options as possible.

Its purpose is to be opensource and shared.

So, I'm trying to have a nice and indented, readable code, but I'm having the following problem.

That looks ugly :

public Where<Item, Integer> getWhereSearchOnNameLight(String searchName) {
    try {
        return itemsDao.queryBuilder().selectColumns(Item.COLUMN_ID,
                                                     Item.COLUMN_NAME,
                                                     Item.COLUMN_CATEGORY_ID)
                                      .where()
                                      .like(Item.COLUMN_NAME, "%" + searchName + "%");
    } catch (SQLException e) { e.printStackTrace(); }
    return null;
}

This looks pretty :

public Where<Item, Integer> getWhereSearchOnNameLight(String searchName) {
    try {
        return itemsDao.queryBuilder().selectColumns(Item.COLUMN_ID,
                                                     Item.COLUMN_NAME,
                                      .where()       Item.COLUMN_CATEGORY_ID)
                                      .like(Item.COLUMN_NAME, "%" + searchName + "%");
    } catch (SQLException e) { e.printStackTrace(); }
    return null;
}

But the compiler is associating the .where() with the Item.COLUMN_CATEGORY_ID), just because they are on the same line.

I'd like to make him understand that it's only for display purpose, but I can't.

Any ideas?

Was it helpful?

Solution

In java, you can't do that. Maybe you can change their position.
It looks pretty too.

public Where<Item, Integer> getWhereSearchOnNameLight(String searchName) {
    try {
        return itemsDao.queryBuilder().where()
                                      .like(Item.COLUMN_NAME, "%" + searchName + "%")
                                      .selectColumns(Item.COLUMN_ID,
                                                     Item.COLUMN_NAME,
                                                     Item.COLUMN_CATEGORY_ID);
    } catch (SQLException e) { e.printStackTrace(); }
    return null;
}

OTHER TIPS

Compilers strip and ignore all whitespace so you need to respect the correct ordering of elements (i.e., have the where() after the ) of selectColumns).

There's really not much you can do, but try to reorganize it in a way that reads best. Many programmers will pick and choose their style; my advice is to pick a style and stick to it.

For instance, a hit of the return key and a few keystrokes in IntelliJ 13 gets me this:

public Where<Item, Integer> getWhereSearchOnNameLight(String searchName) {
    try {
        return itemsDao.queryBuilder()
                .selectColumns(Item.COLUMN_ID,
                        Item.COLUMN_NAME,
                        Item.COLUMN_CATEGORY_ID)
                .where()
                .like(Item.COLUMN_NAME, "%" + searchName + "%");
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

And that is my preferred style, where applicable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top