Page 1 of 2

++ and -- additions to PB

Posted: Tue Oct 06, 2020 8:22 am
by kinglestat
Consider the following code

Code: Select all


Example1
Index = i
i + 1

*Suggested*
Index = i++

Example2
i + 1
Index = i

*Suggested*
Index = ++i


Re: ++ and -- additions to PB

Posted: Tue Oct 06, 2020 11:40 am
by spikey
The existing method is already fully optimized - it's not going to get any better.

Code: Select all

; i + 1
  INC    qword [v_i]
; 
; i + 2
  ADD    qword [v_i],2
; 
; i + 3
  ADD    qword [v_i],3
It doesn't make sense actually as notation to mathematicians, non-IT scientists or non-IT engineers. The only people who will understand it are people who are already experienced programming in C, or derived languages, to some degree. As a consequence I would argue that it really doesn't improve program readability. It's just terse and arcane. (Yes I realise that there are lots of people around that would still understand it but that's not the point.)

Re: ++ and -- additions to PB

Posted: Tue Oct 06, 2020 11:56 am
by BarryG
spikey, that's not what he's asking. In example 1, he wants "Index = i++" to make the variable "Index" equal "i", and then increment "i" by one immediately after; so two steps in one command. Not very Basic-like, so I vote no.

Re: ++ and -- additions to PB

Posted: Tue Oct 06, 2020 12:11 pm
by spikey
Yeah fair point. The resulting assembler is more complex if you include the assignment - and I was trying to point out simply that the current convention is already optimized. The pre-increment and post-increment notations were orginally present in C to make sure the compiler optimized the output properly - PB does this automatically. (I hope by now that modern C compilers will too but I haven't checked).

Thinking about it - that's another reason not to do it - it gives a false impression that the increment and the assignment will somehow become one operation magically - this isn't true at all.

As I said before I really feel that this notation in no way adds to program readability and that's the only reason to implement it in PB.
It's not necessary to get an optimization.

Re: ++ and -- additions to PB

Posted: Tue Oct 06, 2020 1:32 pm
by Mistrel
Please provide a use case where this would result in more readable code.

Re: ++ and -- additions to PB

Posted: Tue Oct 06, 2020 2:23 pm
by Bitblazer
No. I like using this shorter syntax in other languages but it really reduces the readability of sources. That should be an important aspect for a basic language. I know some CPU's have special command sets which benefit from this, but that should be a case for the optimization phase of a compiler. The more important aspect here is the readability and the readability would get worse as more and more examples would use it.

And for any experienced user who really needs it - use a macro ;)

Re: ++ and -- additions to PB

Posted: Tue Oct 06, 2020 2:42 pm
by Little John
kinglestat wrote:Consider the following code

Code: Select all


Example1
Index = i
i + 1

*Suggested*
Index = i++

Example2
i + 1
Index = i

*Suggested*
Index = ++i

The main effect of this would be to worsen the readability of the code.

-1 from me.

Re: ++ and -- additions to PB

Posted: Tue Oct 06, 2020 2:58 pm
by skywalk
I agree this does NOT improve the PB language.
Add multiline comments waaay before considering this. :evil:

Re: ++ and -- additions to PB

Posted: Tue Oct 06, 2020 11:53 pm
by STARGĂ…TE

Code: Select all

Macro Inc(var)
	var : var + 1
EndMacro
Macro PreInc(var)
	var + 1 : var + 1
EndMacro
Macro Dec(var)
	var : var - 1
EndMacro
Macro PreDec(var)
	var - 1 : var - 1
EndMacro


Define I.i = 4
Define Index = 4

Index = Inc(I)
Debug Index
Debug I

Index = PreInc(I)
Debug Index
Debug I

Index = Dec(I)
Debug Index
Debug I

Re: ++ and -- additions to PB

Posted: Wed Oct 07, 2020 1:11 am
by Tenaja
skywalk wrote:I agree this does NOT improve the PB language.
Add multiline comments waaay before considering this. :evil:
CompilerIf works well for multiline comments.

I wished it had the c style inc, too... After coding a while, I kind of got used to not not having it. However, adding the feature won't break anything, and won't change other people's code who choose not to use it. I would definitely use it, if added... But it would need good documentation as to the order of things, and what was modified.

Re: ++ and -- additions to PB

Posted: Wed Oct 07, 2020 5:30 am
by kinglestat
So many lawyers :)

First of all, it's not to optimize regarding speed, but reducing lines of code.
I chose PB many years back mainly because my gut feeling was that Freak and Fred seem to come from a C background, much like me.
I do understand readability, but much like in C, it's the coders choice when and if to make the code more readable.

Code: Select all

 AddMapElement( mapVariables(), varname )
         mapVariables()\Index    = gVarPos
         gVarPos + 1

;Suggsted

 AddMapElement( mapVariables(), varname )
         mapVariables()\Index    = gVarPos++

;1 line saved and more readable in my opinion


Re: ++ and -- additions to PB

Posted: Wed Oct 07, 2020 7:13 am
by collectordave
-1

Code: Select all

 AddMapElement( mapVariables(), varname )
         mapVariables()\Index    = gVarPos
         gVarPos + 1

;Suggsted

 AddMapElement( mapVariables(), varname )
         mapVariables()\Index    = gVarPos++
Looking at the code as a non experienced programmer the first one is ok.

Assign gVarPos to the map element and then increment gVarPos

The suggested to me looks like Assign the incremented gVarPos to the map element.

When learning a language readability is the key even if only a few posted examples using the suggested way it would confuse those like me attempting to learn.

Why not:-

Code: Select all

 AddMapElement( mapVariables(), varname )
         mapVariables()\Index    = gVarPos:gVarPos + 1
One line saved still readable.

CD

Re: ++ and -- additions to PB

Posted: Wed Oct 07, 2020 8:44 am
by Little John
kinglestat wrote:So many lawyers :)
No. Just some people with an own opinion.
kinglestat wrote:

Code: Select all

;Suggsted

 AddMapElement( mapVariables(), varname )
         mapVariables()\Index    = gVarPos++

;1 line saved and more readable in my opinion

This is only readable for people who know C (or languages such as Java that also use this syntax).
It has nothing got to do with a Basic-style language.
People who like C-style syntax maybe just should use C. :mrgreen:

Re: ++ and -- additions to PB

Posted: Wed Oct 07, 2020 10:02 am
by kernadec
Hi
can be use macro
Best regards

Code: Select all

Macro inc(x,y)
  x = x + y
EndMacro

Macro dec(x,y)
  x = x - y
EndMacro

While i.d < 360
    inc(i,Sqr(2))
 Debug i
Wend

; While  i.d > -360
;     dec(i,Sqr(2))
;  Debug i
; Wend

Re: ++ and -- additions to PB

Posted: Wed Oct 07, 2020 5:21 pm
by #NULL
Macros won't work within arbitrary expressions and within procedure arguments etc.

Code: Select all

    int i;
    i = ++i + i++ + ++i;
    printf("%d", ++i); // 7
I guess a procedure taking by ref and returning by val would work better, but it's not serious.
I'm not for this adaption btw. It's very non-pb and anybody who thinks Fred/Freak would consider adopting this is delirious :)