Android Question Conditional and unconditional jumps in B4X

DOM85

Active Member
Licensed User
Longtime User
Hello,
All processors have conditional and unconditional branch instructions (Je, Jne, Jl, Jmp, ...), and operating systems use them a lot.
"Loops, Do..." are a programming tool, conditional and unconditional branches too.
Often, loops require a lot of flags to exit, when a simple Goto or jump would simplify both processing and understanding during maintenance.

Is implementing these instructions on B4X at least inside a Sub function really too difficult?

Thank you.
 

QSerg

Member
........optimized or otherwise adapted to an object-oriented approach.....
This statement is VERY valid - well so often we need to jump over OO loop optimizing and adapting out code in situations where OO approach is completely and totally useless and result can be achieved much faster and by far more efficient without OO rubbish. And B4A as Java related language is very prone to such problem.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
so often we need to jump over OO loop optimizing and adapting out code in situations where OO approach is completely and totally useless and result can be achieved much faster and by far more efficient without OO rubbish.
interesting: can you provide a sample?
 
Upvote 0

BlueVision

Well-Known Member
Licensed User
Longtime User
well so often we need to jump over OO loop optimizing and adapting out code in situations where OO approach is completely and totally useless and result can be achieved much faster and by far more efficient without OO rubbish.
I understand what you are suggesting. But do you really need it? A programme will not run twice as fast because of this.
I've been programming with BASIC since the mid-80s on a wide variety of systems. To be honest, I've never really needed the GOTO command. Of course it was a convenient solution, especially with interpreter BASIC and above all when you wanted to test something quickly with infinite looping.
But even with ‘better’ BASIC dialects (Turbo-BASIC) I always tried to bring a certain structure into the programme. That worked quite well with GOSUB and went in the direction of procedure-orientated programming.
Of course, it is also somewhat due to the envy of other programming languages that BASIC never had a good reputation because of the GOTO command. So they probably started to eliminate GOTO.
Honestly, the language is still the easiest programming language to learn, with or without GOTO. Do I miss GOTO? Not really. Then why should it be reintroduced? As far as I know, Visual BASIC no longer had GOTO. And to my delight, BASIC has become very popular. As VB or VBS within Excel and also here with B4X. It is still BASIC. I would never try to learn C nowadays. We have Compiler, Designer, structural programming and countless libraries we can use. So why we need GOTO? Interested in an example too...
 
Upvote 0

QSerg

Member
...A programme will not run twice as fast because of this....
I do not understand what you referring to GOTO or OO? having or not having GOTO does not impact speed of final code nor restrict logic anyhow. It is mere convenient tool that allow from time to time create more elegant code. This is about it. OO and especially Java with all those Virtual Java Machines severely slow final code and we afloat only because we have enormously powerful processors multiplied on fast and practically unlimited memory nowadays. Even VB with stupid conversion of everything (even integer!!!!) before multiplication and division to ugly Variant (that never meant to be exists, IMHO) and then converting it back to whatever it was another real killer of speed of final code.
 
Upvote 0

QSerg

Member
interesting: can you provide a sample?
I have to dig my code to give you practical example and I am too lazy to do so. One notorious example that I remember I need to manipulate with string and string method was unable to do what I want, so I have to declare another structure that I use for nothing but just achieve requested functionality. Incredibly ugly and incredibly non-obvious, much more so than using GOTO. Plus continuous requirement to convert strings back and forth if you need to use them outside B4A. Ugly and unproductive in both writing code and speed of execution.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
OO and especially Java with all those Virtual Java Machines severely slow final code
This is purely wrong. Java and the JVM are extremely fast. The compiler optimizes many things and all kinds of preoptimizations are simply a waste of time.

One notorious example that I remember I need to manipulate with string and string method was unable to do what I want, so I have to declare another structure that I use for nothing but just achieve requested functionality. Incredibly ugly and incredibly non-obvious, much more so than using GOTO.
Hard to say anything without an example.

