++ and -- additions to PB

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
kinglestat
Enthusiast
Enthusiast
Posts: 732
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

++ and -- additions to PB

Post 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

I may not help with your coding
Just ask about mental issues!

http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: ++ and -- additions to PB

Post 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.)
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re: ++ and -- additions to PB

Post 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.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: ++ and -- additions to PB

Post 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.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: ++ and -- additions to PB

Post by Mistrel »

Please provide a use case where this would result in more readable code.
Bitblazer
Enthusiast
Enthusiast
Posts: 733
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: ++ and -- additions to PB

Post 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 ;)
webpage - discord chat links -> purebasic GPT4All
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ++ and -- additions to PB

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: ++ and -- additions to PB

Post by skywalk »

I agree this does NOT improve the PB language.
Add multiline comments waaay before considering this. :evil:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ++ and -- additions to PB

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: ++ and -- additions to PB

Post 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.
kinglestat
Enthusiast
Enthusiast
Posts: 732
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Re: ++ and -- additions to PB

Post 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

I may not help with your coding
Just ask about mental issues!

http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: ++ and -- additions to PB

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ++ and -- additions to PB

Post 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:
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: ++ and -- additions to PB

Post 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
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: ++ and -- additions to PB

Post 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 :)
Post Reply