Just curious...

DPaul

Active Member
Licensed User
Longtime User
I had my first programming course about 45 years ago.
Yes!
Back then there were some "golden" rules.
As gold does not rust, I wonder if they still teach them to the young boys and girls.
e.g.
"The fewer lines of code, the better the program".
I catch myself rewriting subs, to see if i can do with a few lines less.

Does that make me a dino ?

DPaul
 

eps

Expert
Licensed User
Longtime User
It depends... I try to ensure that my code is as robust as possible - quite often people say 'oh that will never happen' - but almost as soon as you say it isn't possible it becomes a little more possible!

I also try to make my Apps data driven, so that I don't have to update the code.

and of course reuse! Making methods or subroutines so that I can simplify the code.

Of course fewer lines is good, but then it also needs to be readable and the speed of execution is important for me - quite often I've written 'ugly' code but it works so much more efficiently than a more elegant or tight solution. Mostly when working with loops.

Databases can be another oddity... sometimes they work better if you retrieve more data... :)
 

JordiCP

Expert
Licensed User
Longtime User
Does that make me a dino ?
Yes :D! (joking)

I think this made a lot of sense when code/data memory was a scarce resource (and one had to print the code with a dot matrix printer). It still makes sense to be able to synthesize, but only up to the common-sense limits

The golden rules for me are REadability, MOdularity and REusability (known as the RE-MO-RE paradigm which I have just invented), and not take any golden rule too seriously if it conflicts too much with the project particularities (time-rush to test something, fine-tuned-special-purpose algorithm...)
 

sorex

Expert
Licensed User
Longtime User
in some case less lines is more processing under the hood.

you never know what a function does (extra type checking for example) that replaces some functions that can be written with normal code.
 

Peter Simpson

Expert
Licensed User
Longtime User
I always find myself rewriting code once I have finished developing an app. I rewrite my code at the end of a project to make it smaller and more efficient if possible, especially when developing on an Arduino as that has limited memory and processing power compared to and Android device or even an ESP8266 microcontroller. I enjoys going through my code and cutting down the lines, why have 20 lines of code when with a bit of ingenuity that may be able to be cut down to 15 or even less than 10 lines of code (but that's only if my original code was too long in the first place for example). The B4R examples that I have posted always start life longer than the code that I end up posting, and in some cases what I post is 60%+ smaller than my original code just minutes earlier.

Here are 2 examples that are already on this forum.
B4X:
Example 1:
LONG:
If medead = True Then
    medead = False
Else
    medead = True
End If

SHORT:
medead = Not(medead)

Example 2:
LONG:
If points > 10 Then
    points = 10
Else
    points = points + 1
End If

SHORT:
points = Min(points + 1, 10)

Do Until, Do While, For loops all comes into play and I think very carefully before choosing which one is best for the given scenario. Generally tidying up source code and keeping everything short and sweet is what I do once everything is working correctly in the first place. Oh yes I also put comments in my code to remind me of how more complicated routines work :)

Enjoy...
 

sorex

Expert
Licensed User
Longtime User
that's what I meant with under the hood.. that not() will probably do the same as your long version but with extra call, return, stack operations etc it requires 'more' processing.
 

Peter Simpson

Expert
Licensed User
Longtime User
that's what I meant with under the hood.. that not() will probably do the same as your long version but with extra call, return, stack operations etc it requires 'more' processing.

Maybe, maybe not It's not 'my' not() @sorex, I said that the example came from a post on the forum, I just copied and pasted it.
Not() does make it shorter, easier and faster to read though IMO. It's dead simple to time how much multiple long vs multiple short examples take to perform their tasks, maybe you should consider doing some timing if it bothers you that much and posting the results ;), I'm only joking with you @sorex don't take me seriously :D.

I do understand what you are saying about the extra calls and more processing to execute those extra calls though. I personally like my code as short as humanly possible as I do not like wading through lots of lines if I don't have to, plus it's usually (but not always) more efficient if it's shorter. BTW I don't always use Not(), but I do use it a lot...
 
Last edited:

DPaul

Active Member
Licensed User
Longtime User
In the early days there was no "Select Case", there was "If...Then".

When "select case" appeared it was understood that the "under the hood" processing of "select case"
was many times faster than "If then else..."

Is that still true, because if i can, i will replace "if then" with "select case", even for simple things.

?
DPaul
 

JordiCP

Expert
Licensed User
Longtime User
The funny thing about your response to my post is that you were actively participating in the original post that I got the above code from @JordiCP, but you never said jack all about it then.
oops! you're right :eek:

My comment was not made to seem 'smarty', hope it has been understood this way ;)
 

sorex

Expert
Licensed User
Longtime User
@Peter Simpson ,

the long version can be reduced to a single lined if/then if you changed logic but then the min() version is still 10 chars shorter.

too bad my brain usually picks the retro version (you never know you want to add more to the THEN) :)

B4X:
points=Min(points+1,10)

if points<10 then points=points+1
 

Peter Simpson

Expert
Licensed User
Longtime User
@Peter Simpson ,

the long version can be reduced to a single lined if/then if you changed logic but then the min() version is still 10 chars shorter.

too bad my brain usually picks the retro version (you never know you want to add more to the THEN) :)

B4X:
points=Min(points+1,10)

if points<10 then points=points+1

Not my original post as I said previously, but thank you anyway @sorex 5/5 :)

FYI what you wrote is how I do it :cool:
 
Top