Return from function - True or 1?

Talk about anything
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Return from function - True or 1?

20 Jul 2018, 12:32

I'm trying to understand the best way to write return from function.

Currently, I use Return, True. However, it seems to be no difference with Return, 1. Which one is better from the point of view of best practice?

Also, is it a good idea to have second Return?

Code: Select all

checkIfDirExists(DirName) {
    If (InStr(FileExist(DirName), "D"))
        Return, True ; True or 1?
    Return ; Do I need it?
}

If checkIfDirExists("Foo")
    MsgBox, Directory exists
MannyKSoSo
Posts: 277
Joined: 28 Apr 2018, 21:59

Re: Return from function - True or 1?

20 Jul 2018, 12:36

For the return value personally I use 1 if you are using an If statement since it wants to have 1 or 0 as its value. As for the second return, you don't need it as the } basically is your last return.
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Re: Return from function - True or 1?

20 Jul 2018, 12:44

MannyKSoSo wrote:For the return value personally I use 1 if you are using an If statement since it wants to have 1 or 0 as its value. As for the second return, you don't need it as the } basically is your last return.
Well, after reading the docs it seems I understand. The True is used as alias to 1. So, 1 is better.

Another point to use 1 is the possible confusion between True and variable:

Code: Select all

return MyVar 
return True
They looks identically, but works completely different.

https://autohotkey.com/docs/commands/Return.htm
User avatar
jeeswg
Posts: 5406
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Return from function - True or 1?

20 Jul 2018, 13:01

I would use 1 to be clear, because sometimes True is defined as -1 e.g. Excel macros.
User avatar
gregster
Posts: 1481
Joined: 30 Sep 2013, 06:48

Re: Return from function - True or 1?

20 Jul 2018, 13:02

https://autohotkey.com/docs/Variables.htm#Boolean
The words true and false are built-in variables containing 1 and 0. They can be used to make a script more readable...
Boolean values: When an expression is required to evaluate to true or false (such as an IF-statement), a blank or zero result is considered false and all other results are considered true. For example, the statement if ItemCount would be false only if ItemCount is blank or 0. Similarly, the expression if not ItemCount would yield the opposite result.
False is a built-in variable that evaluates to 0. Only false, 0 or a blank value will be considered false in a boolean sense.
True is a built-in variable that evaluates to 1. But all variable values that are not 0, false or blank will be considered true in a boolean sense, for example 34, -0.99 or the literal strings "hello" or "false".

Code: Select all

if "false"
	msgbox "false", but true
if false
	msgbox 
else 
	msgbox false %false%
User avatar
Flipeador
Posts: 1019
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Return from function - True or 1?

20 Jul 2018, 13:06

If the function does not return any value, it makes no sense to use return.
If the function is going to return an empty string, return "" can be more descriptive (personally I don't like to treat an empty string as "nothing" or as FALSE).
If your function is going to return only values 1/0 as true/false (boolean), using TRUE/FALSE can be more descriptive than 1/0.

john_c wrote:Another point to use 1 is the possible confusion between True and variable
There is no confusion if you have read the documentation, TRUE/FALSE are of type constant. It is not difficult to understand. https://autohotkey.com/docs/commands/Se ... tm#Remarks.
Note: I moved this topic to the Offtopic subforum due to: Which one is better from the point of view of best practice?.
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
swagfag
Posts: 1537
Joined: 11 Jan 2017, 17:59

Re: Return from function - True or 1?

20 Jul 2018, 13:21

for ur particular example, the following constitutes best practice by my book:

Code: Select all

checkIfDirExists(DirName) {
    return InStr(FileExist(DirName), "D")
}
User avatar
Flipeador
Posts: 1019
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Return from function - True or 1?

20 Jul 2018, 13:33

jeeswg wrote:I would use 1 to be clear, because sometimes True is defined as -1 e.g. Excel macros.
-1 is still TRUE. ;)
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
jeeswg
Posts: 5406
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Return from function - True or 1?

20 Jul 2018, 14:06

- @Flipeador:

Code: Select all

Func1()
{
	return True ;ambiguous (unless you already knew that in AutoHotkey, True = 1)
}
Func2()
{
	return 1 ;unambiguous
}
- @john_c: Btw I would call the variable 'Dir', not 'DirName'. I think that 'dir name' is ambiguous, e.g. for 'C:\Program Files\AutoHotkey', I would say that 'AutoHotkey' is the dir name.

- Of the following two versions, which do people prefer?

Code: Select all

checkIfDirExists(Dir)
{
	if (InStr(FileExist(Dir), "D"))
		return 1
	else
		return 0
}
checkIfDirExists(Dir)
{
	if (InStr(FileExist(Dir), "D"))
		return 1
	return 0
}
- Here is what I have for a DirExist backport using Coco's coding style. Part of 'AHK v2 functions for AHK v1'.

Code: Select all