Plus continuous requirement to convert strings back and forth if you need to use them outside B4A
???
 
Upvote 0

BlueVision

Well-Known Member
Licensed User
Longtime User
Java with all those Virtual Java Machines severely slow final code and we afloat only because we have enormously powerful processors multiplied on fast and practically unlimited memory nowadays
The compiled output is very fast, believe me.
I get your point, but is there a true alternative to a higher language with an optimized compiler? Only alternative for speeding up would be using direct machine code for writing a programme. I remember very far back in the 80's programming in assembler for MOS 6502 and Zilog Z80. Honestly, it was not worth doing this. Who is in need of that speed?

A well-written programme hardly makes any difference to a pure machine programme in terms of processing speed after compilation. As a rule, the programme waits for input from a user at some point anyway. Of course, there are also applications that are very time-critical (weather forecasting and climate research, the calculation of corresponding models). But these are very specialised and there are more specialised high-level languages and more sophisticated compilers for them. The processing speed of today's processors is so high that the effort of processor-orientated programming is simply not worth it. Programming costs a lot of money and trying to write a complex programme in today's machine language is simply futile and uneconomical. Who, for example, is supposed to maintain this code and adapt it to constantly changing conditions? In my opinion, it is a hopeless battle. In addition, there are a large number of sophisticated processor architectures that are constantly changing, and it is nowhere near as easy as it used to be to write the code for at least one processor family.
Many programming principles (such as lazy loading) lead to further increases in speed and make sense.
If you use such techniques, I personally see no reason to go back to the roots. That would no longer work in today's fast-moving world.

Of course you can also implement new commands in an established high-level language, why not if it makes sense. But times, technology and programming techniques have changed. They have actually developed in an evolutionary way, unproductive stuff has fallen by the wayside and largely died out. A lot of money is at stake.
 
Upvote 0

QSerg

Member
This is purely wrong. Java and the JVM are extremely fast. The compiler optimizes many things and all kinds of preoptimizations are simply a waste of time.


Hard to say anything without an example.


???
Erel, with all respect there is no person in whole Universe that potentially could convince me that interpreter (JVM) will be as fast as direct optimized code. I am in this business from middle 80-s.

I will try to dig for an example.

Re: ???. I need to write an application that communicate to MS SQL from Android. I been looking for tools everywhere. All so called "solutions" plainly ugly, cumbersome, and restricted. So I finished with creation of my own service that communicate with Android application written in B4A (this is where I start to use B4A), send requests to SQL and return results to Android. Because of so called "strings" in B4A (obviously inherited from Java) not real string, but rather unnecessarily extensive complex structures while on other end they more or less string continuous conversion required. I never understand what was wrong with zero-terminated string that almost native to processor and I never understand what is wrong with functions for string manipulation and why I have to use ugly inconvenient and cumbersome methods. In B4A (read in Java) all string manipulation is nightmare comparing to VB6 where string manipulation very inconvenient comparing to C. I understand that "New generation choose Pepsi", but it does not mean that old trusted horse should be sent to abattoir. All OO and Java in particular forces one to study classes and jumping through the loop by using them instead of just write code. All above is no more than personal opinion.
 
Upvote 0

QSerg

