Protected 'already declared'

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Protected 'already declared'

Post by Little John »

Rinzwind wrote:The point being that dumping all protected variables at the top splits the code in two and looks messy.
No, it looks tidied up and the code is not "split" at all.
Rinzwind wrote:Sometimes a var is needed at two separate spots, so mentioning it twice would be handy syntactically
Sometimes a variable is needed even at say 10 spots. You can "mention" it as often as you like. But declaration is only needed once for each variable.
Rinzwind wrote:Side note: Other languages allow you to declare vars scoped to a while-loop or if-branch.
So what? This is a different situation. Just declaring a variable over and over again by repeatedly using Protected does nothing have to do with the scope of the variable.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Protected 'already declared'

Post by Josh »

My opinion is divided on this issue:
  • Personally, I declare all variables at the beginning of a procedure. If not necessary something else, I use only integers and strings for simple variables, whereas I always use the $ sign for strings. This results in a clean readable code, where the lines are not inflated by unnecessary information, making the code more difficult to read.

    If somehow possible, the name should already say enough about the usage, so that a further explanation by a repeated declaration is not necessary. The name of Array, Lists and Maps is always the plural (simplified by an appended 's') of the individual elements, so that it is already clear here what is meant approximately.
  • On the other hand, it should be up to everyone to write their code the way they want. Additionally, Macros or IncludeFiles could cause situations where a variable is declared repeatedly.
For these arguments and that there is definitely no reason why a repetition of Protected should not be allowed, I give a +1 for this request.
sorry for my bad english
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Protected 'already declared'

Post by Little John »

Josh wrote:On the other hand, it should be up to everyone to write their code the way they want.
The world is not that simple. A programming language is not just for people to communicate with a computer, but it's also for people to communicate with each other. That's why e.g. we are sharing code here on the forum. An effect of that proposed strange programming style is confusing less experienced users, as we've seen here already.
Josh wrote:Additionally, Macros or IncludeFiles could cause situations where a variable is declared repeatedly.
Perhaps with Define or Global. But with Protected :?:
I can't recall ever having read that someone had a problem with this during 12+ years here on the forum.
Josh wrote:there is definitely no reason why a repetition of Protected should not be allowed
A programming language shouldn't encourage people to use poor programming style.

The main problem with spreading variable declarations all over a procedure probably is, that this defeats the purpose of EnableExplicit!

EnableExplicit is very useful for detecting typos.
So if e.g at the beginning of a procedure it reads

Code: Select all

Protected fill
and later in the procedure it reads

Code: Select all

fll = 3
then the compiler will complain about this typo.

But if we write

Code: Select all

Protected fll = 3
somewhere in the middle of a procedure, then this typo will not be detected even if EnableExplicit is used!

In contrast, declaring all protected variables in one place dramatically reduces that risk. When it reads

Code: Select all

Protected fill, fll
at the beginning of a procedure, we will immediately see that this looks suspicious, and we will check whether or not this is actually intended.
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Protected 'already declared'

Post by Rinzwind »

In a complex procedure it IS messy if all vars are dumped at top at only used way down. Same thing with globals btw. But there it is possible.

Just give people a choice MAN :D

Anyway, the behavior now is not consistent between global, define (which should be get rid off) and protected (which should be called local but called protected because define exists...).
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Protected 'already declared'

Post by #NULL »

Rinzwind wrote:define (which should be get rid off)
Only in Procedures, where it is used rarely anyway. But it's still used normally for main-local variables with EnableExplicit. But there is nothing wrong with using Define for procedure-local variables. It behaves like protected but is more consistent then with the use of Define for main-locals (except for Dim etc.).
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Protected 'already declared'

Post by #NULL »

I would forbid ANY redeclaration, even with the same type. There is also the issue of wrongly assumed zero-initialization on redeclaration in some cases. Some people might want to allow at least Global redeclaration in Procedures (for clarity), but it's pointless and Shared should be used instead, because the point of Global is to not have to declare it in the procedure.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Protected 'already declared'

Post by TI-994A »

#NULL wrote:...there is nothing wrong with using Define for procedure-local variables. It behaves like protected...
Not exactly. It's not possible to re-declare global variables with Define in any scope. That's where Protected comes in.

