Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Should empty variables be treated as 0 in math expressions?


  • Please log in to reply
43 replies to this topic

Poll: Should math expressions treat empty variables as zero? (15 member(s) have cast votes)

Should math expressions treat empty variables as zero?

  1. No, an empty variable should produce an error. (5 votes [33.33%])

    Percentage of vote: 33.33%

  2. Yes, an empty variable should equal zero, but #Warn should provide notification of it. (5 votes [33.33%])

    Percentage of vote: 33.33%

  3. Yes, an empty variable should equal zero, and #Warn should not provide notification of it. (5 votes [33.33%])

    Percentage of vote: 33.33%

Vote Guests cannot vote
majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006

Concerning the idea of adding a directive to choose the behavior, I don't like it because:

Funny how you didn't think that way when implementing #NoEnv.....
Posted Image

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I consider #NoEnv a transitional directive on the way to v2. My tentative intent is to remove that directive in v2 and eliminate automatic environment variables altogether. In other words, scripts would have to use EnvGet to retrieve environment variables.

Also, I hope you don't mind but I changed your topic title "Using zeros on non existent variables in expressions" to something more appropriate to what this topic has become. If you don't like the new title, feel free to edit it.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

Thanks PhiLho. I also checked the following in PHP:
$a = ""; // Init to explicit empty string.
5 + $a -> Yields 5 without warning.
5 * $a -> Yields 0 without warning.
5 / $a -> Yields "" and gives warning "Division by zero".

Yes, I forgot to test the empty string cases. I updated my previous message, but I didn't tested the division.
Curiously, permissive VBS is the only one not allowing such automatic conversion...

in hindsight, directives like #EscapeChar may have been a bad idea

It might be added to the list of commands to put in a purgatory section of help, as I suggested before... Indeed, these commands are bad idea. :O)

I believe I will vote for change of behavior in v2: empty string becomes 0 in arithmetic operations. No strong opinion on underfined variables. Defaulting them to 0 would be consistent to defaulting them to empty. On the other hand, typos like 'variiable' instead of 'variable' would be harder to spot.
I don't agree on the point of view of Chris on such directive, it is nice for debugging. After all, Perl has 'use warnings', VB has 'option explicit', and so on.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004

I don't agree on the point of view of Chris on such directive, it is nice for debugging. After all, Perl has 'use warnings', VB has 'option explicit', and so on.

Although I'm against a #TreatEmptyAsZero directive (since it would basically split AHK into two different languages), I'm in favor of directives that change the warning level such as #MustDeclareVars and #ShowWarnings (even though #MustDeclareVars isn't exactly a warning, I think it falls short of splitting AHK into two different languages).

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

My tentative intent is to remove that directive in v2 and eliminate automatic environment variables altogether. In other words, scripts would have to use EnvGet to retrieve environment variables.


EnvGet() please! Command syntax will not be suitable for this.

Regards, :)
kWo4Lk1.png

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
EnvGet is already a command but will probably someday have a function counterpart (as will most simple OutputVar commands).

As for the poll, I'm a little surprised at the way it's going. I thought people would opt for treating empty variables as zero. I haven't voted, but if I did it would probably be for the "Maybe" option.

JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004

Does anyone know offhand how PHP and other weakly-typed languages handle empty strings and unitialized variables in math expressions?

...someone posted an example already, but...

javascript:a='';alert(a+5)      //5
javascript:a='1';alert(a+5)     //15
...since JavaScript uses + for concat...this problem fixes itself...it don't "treat empty strings as zero"...it treats "adding an empty string to a number as concatenation"...I know no one likes my idea on that, but JavaScript got concat right...VB's & sucks...PHP's . sucks...adding strings & numbers are the same...

I think you should remove all "bug detection" (wait, read on...) in favor of nice syntax, easy, cool...& instead replace it with a directive (yup, I said it)...