Member
The compiled output is very fast, believe me.
By definition pseudo-code run by JVM cannot be faster (and even cannot have the same speed) as native code to processor. I was searching for converter Java to native code and I did not succeed - all of them restricted in this or that way. Believe me it seems to you fast enough purely because nowadays we have enormously fast hardware.
...Who is in need of that speed?
If it too fast for you you always can put few dummy cycles. But you cannot make it faster than processor's abilities.
A well-written programme hardly makes any difference to a pure machine programme in terms of processing speed after compilation.
It would be correct if you talking about true compiler with optimizer. And it would be incorrect for Java
As a rule, the programme waits for input from a user at some point anyway. Of course, there are also applications that are very time-critical (weather forecasting and climate research, the calculation of corresponding models). But these are very specialised and there are more specialised high-level languages and more sophisticated compilers for them. The processing speed of today's processors is so high that the effort of processor-orientated programming is simply not worth it. Programming costs a lot of money and trying to write a complex programme in today's machine language is simply futile and uneconomical. Who, for example, is supposed to maintain this code and adapt it to constantly changing conditions? In my opinion, it is a hopeless battle. In addition, there are a large number of sophisticated processor architectures that are constantly changing, and it is nowhere near as easy as it used to be to write the code for at least one processor family.
Come on! Firstly there is C that allow you to write fast code that almost directly translates into machine code. And secondly leave untouched program written in C and in Java and try to modify it after 10-15 years. Chances are that competent programmer in C will do it with easy while in Java you will face whole lot of new methods and structures and half of method in original program will be depricated and unknown to new generation of programmers. This is if it will work on "updated" JVM altogether.

Many programming principles (such as lazy loading) lead to further increases in speed and make sense.
If you use such techniques, I personally see no reason to go back to the roots. That would no longer work in today's fast-moving world.

Of course you can also implement new commands in an established high-level language, why not if it makes sense. But times, technology and programming techniques have changed. They have actually developed in an evolutionary way, unproductive stuff has fallen by the wayside and largely died out. A lot of money is at stake.
Disagree. More often then not it not dictated by "evolution" but rather by fashion and desire to create new business opportunities. Heck, VB6 is still alive and everyone excited about TwinBasic. C is still here and even Assembler (though for very special stuff). And yet Fortran is still here among scientific community. And how many new and "progressive" technologies sank to the bottom of Mariana Trench in last 3 decades? Java in my eyes has only one advantage - theoretical possibility to run everywhere. Theoretical because you cannot have universal classes that could work on absolutely any device. And who ever going to run my application written specifically for Android bar-code gun on Oracle Solaris??????????? It was born because of specific device and it will die once device disappears. No portability ever required for such applications.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Erel, with all respect there is no person in whole Universe that potentially could convince me that interpreter (JVM) will be as fast as direct optimized code.
JVM is not an interpreter. However more importantly, I'm fine with people having different opinions, even if I know that they are wrong :)
 
Upvote 0

BlueVision

Well-Known Member
Licensed User
Longtime User
I never understand what is wrong with functions for string manipulation and why I have to use ugly inconvenient and cumbersome methods. In B4A (read in Java) all string manipulation is nightmare comparing to VB6 where string manipulation very inconvenient comparing to C.
Well, some of your considerations are understandable, but simply cannot be economical. It's highly theoretical. True, but no way to make it work as you want. What is your problem with the string functions? It is precisely these functions that can be wonderfully replaced via the substring functions. You no longer need LEFT, MID, RIGHT, you can easily search for substrings or replace them. Only the syntax has changed.

I understand you. I just don't really understand the benefit compared to the effort you are putting in.
You're looking for a language for a programming solution, you want to write an application for it. But if you have such high requirements in terms of processing speed, but the application is only tailored to a very specific machine and is so time-critical, then B4A (B4X) may not be the right product for you. If every millisecond is important to you, you will only be left with programming in assembler. Regardless of the amount of data to be processed, the development will take an insane amount of time.
In my opinion, B4X-products are still the best compromise for your requirements. Ultimately, you can measure the runtimes only when you run the programmes against each other.
And when it comes to the implementation of programming principles for SQL: Here in the forum you have enough experienced developers and programme examples to help you. It won't cost you a cent.

I can understand your displeasure to a certain extent. As you say yourself, JAVA can run anywhere and is largely platform- and processor-independent. What other language can do that? Yes, programmes are huge and bloated these days, but that's not the fault of the programming language developers. The cause is the speed of technical evolution. The revolutionary concept of a processor with several cores and pipelining will probably be off the table again in a few years because something new will come along.
So, JAVA was developed to speed up programming on different platforms independent from the hardware architecture by using a runtime environment. And this is nothing else than a virtual machine. It can not be faster than assembler. That's right. But how fast is fast enough in your opinion?

