After making more than 58k posts in the forum, I decided that it is a good time to build a list of tips and common mistakes. 12 points, in no particular order:
- Separate code from data. Putting the data directly into the code makes your program unreadable and less maintainable.
There are many simple ways to deal with data. For example you can add a text file to the Files tab and read it to a List with:
Dim data As List = File.ReadList(File.DirAssets, "SomeFile.txt")
- Don't Repeat Yourself (DRY principle). If you find yourself coping and pasting the same code snippet multiple times and then making a small change then it is a good idea to stop and try to find a more elegant solution.
Repeated code is difficult to maintain and update. The Sender keyword can help in many cases (old and still relevant tutorial: Tick-Tack-Toe: working with arrays of views).
- Map collection. All developers should know how to use a Map collection. This is by far the most useful collection. Tutorial: https://www.b4x.com/android/forum/threads/map-collection-the-most-useful-collection.60304/
- New technologies and features. Don't be afraid to learn new things. As developers we always need to learn new things. Everything is evolving whether we want it or not. I will give MQTT as a good example. I wasn't familiar with this technology. When I started learning about it I was a amazed to see how easy and powerful this solution.
B4X specific features that all developers should be aware of:
- Smart strings literal: https://www.b4x.com/android/forum/threads/50135/#content
- For Each iterator: https://www.b4x.com/android/forum/threads/loops.57877/
- Classes: https://www.b4x.com/android/forum/threads/18626/#content
- Logs. You should monitor the logs while your app is running. Especially if there is any error. If you are unable to see the logs for some reason then take the time to solve it. Specifically with B4A-Bridge the logs will only appear in Debug mode. If you encounter an issue that only happens in release mode then you need to switch to usb debug mode.
- Avoid calling DoEvents. DoEvents interferes with the internal message queue. It can cause unexpected issues. There are very few cases where it is required. This was not the case when B4A v1.0 was released. Since then the libraries have evolved and now offer better solutions. For example if the database operations are too slow (and you are correctly using transactions) then you should switch to the asynchronous methods.
You can use the new Sleep method instead: https://www.b4x.com/android/forum/threads/79578/#content
- Strings are made of characters not bytes. Don't try to store raw bytes as strings. It doesn't work. Use arrays of bytes instead. The proper way to convert bytes to strings is with base 64 encoding or ByteConverter.HexFromBytes.
- (B4A) Use services, especially the Starter service. Services are simpler than Activities. They are not paused and are almost always accessible.
Three general rules about global variables:
1. All non-UI related variables should be declared in Process_Globals.
2. Public (process_global) variables should be declared and set / initialized in Service_Create of the Starter service.
3. Activity process globals should only be initialized if FirstTime is true.
This is only relevant to B4A. It is simpler in B4J and B4i as there is no special life cycle and the modules are never paused.
- UI Layouts. B4X provides several tools to help you implement flexible layouts that adopt to all screen sizes. The main tools are: anchors and designer script. Avoid adding multiple variants (two are fine). Variants were introduced in v1.00, before the other features. Variants are difficult to maintain and can be replaced with scripts.
Anchors are very simple and powerful.
Don't overuse percentage units (unless you are building a game).
- B4J as a backend solution. B4A, B4i, B4J share the same language, same concepts and mostly the same APIs. It is also simple to exchange data between the different platforms with B4XSerializator.
It is easy to implement powerful server solutions with B4J. Especially when the clients are implemented with B4A, B4i or B4J.
- Search. Use the forum search feature. You can filter results by adding the platform(b4a for example) to the query or by clicking on one of the filters in the results page.
Most of the questions asked in the forum can be solved with a few searches.
- Notepad++. At one point or another we need to work with text files. I highly recommend all developers to use a good text editor that shows the encoding, the end of line characters and other important features. https://notepad-plus-plus.org/