2

I inherited the project from a very experienced Java developer. The project is Android app and the guy who coded is is very experienced in coding regular Java apps. He introduced some file structures that I haven't come across before (I haven't coded Java apps outside Android area). I can follow the code with no problem and I actually have finished a few add-ons so far, but now I would like to know more of why he used such approach since it may help me myself write a better code. On the other hand, if his approach is not suitable for Android apps, I would like to know that as well so I don't use such structures.

I will divide all questions into points.

  1. When dealing with a database, he made 3 layers: entities, data_access and controllers. This is a kind of MVC structure. Is it advised to use it in Android apps?

  2. In controller part, he made the following file structure:

controller/
      async/
            AsyncAddObject.java
            AsyncExecutorAdd.java
            AsyncExecutorGetAll.java
          Entity1Controller.java
          Entity2Controller.java
          Entity3Controller.java
          IController.java

All async files are the generics.

Is this structure familiar to you? I remember executors used in Java, but have never used such approach in Android development. If this is a proper way, what is Executor used for?

What about IController.java - something usual in regular Java coding?

+++++++++
Then when we want to add some entry into database, he uses the following code

Entity1 ent = new Entity1();
ent.setSomething(someParameter);

AsyncAddObject<Entity1> obj = new AsyncAddObject<Entity1>();
obj.setController(mEntity1Controller);
obj.setObj(ent);
new AsyncExecutorAdd<Entity1>().execute(obj);

Does this look confusing to you? I usually call some database method and pass object to it. For example, I would do it like this

Entity1 ent = new Entity1();
ent.setSomething(someParameter);

MyDatabaseClass.addEntity1(ent); //just an example, adding to database will be done on non-Main thread. 

This is it. I apologize for a long post.

2
  • 2
    I am not qualified to comment on best practices for Android development, but the class names Entity1...3 are not descriptive enough or they demonstrate code that is not extensible for new Entity types in the future. Further the naming pattern ISomething for interfaces is common for .NET applications but not a typical naming convention for Java apps. I hope somebody with more Android experience can directly answer your question.
    – maple_shaft
    Commented Aug 16, 2013 at 9:20
  • 1
    @maple_shaft Names Entity1...3 are not real names in the project. I used them to hide the real code. Entities in this project are used as a model classes. I usually make model directory and put model classes in it.
    – deviDave
    Commented Aug 16, 2013 at 9:24

1 Answer 1

3

On Android events are triggered on the main UI thread, but you can not do any work on the main UI thread.

What the developer might be doing is sending the task of addObject to another thread so that the main UI thread can continue.

Looks like he has created a few classes to make these kinds of tasks easy to do. This is good practice, but I am making an assumption. Without seeing the code of these files we won't know for sure.

In your example;

MyDatabaseClass.addEntity1(ent);

If that is writing to a SQLite database on the Android device, then there is the risk that the database is stored on an SD card. SD cards are a bottleneck for performance on mobile devices, and this will block the main UI thread. You should not be doing any time consuming tasks on that thread.

I'm not even sure if Android will let you do it. There are some cases where the SDK will throw an exception if you attempt to do certain operations in the main thread.

7
  • Yes, that is true. He extended AsyncTask<>, that is not the issue. I am more concerned about naming practice, about executors since I have never seen such terms before. Also take a look at the last part of the question - his way and my current way. His way is better?
    – deviDave
    Commented Aug 16, 2013 at 9:26
  • Hey main UI and threading is not an issue here. I am experienced Android developer, I know that :). I use AsyncTask almost all the time. The fact is that he's using 10 lines and I am using 3 lines of code for the same thing.
    – deviDave
    Commented Aug 16, 2013 at 9:27
  • 1
    AsyncAddObject<Entity1> is the subject. AsyncExecutorAdd<Entity1> is the worker. Those names are perfectly understandable to me.
    – Reactgular
    Commented Aug 16, 2013 at 9:28
  • What is the purpose of executor?
    – deviDave
    Commented Aug 16, 2013 at 9:29
  • @deviDave well the question didn't read like that. Obviously fewer lines of code is better.
    – Reactgular
    Commented Aug 16, 2013 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.