IN GENERAL:
Erel made a masterpiece of software to strip JAVA further down to people like me for instance. Knowledge from good old BASIC-days, some assembler of nowadays outdated processors or forgotten programming languages like PASCAL. Here you step in and create after some hours a piece of software that works.
I see no reason to discredit B4A(B4X) because of this. Absolutely not.
My conclusion: It is pointless to think about such things. You lose far too much time and in the end it's all just theory you can't put into reality.
Let's agree to disagree.
 
Upvote 0

QSerg

Member
JVM is not an interpreter. However more importantly, I'm fine with people having different opinions, even if I know that they are wrong :)
This or that JVM is additional layer that consume computer resources that otherwise could be funneled to do main task. Again with modern super-fast hardware, multicore processors and real life considerations like awaiting for user input difference is so small that sometimes special methods required to measure it and from practical POV speed pretty much the same. However it does not mean that when using properly compiled machine code that can utilize modern hardware features program will run faster.
 
Upvote 0

QSerg

Member
...But how fast is fast enough in your opinion?...
Honestly I do not understand your post.

I never bashed B4A. Quite on the contrary I am very happy that I found it because it removes necessary to use ugly Java by great extend. Android is Java base product, so there is no way around if one need to program it and I agree with you that B4A is an excellent compromise.

From other hand all that rubbish about fast development in Java is no more than rubbish. Have a look through this forum - people very seldom if ever asking about B4X language, almost all question related to methods, libraries and so on. Thus instead of study hardware and related libraries/function I have to study Java's implementation of MFC. And unlike studying hardware (i.e. processor) and languages this is never ending quest with continuously changing and deprecated methods and properties. Where this Java make sense? In huge environment like massive web-site when you need to shift it from one OS to another. But how those so called "flexibility" helping in my particular case when program is written for particular device and for particular task? Is it makes developing such program faster? Nope. It is makes it run faster? Nope. Or because I know Java I can immediately change platform? Obviously not - I have to dig for classes suitable for given task exactly in the same way how I use to dig for libraries (or port assignation and software interruption) in the past. On top of this some stuff does not work in certain release of Android. All that "universalizm" is greatly exaggerated or even does not exists at all.

So how fast is enough? Remember that I am in business for more than 40 years and I started when each byte and processor cycle need to be counted. So in my eyes fast enough is when it cannot be any faster even if you cannot measure it by using your wrist watch. Even now I am re-utilizing space and optimizing cycles and after half an hour of mucking around I think WTF I am doing? Old habits dying hard. Or never die at all.
 
Upvote 0

QSerg

Member
This or that JVM is additional layer that consume computer resources that otherwise could be funneled to do main task. Again with modern super-fast hardware, multicore processors and real life considerations like awaiting for user input difference is so small that sometimes special methods required to measure it and from practical POV speed pretty much the same. However it does not mean that when using properly compiled machine code that can utilize modern hardware features program will run faster.
How I can edit my posts? Last sentence should be like this:

However when using properly compiled machine code that can utilize modern hardware features program will run faster.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
There are certain situations where I have found GOSUB/RETURN very useful and much more readable.
Specifically I have had some complicated routines with a lot of local variables where all or most of the branches have some common code.
B4X requires that I either duplicate all of that common code or make all of the local variables not local. Not very readable unless I create a separate module for just that Sub.
 
Upvote 0

QSerg

