Jump to content

Advice on using numbers as variables...


Recommended Posts

3 hours ago, JockoDundee said:

 But you don’t see any value in using descriptive constants for the return codes, for instance?

That's an interesting question... not really, unless there is a provided function to "decode" them... something like:

Global Enum $_WINHTTP_ERR_SUCCESS,
            $_WINHTTP_ERR_INVALID_OBJECT,
            $_WINHTTP_ERR_NO_CONNECTION,  ; etc
            $_WINHTTP_ERR_SIZE

Global Const $__WINHTTP_ERR_DESCRIPTION[$_WINHTTP_ERR_SIZE] = [ _ 
        [$_WINHTTP_ERR_SUCCESS, "Success: No error"],_ 
        [$_WINHTTP_ERR_INVALID_OBJECT, "Invalid object passed to function"],_ 
        [$_WINHTTP_ERR_NO_CONNECTION, "A connection was not made to the external server"]_
    ]

Func _WINHTTP_ErrorDescription($iError)
    For $i=0 To Ubound($__WINHTTP_ERR_DESCRIPTION) - 1 ; Should equal $_WINHTTP_ERR_SIZE, but don't use it in case the array wasn't updated
        If $iError = $__WINHTTP_ERR_DESCRIPTION[$i][0] Then
            Return $__WINHTTP_ERR_DESCRIPTION[$i][1]
        EndIf
    Next
    Return SetError(1, 0, False)
EndFunc

; N.B. Didn't test, just as an example

I would love to have something like this in a lot more UDFs, but I realize that it's probably not realistic in most cases. It might make sense in something like the WebDriver, where there are a limited number of errors, but this doesn't work for everyone/everywhere.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

On 11/1/2020 at 8:46 PM, JockoDundee said:

... Not to add ex post facto requirements as a justification for my decision, but when you doing chess interfaces, a large portion of the code is looping up files and over ranks and doing compares etc. So performance has to be considered.Basically, things like

For $row = 0 To 7
   For $col = 0 To 7
   
   If $board[$row][$col] = ...
  
  Next
Next

Chimp’s code requires multiple String library calls per usage.  So I was looking to maintain the performance of direct array access with more intuitive semantics.

you could then use a "nearly" direct access to the array using an "indirect" addressing to the row with no loss of performances and allowing the normal usage of the "For Next" loops ...

#include <array.au3> ; only For _ArrayDisplay
Local $board[8][8]
Local $aRow[] = [7, 6, 5, 4, 3, 2, 1, 0] ; for the indirect addressing of the rows (see below)*
Local Enum $a, $b, $c, $d, $e, $f, $g, $h ; (not really needed)

Local $asCol[] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] ; just to view the mnemonics in the array

For $row = 0 To 7 ; just use 0 to 7 as you normally would

    For $col = $a To $h ; or use 0 to 7 as well

        ; instead of addressing the row directly with $board[$row][$col]
        ; use *indirect addressing like here below $board[ $aRow[$row] ][$col]
        ;                                                  -----------
        $board[$aRow[$row]][$col] = $asCol[$col] & $row + 1

    Next
Next

_ArrayDisplay($board)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

wanting to stay on topic with the post title, if we also want to use numbers as variable names I would use this "fake" association

Local Enum $1, $2, $3, $4, $5, $6, $7, $8

therefore always using indirect addressing to reach the checkboard cells

#include <array.au3> ; only For _ArrayDisplay
Local $board[8][8]
Local $aRow[] = [7, 6, 5, 4, 3, 2, 1, 0] ; for the indirect addressing of the rows (see below)*
Local Enum $a, $b, $c, $d, $e, $f, $g, $h
Local Enum $1, $2, $3, $4, $5, $6, $7, $8 ; allows to use these "dummy" numbers $1-$8 as (and instead of) 0-7

Local $asCol[] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] ; just to view the mnemonics in the array

For $row = $1 To $8 ; instead of using 0 to 7 as you normally would
    For $col = $a To $h ; instead of using 0 to 7 as you normally would

        ; instead of addressing the row directly with $board[$row][$col]
        ; use *indirect addressing like here below $board[ $aRow[$row] ][$col]
        ;                                                  -----------
        $board[$aRow[$row]][$col] = $asCol[$col] & $row + 1

    Next
Next

_ArrayDisplay($board)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • 3 months later...
On 11/3/2020 at 6:59 AM, Chimp said:

wanting to stay on topic with the post title, if we also want to use numbers as variable names I would use this "fake" association

Little did I know that you were the true pioneer of using numbers as variable names :)

 

 

Code hard, but don’t hard code...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...