Variables should usually be defined only once within each scope. However, since this varies from language to language, and best-practices opinions seem to be split as well, the least we should expect from a language is consistency among the various declarative functions.

So, either allow redundancy with Protected, or limit definitions with Global and Define to only once, within any single scope.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Protected 'already declared'

Post by Demivec »

I believe redundant declarations were allowed as a way to work around include files explicitly declaring variables they used, i.e. a required Global from the file they were included in as source in that they were going to depend on.

I agree with Rinzwind with respect to some incoherence with declaring variables at the top of a long stretch of code when they won't be first used until a much later point. Unfortunately putting declarations and redeclarations at each distinct point a variable is used is very messy. Imagine doing this for an all-purpose index variable 'i'. Since the scope of a variable can't really be restricted any finer than a procedure block it makes more sense to declare everything at the beginning of that block, or at least in a single group with all other variables in that scope so that only one location has to be checked to verify what has been declared and to make useful comparisons for unneeded or incorrectly declared variables such as in the manner that Little John mentioned.
Those choices are based on the choices we have in PureBasic to limit scope and not necessarily from anyone's personal preferences. Those preferences may be based on how things are done in other languages with their respective uses of scope. If a programmer wants to declare variable just before the code where it is used, where that code is part of a much larger body of code, the generally accepted practice would be to make a procedure out of that distinct smaller section of code and declare its own variables inside it. There are many tradeoffs in regard to that, many involving the speed of execution but it can add clarity.

I personally would be happy to see additional choices of scope as part of my wish list, though I don't realistically ever see that happening. I would love to see any block of code able to be designated as a restricted scope and for it to inherent from the scope that it resides in.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Protected 'already declared'

Post by Josh »

TI-994A wrote:Not exactly. It's not possible to re-declare global variables with Define in any scope.
This is the reason why I use in procedures nearly exclusively Define. Identical variable names in the main program and in the procedure can easily lead to confusion.

Protected I actually only use in reusable procedures and in some development tools, whose code is so worn out that I am not interested in it anymore. These procedures I also put between DisableDebugger/EnableDebugger so that they don't show up during debugging.
sorry for my bad english
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Protected 'already declared'

Post by Rinzwind »

Best practices in all programming languages that support it:
declare variables at the place where they are first used. Some even support scoped to if statement or loop etc.

So it would be welcome if I can just mention the variable twice to make the code more clear. I can do so already with Global, Define, Shared and Constants! Protected (and Static) must join that list. It is only consistent that way.

For now, I go back to Define then...
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Protected 'already declared'

Post by Demivec »

Rinzwind wrote: Wed Nov 03, 2021 4:04 am Best practices in all programming languages that support it:
declare variables at the place where they are first used. Some even support scoped to if statement or loop etc.

So it would be welcome if I can just mention the variable twice to make the code more clear. I can do so already with Global, Define, Shared and Constants! Protected (and Static) must join that list. It is only consistent that way.
In the meantime, if you are reusing a variable later in a complex procedure as if it were a newly declared variable why not just use an initializing statement and a comment in place of a new declaration to clarify things. This can be formalized with a macro for numeric and string types according to someone's programming style.

Code: Select all

Macro RedeclareStr(_var_, _value_ = "")
  _var_ = _value_
EndMacro

Macro Redeclare(_var_, _value_ = 0)
  _var_ = _value_
EndMacro

Procedure mystuff()
  Protected test, love$
  ;<code using test and love$>
  
  Redeclare(test, 12) ;reusing test var as a new var
  ;<code using test>
  
  RedeclareStr(love$, "") ;reusing love$ var as a new var
  ;<code using love$>
  
EndProcedure

@Edit: Made a correction to the macro code. I also removed the reminder I previously posted to clarify this post didn't modify anything I had said previously. The reminder apparently seemed to be a bump to the topic for everyone to restate things already said. Don't need any infinite loops here. :)
Last edited by Demivec on Wed Nov 03, 2021 8:31 am, edited 2 times in total.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Protected 'already declared'

Post by Little John »

Demivec wrote:Just a reminder that my viewpoint is that allowing redeclaration using protected is a positive thing.
This is just a reminder that my viewpoint is that allowing redeclaration using “protected” is a negative thing, :-)
see viewtopic.php?p=547898#p547898
Post Reply