Member
There are certain situations where I have found GOSUB/RETURN very useful and much more readable.
Specifically I have had some complicated routines with a lot of local variables where all or most of the branches have some common code.
B4X requires that I either duplicate all of that common code or make all of the local variables not local. Not very readable unless I create a separate module for just that Sub.
This is another very valid point. Because of absent of pre-processor (or in-line functions) and absent of GOTO it is well so often one need to duplicate code everywhere with danger to forget changing all duplication when necessarily. Or otherwise create swamp of global variables that could be difficult to trace especially in massive project.
 
Upvote 0

QSerg

Member
or gather together all of the local variables into a custom type variable (or, at least, all the local variables used by the common code), and then pass that custom type variable as a parameter to the common code sub. Any changes made to the "local variables" inside the custom type variable, are seen by the caller when the common code sub returns.

Has advantage over "make all of the local variables not local" in that it makes the Sub re-entrant, ie each call to the sub with local variables, has its own set of local variables.

You can do likewise with Arrays, Lists and Maps: they all get passed by reference too.

Still a bit awkward, but not as awkward as making local variables global where they are at risk of being trashed by other invocations of the sub.
Sometimes aggregate numerous local variables that used for totally different reasons into one structure not practical at all. Plus everywhere outside C passing parameters by val or by reference is obscure and not straightforward at all to extend that ages ago I developed habits not to change anything that passed into sub ultimately treating all parameters as passed by value.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
@QSerg

You have used the word 'ugly' to describe code at least 8 times and 'fast' many times.

Wiki: Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making specific disciplined use of the structured control flow constructs of selection (if/then/else) and repetition (while and for), block structures, and subroutines.

Lucky for us who value "clarity, quality, and development time", CPUs are now superfast and memory is superlarge.
I have programmed in a context where speed and memory were very limited. I used machine language code to overcome the contraints of the era.
But very few readers of those programs would have any idea what how they worked. I thought they were clever, but I never thought they were beautiful.

All B4J and B4A code is compiled to Java Virtual Machine code (JVM). Over time the optimization of this code has evolved into a very sophisticated art.
JVM has branch instructions with targets expressed as offsets.

I don't like Java code, I find it ugly, as I do most other computer languages.
But I find B4X code beautiful. It is not perfect, but over time many improvements have been made, and they keep on coming.

To each person, what is beautiful is personal. By posting in this forum, you're questioning the aesthetic of all members.
We will defend what we like, that's why you getting more airtime time than you deserve. I suggest that you move on.
 
Upvote 0

DOM85

Active Member
Licensed User
Longtime User
Unconditional jump instructions like GOTO are considered deprecated for several reasons:

  1. Complexity of control flow: Using GOTO makes program control flow much more complex and less clear, leading to code that is difficult to read, understand, and maintain over time.
  2. Debugging difficulties: GOTO can make debugging harder since unconditional jumps can complicate tracking program execution in a linear fashion.
  3. Lack of structured programming: The use of GOTO violates the principles of structured programming, which promotes control flow constructs like if-else statements, loops, and subroutines to improve code readability and maintainability.
  4. Potential errors: Overuse of GOTO can easily lead to programming errors, such as infinite loops or situations where parts of the code are not executed as expected.
In modern programming, many languages have removed or significantly restricted the use of GOTO to encourage safer and more structured programming practices. However, in certain very specific and controlled cases, some languages still support similar constructs.

One of the most famous critics of GOTO was Edsger Dijkstra, a pioneer in computer science. In his famous 1968 article titled "Go To Statement Considered Harmful", published in the Communications of the ACM journal, Dijkstra strongly criticized the indiscriminate use of GOTO, emphasizing how it can compromise code clarity and debugging ease. This article had a significant impact on the widespread adoption of structured programming practices.

In summary, abandoning GOTO has been an important step toward writing clearer, safer, and more maintainable code in modern programming languages.
I see that everyone has its own idea about jumps. For me, in some cases, Jumps (goto) simplify greatly my code maintenance.
In addition main languages have it (C++ is an important language), and all the processors have it.
I don't understand why some people want to ban it !
Banning is not a solution !
 
Upvote 0
Top