DirExist(FilePattern)
{
    local AttributeString := FileExist(FilePattern)
    return InStr(AttributeString, "D") ? AttributeString : ""
}
- Here is a function I wrote in the past.

Code: Select all

JEE_IsDir(vDir)
{
	return !!InStr(FileExist(vDir), "D")
	;return !!DirExist(vDir)
}
User avatar
Flipeador
Posts: 1019
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Return from function - True or 1?

20 Jul 2018, 14:15

jeeswg wrote:unless you already knew that in AutoHotkey
I already answered it above. It's "ambiguous" for new users who do not know anything about programming. Are you going to limit yourself just because there are users who do not know anything about programming?.Has no sense.

TRUE is not used as 1, but as "true". If your function is going to return 1, you must use return 1, and not return TRUE.
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
gregster
Posts: 1481
Joined: 30 Sep 2013, 06:48

Re: Return from function - True or 1?

20 Jul 2018, 14:19

But true evaluates to 1... always. (Of course, you can use other numbers instead.)

Code: Select all

msgbox % test()
test(){
	return true
}
msgbox % true
User avatar
jeeswg
Posts: 5406
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Return from function - True or 1?

20 Jul 2018, 14:22

- @Flipeador: I do tend to consider that often people looking at code will be experienced programmers, who do not happen to be familiar with AutoHotkey, but who would like to translate the code to their favoured programming language.
- @gregster: Thanks for pointing that out, I meant to mention that.
User avatar
gregster
Posts: 1481
Joined: 30 Sep 2013, 06:48

Re: Return from function - True or 1?

20 Jul 2018, 14:23

- @gregster: Thanks for pointing that out, I meant to mention that.
That was rather a repsonse to Flipeador :D

"If your function is going to return 1", you would normally use return 1 - but you wouldn't have to, you could return TRUE, too.
Last edited by gregster on 20 Jul 2018, 14:26, edited 1 time in total.
User avatar
Flipeador
Posts: 1019
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Return from function - True or 1?

20 Jul 2018, 14:26

jeeswg wrote:I do tend to consider that often people looking at code will be experienced programmers, who do not happen to be familiar with AutoHotkey, but who would like to translate the code to their favoured programming language.
That is a totally personal decision. If someone has experience in programming (in another language), it is assumed that you already have to know this. You do not need to know about AutoHotkey for these things. Also, it is not difficult to look at the documentation, unless you are allergic to it.

Edit* I'm not going to reduce the clarity of my Script just to make it easier for other users to understand it (so that in the end he ends up using another programming language), watch a tutorial or read the documentation (or ask me, I'll be happy to explain it to you).
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
Flipeador
Posts: 1019
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Return from function - True or 1?

20 Jul 2018, 14:33

@gregster I'm just saying that if the function is intended to return a boolean value, it is more descriptive to use TRUE/FALSE.
Of course, you can also do things like return TRUE+2, but personally I find it horrible.
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
gregster
Posts: 1481
Joined: 30 Sep 2013, 06:48

Re: Return from function - True or 1?

20 Jul 2018, 14:49

Yeah, I meant specifically: " If your function is going to return 1, you must use return 1, and not return TRUE." Here returning 1 and True is just equivalent - it is exactly the same programatically: 1 will be returned. Of course, you are right, if the return value is not used as a boolean value afterwards, the reader of the source code might get a wrong impression. Therefore, I wrote could :). The use of must in your sentence felt a bit too strong here, I would say should instead...
User avatar
Flipeador
Posts: 1019
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Return from function - True or 1?

20 Jul 2018, 15:05

@gregster You have understood perfectly what I was referring to. Yes, I used the wrong word.
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
jeeswg
Posts: 5406
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Return from function - True or 1?

20 Jul 2018, 15:50

I might do something like:
return True ;1
but since it literally returns 1, I might just do:
return 1
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Re: Return from function - True or 1?

20 Jul 2018, 16:52

jeeswg wrote:Btw I would call the variable 'Dir', not 'DirName'. I think that 'dir name' is ambiguous, e.g. for 'C:\Program Files\AutoHotkey', I would say that 'AutoHotkey' is the dir name.
Probably this terminology will be interesting for you: https://stackoverflow.com/a/26637489
User avatar
jeeswg
Posts: 5406
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Return from function - True or 1?

20 Jul 2018, 17:24

- Yes, it's all debatable. Thanks for the link.
- This is pretty good:
SplitPath, InputVar [, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive]
- I actually think of *filename* as full path, and *file name* as 'name' i.e. full path minus dir.
- Here are my notes on Excel macros:
vPath := oXl.ActiveWorkbook.FullName ;path (dir\name)
vDir := oXl.ActiveWorkbook.Path ;dir
vName := oXl.ActiveWorkbook.Name ;name

Return to “Offtopic”

Who is online

Users browsing this forum: Gio and 12 guests