#Help
;...or...
#OKIGiveUpWhatsWrong
...that directive would turn all warnings on like...#MustDeclareVars...it would be a general purpose "Help me!" call to the AHK parser...which would/could catch the "error" of adding strings to numbers (since some people *cough*PhiLho*cough* think it's an error)...
Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
For me the only logical alternative to the current AHK behavior was to treat empty variables such that they do not change the value of the rest of the expression. That is, x+empty = x, x*empty = x, x**empty = x, etc. It means, empty variable should be 0 in additions/subtractions, 1 in multiplication or as divisors or exponents. However, empty/x or empty**x still do not make sense, probably a 0 is what most of us would expect. Is not it too complicated? I voted for keeping the current behavior. (Also, environment variables, global variables will give you surprises if you rely on un-initialized variables).

And the fun just starts here. What about Abs(empty), Ceil(empty)...? They return 0 now, which is a bit inconsistent. It is similar with Sin, Sqrt, Exp, Ln. And the two-argument arithmetic functions: Mod(empty,2) Mod(2,empty), Round(x,empty)?

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
JSLover, thanks for the ideas.

What about Abs(empty), Ceil(empty)...? They return 0 now, which is a bit inconsistent. It is similar with Sin, Sqrt, Exp, Ln. And the two-argument arithmetic functions: Mod(empty,2) Mod(2,empty), Round(x,empty)?

That's a good point. I either didn't think of it when designing them, or just opted for code simplicity (can't remember which). In any case, it seems fairly harmless so the current behavior should probably be kept for backward compatibility. On the other hand, they could be changed in conjunction with #v2 if anyone can make a case that it would be worthwhile.

Thanks.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006

if anyone can make a case that it would be worthwhile.

Well.... what to say....
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
I voted for the first option, but I don't agree with the wording of the pool:

1) Yes because the convenience of EmptyString+5=5 outweighs the bug-detection benefits.

3) No, leave it as-is because EmptyString+5="" helps detect bugs (and/or other benefits).

I don't feel that current behavior helps to detect bugs. We get a strange result, but neither warning nor error. So it doesn't detect bug, but introduces a "wrong" behavior that can be actually hard to detect/find.

In any way, I don't agree with Laszlo, I don't feel such variable should have a neutral behavior. At least, no language I know have chosen this option...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
I agree.

I already tried to change that and also to add vote option for directve but I can't do that even in my own post...

Current questions are directing the answer...
Posted Image

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Current questions are directing the answer...


:D

That is why I opted not to vote! "Other" would rather make my opinion sound faint!
kWo4Lk1.png

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
If the current poll doesn't have what you need, feel free to start another one in a new topic. Alternatively, I can erase this one after it's run a few more days, which should allow you to create another one here.

However, I don't think a directive like #TreatEmptyVarsAsZero should be added for the reasons I described earlier.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006

However, I don't think a directive like #TreatEmptyVarsAsZero should be added for the reasons I described earlier.

Why should we vote then if your vote is the "strongest" ? I don't have problem with you commanding, but please state that loud, so we don't live in imaginary world.

Your reasons are not good for me:

1) It reduces the ability to copy & paste code from one script to another. In other words, if a function or subroutine relies on the directive being turned on, it wouldn't work when pasted into another script whose author wants it kept turned off.

2) It reduces the ability to collaborate on a project when one author is used to writing functions with the directive On and the other with it Off. You might say, "they can just turn it on and off in their individual functions/include-files, then paste them together". But it just seems wrong to encourage a split in scripting habits by providing the directive in the first place.

I would call this 1) as it is the same thing repeated twice.
I don't see this as an valid reason. There are already number of colaboration breaking directives and NoEnv is not the only one. In my merging article I describe some such problems. This was one of the reasons why I asked you to think about multiple threads of execution and namespaces. Coordmode, SetKeyDelay, SetFormat etc.. will all change scripts behavior if not specified so regarding to this new directive will not spoil collaboration as it is already not possible for sure. It will just introduce new "colaboration case". People get used to NoEnv witch introduced the very same type of problem.

3) It complicates the language because arguably, there are already too many directives.

This thing is optional and as such can not spoil the languages as it adds new functionality as needed. People that don't want to use it can skip it or even not know for its existance. The same as with NoEnv. Number of people here including me start every new script with this directive but then, there is much bigger community that doesn't...

THE POINT: New directive can not spoil something that is already spoiled.


For me the only logical alternative to the current AHK behavior was to treat empty variables such that they do not change the value of the rest of the expression.

No other language behaves this way. I don't have problem with that except if it makes coding much harder.
Posted Image