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?
- No architecture at all (Nearly everything is in the Activity)
- using var not val for variables
- a lot of useless comments (e.g. sets the text to the textview or return the number of the items in the list)
- using findViewById() method (better: enable DataBinding or ViewBinding)
- No usage of the Strings.xml (only hardcoded strings)
- Manual json parsing (better use Gson/kotlinX.Serialization/Moshi)
- Creation Adapter/Layout manager every time a call happens/user search something
- Code format could be improved (remove useless empty lines)
CodePudding user response:
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 -
- MVVM in Android by G&G
- Modern Android App Architecture by Google
- Sample App with MVVM architecture
- How to consume API with Retrofit and GSON.
CodePudding user response:
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
CodePudding user response:
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.
