Just curious...

DPaul

Active Member
Licensed User
Longtime User
OK, maybe here another short fav of mine that i use a lot.

I have an int X that can only be 1 or 2. It switches all the time. (i also use X as an index to show string(1) or string(2))
So:
if x = 1 then x = 2 else x = 1
but i use:
x = abs(x-3)
Or is somebody about to tell me that this can be done shorter ? :-(

DPaul
 

Star-Dust

Expert
Licensed User
Longtime User
more short
B4X:
x=3-x
 

Star-Dust

Expert
Licensed User
Longtime User
In principle when a variable has to oscillate on two numeric values the formula would be this

B4X:
ValueNew=(Value1+Value2)-ValueActual

in your case
B4X:
X=(1+2)-X
 

DPaul

Active Member
Licensed User
Longtime User
In principle when a variable has to oscillate on two numeric values the formula would be this

B4X:
ValueNew=(Value1+Value2)-ValueActual

in your case
B4X:
X=(1+2)-X

Very interesting!
Works with any 2 positive numbers ...
Works with any 2 negative numbers ...
And 1 pos and 1 neg also !

OK, since were on this route, i also have silly looking lines like this: (alternating button text when pressed)
If but.text = "GO" then but.text = "STOP" else but.text = "GO"

In tune with previous posts, i've thought about making something like string(1) = "GO" , string(2) = "STOP"
This will work if you introduce an index variable, but it won't be shorter!

Anything i should now?
DPaul
 

Star-Dust

Expert
Licensed User
Longtime User
For string this is the method

B4X:
' Button.Text initial value "Stop"
Sub Button_Click
Button.Text="StopGo".Replace(Button.Text,"")
End Sub
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
But these are basic functions that teach you at school. At least in Italy, high school computing students were also told how to reduce the code to the maximum.
Also because in my time when you got 128kb of ram was so much
 

DPaul

Active Member
Licensed User
Longtime User
For string this is the method

B4X:
' Button.Text initial value "Stop"
Sub Button_Click
Button.Test="StopGo".Replace(Button.Test,"")
End Sub

Very nice (-> i think you mean button.te X t) !

That is what i meant with my first post, 45 years ago, when punching 80-column cards,
there was obviously nothing like this. So young boys and girls are tought new tricks :)
I've spent all morning changing my code :)

DPaul
 

Star-Dust

Expert
Licensed User
Longtime User
Changes the thread title in Amarcord
 

Star-Dust

Expert
Licensed User
Longtime User
The difference between the programming of 20 years ago is that of today can be explained by a simple example.

Imagine two tailors who must make the same dress.

The first tailor does not want to use unnecessary fabrics therefore marks with various chalk pieces to be cut out so that they combine with each other. So there will be less waste and less cloth will be consumed. This takes a lot more time to realize. Obviously there must be no mistakes in cutting because there is not enough cloth to cut the same piece, so cuts will be made more slowly and with more attention.

The second tailor wants to go faster. So buy more fabric in case of hurrying mistakes a few cut points as well each piece of fabric gets fewer pieces because it must go in a hurry. But surely this entails a greater expense of raw material. But less production times also more mistake, more cloth, more means you can correct any mistakes in cuts that may be going in a hurry.

20 years ago there were less powerful machines, less fast processors, less powerful hardware, ram memories and smaller hard drives. Developing an app took more time because the algorithm is studying how to reduce size and increase speed.

Today if a too heavy application I suggest you change the hardware ... And every software or operating system update requires ever more hardware resources
 
Last edited:

ilan

Expert
Licensed User
Longtime User
Or is somebody about to tell me that this can be done shorter ? :-(

this is a much better way:

B4X:
If x = 2 And x > 1 Or x = 2 And x < 3 And x > 1 Or x * 2 Mod 2 <> 1 Then x = x Mod 2 + 1
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
this is a much better way:

B4X:
If x = 2 And x > 1 Or x = 2 And x < 3 And x > 1 Or x * 2 Mod 2 <> 1 Then x = x Mod 2 + 1
o_O
 

Star-Dust

Expert
Licensed User
Longtime User
Make your code unreadable as possible if someone tries to steal snipped from it ;)
Do you think there was someone interested in stealing this code?
 

Star-Dust

Expert
Licensed User
Longtime User
is wrong. Try this:

B4X:
If (x=2)And(x>1 And x<3)And(x<>1)Or((x*2) Mod 2<>1) Then x=(x Mod 2)+2 ELSE X=sqrt(X*X*9)/3
 
Top