-1

I am really new to android app development using Kotlin. I got a coding challenge to develop an app to search for tv shows using the TV MAZE API. I learned some Kotlin basics and developed the app. I got the following feedback for my coding. Can anybody help me to understand the following points and how to improve my current code with respect to following points?

  1. No architecture at all (Nearly everything is in the Activity)
  2. using var not val for variables
  3. a lot of useless comments (e.g. sets the text to the textview or return the number of the items in the list)
  4. using findViewById() method (better: enable DataBinding or ViewBinding)
  5. No usage of the Strings.xml (only hardcoded strings)
  6. Manual json parsing (better use Gson/kotlinX.Serialization/Moshi)
  7. Creation Adapter/Layout manager every time a call happens/user search something
  8. Code format could be improved (remove useless empty lines)

Here is the code link

geethuth
  • 91
  • 1
  • 2
  • 10
  • 3
    Unfortunatelly the question is to broad for StackOverflow. If your project was work or study related, you should ask your mentor / teacher for more guidance. Here is some context for your feedback: 1) Read this guide on android app architecture: https://developer.android.com/jetpack/guide 2) Just use `val` whenever possible. 4) Check out this guide https://developer.android.com/topic/libraries/view-binding 5) Your UI text should be placed in XML files instead of code. This makes it easier to translate the app: https://developer.android.com/training/basics/firstapp/building-ui#strings – Alexander Hoffmann Jan 26 '22 at 08:42
  • 2
    6) Parsing JSON manually is error prone. You should use libraries which handle this. Check out the mentioned libraries. 8) Code should be formatted to make it easier to read. Meaning, you should follow a style guide and use the auto formatter feature in Android Studio. Check out the style guide: https://developer.android.com/kotlin/style-guide – Alexander Hoffmann Jan 26 '22 at 08:46

3 Answers3

3

It is impossible to answer the questions in StackOverflow. But for your reference, I can provide some resources and documentation which you can follow to improve your code -

  1. MVVM in Android by G&G
  2. Modern Android App Architecture by Google
  3. Sample App with MVVM architecture
  4. How to consume API with Retrofit and GSON.
Naimul Kabir
  • 434
  • 6
  • 13
1

1 . No architecture at all

You should apply MVVM or MVP architecture for Your app.

2. using var not val for variables

You should understand the difference between mutable immutable variables, and use val for immutable

3. a lot of useless comments (e.g. sets the text to the textview or return the number of the items in the list)

You should first add the comment for the public method in interfaces, add comments to write class describe like JavaDoc https://stackoverflow.com/a/11764075/6352712

You should add comments to explain logic if logic looks not clear https://www.baeldung.com/cs/clean-code-comments#2-explaining-unclear-code

If code is clear and understandable You mustn't comment on it

4.using findViewById() method (better: enable DataBinding or ViewBinding)

use Kotlin static instead viewFindById https://medium.com/androiddevelopers/use-view-binding-to-replace-findviewbyid-c83942471fc

5. No usage of the Strings.xml (only hardcoded strings)

Please replace your hardcoded message string with adding strings to your android resources https://stackoverflow.com/a/7493367/6352712

6.Manual json parsing (better use Gson/kotlinX.Serialization/Moshi)

You should add Moshi library to your project for parsing https://github.com/square/moshi

8 Code format could be improved (remove useless empty lines)

When You coding You should each time formating your code with hot key in android studio https://stackoverflow.com/a/16580200/6352712

Serg Burlaka
  • 2,351
  • 24
  • 35
1

1- Let's make it clear for you, it's about separation of concern!
Imagine you have a project, You are working on it, and this project gets larger and larger, if you want to debug your application or maintain it later, it's very difficult. when you use an app architecture, separate each layer of your application, for example, you have a View layer that you working on your UI and no business logic of your app exist in this layer, so the code is concise and clear to read and maintain and debugging, you can write test better and some other benefits.
2- When you need a variable that you want to change later, you must use a var keyword, otherwise there is no need to declare a variable as a var.
3- comments use for clearing some codes for other developers or yourself to understand these codes better, Avoiding unusable comments in your project.
4- findViewById() method, is most costly, however if you use viewBinding android use this method behind the scene. benefit to use viewBinding is all of your views is initialized and there is no concern about NPE(Null Pointer Exception).
5- When you want to release your app for users all around the world for some language you have to find hardcoded strings and convert it to that country language, do you agree that this is a complicated procedure? :)
6- Always use Parsers or ORM in your project, because if you parse json by hand it may produce some exception in your app by using wrong json key.
7- Inflating layout in android is most costly, you must update your data and just update your adapter.
8- always be update, and improve your code. :)
read Android Developer Site it can help you, see some course in youtube and books are best friends for us, don't forget to read more books. :), happy coding.

OneDev
  • 557
  • 3
  • 14