-3

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?

gandalf3
  • 1,636
  • 4
  • 24
  • 40
  • Write clear and clean code. Have enough comments. Follow basic indentation. Nothing else is really needed. – PM 77-1 Jan 05 '14 at 04:47
  • 1
    Aside from not being legal Java syntax (nothing to do with Android, btw), why is the second piece of code any better than the first? To me the second version looks like `COLUMN_CATEGORY_ID` is supposed to be part of the WHERE clause, where clearly it is supposed to be one of the returned columns. It also doesn't look any prettier to me. – Ted Hopp Jan 05 '14 at 04:47
  • Trying to convince the compiler that invalid code is "prettier" probably isn't going to go well. – Brian Roach Jan 05 '14 at 04:50
  • PS, it's a builder, you can reorder the method calls. – Brian Roach Jan 05 '14 at 04:54
  • "perfect" is too much! – Phantômaxx Jan 05 '14 at 09:50

3 Answers3

0

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;
}
henry4343
  • 3,871
  • 5
  • 22
  • 30
0

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).

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
0

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.

Makoto
  • 104,088
  • 27
  • 192
  • 230