5 newbie mistakes when creating your first Android apps and what to do with them
In the early stages of learning how to develop Android apps, beginners face many challenges. They can be easily solved by a professional, but often present a difficult task for a beginner.
It also often happens that newbies unknowingly form the wrong approach to writing program code and, without knowing it, complicate their work in the future.
An added complication is that even the simplest Android apps are complex programs involving hundreds of files and lines of code. Running them the first time without errors is not easy.
Vladimir Anisimov, a practicing Android developer with more than 4 years of experience in native and multi-platform development, has analyzed 5 common beginners’ mistakes from his experience and gave life hacks to help solve them.
Applying them, a novice developer will be able to greatly facilitate his training at first and, in the end, become a high-class specialist with decent wages.
Mistake # 1. Look for an error in the code yourself and try to fix it manually if the project does not start
Imagine that you go into the code of your project and see something like this:
All code is highlighted in red and the application does not start.
My first thought is to immediately check the code, look for the error, and try to fix it manually. However, such an approach at the initial stages and without experience in development can lead to new errors.
Instead, it’s better to do the following:
1) Find a line with a description of the error
Typing in the screenshot below is called a log. It already contains information about the error, you just need to find it.
The keyword here is Exception (but maybe Error), the error description is next to it. It needs to be copied and pasted into Google search engine.
The most common errors have already been sorted out on the forums and sites for developers. You will most likely be able to find a solution to your problem.
Also, your development environment can prompt a specific place in the program where the error occurred. These are the links highlighted in blue:
Seeing a place in the code with an error and knowing the description, it is much easier to fix it.
If this error was created by you as a developer (for example, a typo in the code), you must fix it exclusively by hand.
If the error is within the system (for example, the required library with data is not installed), then it must be eliminated in accordance with the recommendations in the log.
2) Try to get rid of the error with 4 buttons in Android Studio that many newbies don’t know about
Try pressing them if you need to fix the error, but you do not understand what to do:
It’s in the Build tab:
This button allows you to rebuild the project, fixing errors.
Sync project with gradle files
This button is in the File tab:
It is logical that if there is no connection between the project files and gradle files (those that collect parts of the project into a single system), the application will not work. The Sync project with gradle files button solves this problem.
This button clears the project of temporary build files. This is useful when you change something in your code, but you don’t see the change when you restart the project.
If the error persists after pressing this button, you can go back to the previous two and press them again.
The Clean project button is located on the Build tab:
This button is the most radical way to deal with errors. It is worth pressing it when the project does not start, Clean Project does not work and you do not understand the essence of the error either.
Invalidate caches cleans up temporary files already by Android Studio itself.
It’s in the File tab:
It should be noted that these tips are aimed at beginners, more experienced developers know what and when to use.
Mistake # 2. Incorrect variable names
This error is probably encountered by 80% of novice developers. It is not critical, but it makes the code difficult and unreadable.
Here’s how it looks like in an example:
What’s the difficulty with variable names like this?
Since school, we used to call variables a, b, c. However, in programming, it is undesirable to do this, since after a while it will be difficult to figure out which letter means what.
This is the key point: the code must be understandable not only by the computer, but also by the person.
If you name your variables this way, you might remember what they mean at design time. However, when you come back to this code after a month, you may forget what you mean. Especially if the code is complex and consists of tens or hundreds of lines.
You will need to find where the variables a, b, c were introduced, and restore the logic in your head.
And if your code is read by another person, it will take even longer for them to understand it.
Sometimes a developer tries to save this situation by using comments on variables:
However, you still have to return to the comments. In addition, they make the code heavier, because now you have to read the code itself and the comments to it.
You need to name the variables right away so that they explain themselves:
In this case, the name of the variable – money – immediately gives an approximate understanding of what it is about. And ideally, you need to clarify what kind of money is, for example, by naming the variable like this: moneyOnBalance.
Let’s take a more complicated example:
This is a code for calculating the balance on the account when buying an item with a value of p and a discount d. Bal – the amount of funds on the balance.
In this case, the variables are given alphabetic names – p, d. The name bal looks clearer and more readable, but it is still not ideal.
Try to understand the essence of the formula. Have you tried it? How many times have you run your eyes from a letter in a formula to an explanation of what it means?
And this is how this code should be styled:
Now the code can be read almost like a book and there is no need to spend time and effort to define what this or that letter means.
Mistake # 3. Excessive logic in the code
Novice developers often try to write everything in the code to the maximum, set variables that can be dispensed with. These are superfluous elements that only complicate the code.
For example, a developer sets 4 separate variables and then uses them in code:
The creation of separate variables can be justified – in the event that these variables will be used in the code many times further. Then this approach will really simplify the code and save the developer time. But in a specific fragment, each variable is used only once in the code.
Also, a mistake was made in the code with the names of variables (see bug # 2). What does message1 tell you? What is 1? And how is it different from message2?
How can you simplify this code and remove all errors:
Immediately set the values that should be returned under certain conditions without introducing variables.
The code immediately became 4 lines shorter, it is easier to read and understand.
Mistake # 4. Focus on abstract assignments
When you study programming from books, video courses, or even with a teacher and already know certain functions, you may be given an abstract assignment to practice them, which is most likely not useful for work.
For example: calculate the factorial of a number, solve a quadratic equation, try all options for sorting data.
These tasks develop algorithmic thinking well and can be useful in the initial stages, but you should not get carried away with them. They will lead you to the fact that you will think well with algorithms, but at the same time you will be able to write only simple and “mathematized” programs.
It is better to start performing tasks related to real life and the work of a programmer as early as possible: for example, develop a computer for a traffic light or a program for processing purchases.
Mistake # 5. Neglecting Debug Mode
If a program you have developed starts, but does not return the values you need (and which are correct), do not rush to look for the error manually. There is a Debug mode for this.
To run the program in it, instead of the usual start button, press the beetle-shaped button next to it:
In this mode, you will be able to see what happens when the program is launched in real time.
In this mode, you can stop the program at any place, while the current value of the variables at the moment is displayed on the screen:
If you can see that the variables have incorrect values, we check the code in a specific line, experiment with the entered elements until we get the desired value of the variable.
This function allows you not to check all the code (as beginners often do) and not to come up with “crutches”, but to check specifically those places in which you suspect an error.
What kind of error it is and what to do with it will need to be determined on your own (as opposed to a critical error due to which the application does not start), however, thanks to the Debug mode, you will have an understanding at what moment the application is running and in which part of the code it is appears and how to fix it.
Contact Vladimir Anisimov in Telegram for questions on mobile development: https://t.me/wovilon
Vladimir’s LinkedIn profile for communication: https://www.linkedin.com/in/vladimir-anisimov-849017135/
Vladimir’s Facebook profile for communication and questions on mobile development: https://www.facebook.com/profile.php?id=100018492451691