Return from function - True or 1?
Re: Return from function - True or 1?
The effect of the code is not the most important aspect in the context of best practices (consider comments, for instance). True does not just mean "the number 1", even though it has that value - the added meaning is the whole reason for having True and False. Return True shows that the function's result should be interpreted as a boolean value, as in if TheFunction(x), not a number as in if TheFunction(x) = 1. A beginner reading the code should not need to know that True = 1; on the contrary, using return 1 with if TheFunction(x) might only serve to mystify the code.
Re: Return from function - True or 1?
- lexikos's post reminded me of something I meant to say. If you use 'return True' within a function, then the function should only ever return True or False. The use of 'return True' or 'return False' implies that the function only returns 'True' or 'False', i.e. a Bool.
- So I would do:- However, I usually advocate efficient code and good comments, so I might do this if I wanted to be extra clear:
- Personal preference: I would probably use 1/0 explicitly in functions, and perhaps True/False in scripts to be more descriptive. Functions are generally hard to follow, so I would put any important information in comments just above or at the top of the function.
- In some situations, seeing 'return True' might mislead people into thinking that the output value has been defined as a Bool, and that anything you output via return will be converted to True or False.
- So I would do:
Code: Select all
DirExists(Dir)
{
if InStr(FileExist(Dir), "D")
return True ;1
return False ;0
}
;or:
DirExists(Dir)
{
if InStr(FileExist(Dir), "D")
return 1 ;True
return 0 ;False
}
Code: Select all
;function returns 1 (True) or 0 (False)
DirExists(Dir)
{
return !!InStr(FileExist(Dir), "D")
}
- In some situations, seeing 'return True' might mislead people into thinking that the output value has been defined as a Bool, and that anything you output via return will be converted to True or False.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Return from function - True or 1?
As I see it, true does not even mean the number 1 (that is as real or natural number). The couple true/false|false/true is not ordered, for example.
true/false is rather intended for logical (aka boolean) operations - not for arithmetic calculations.
It is a question of design: if your fonction is intended to be used alongside or and and the function should return a boolean value (and the name should already inform about it: 'isXX', 'XXexist' etc.) and provide, if necessary, any additional return values using ByRef parameters:
true/false is rather intended for logical (aka boolean) operations - not for arithmetic calculations.
It is a question of design: if your fonction is intended to be used alongside or and and the function should return a boolean value (and the name should already inform about it: 'isXX', 'XXexist' etc.) and provide, if necessary, any additional return values using ByRef parameters:
Code: Select all
var := 2 + isInteger(3)
; versus the following, better I think
var := 2
if (isInteger("test") || isInteger(3))
var := var + 1
isInteger(_prm) {
if _prm is integer
return true
return false
}
Re: Return from function - True or 1?
In AHK v1 it's safe to assume that true means 1 and that false means 0.
A lot of calculations rely on this.
AHK v2 is still open for change however it doesnt seem like anybody wants to change it.
A lot of calculations rely on this.
AHK v2 is still open for change however it doesnt seem like anybody wants to change it.
Recommends AHK Studio
Re: Return from function - True or 1?
I don't see the connection between what you say and what you show. If anything, wouldn't you want to return true or false, since the function only returns true or false?jeeswg wrote:The use of 'return True' or 'return False' implies that the function only returns 'True' or 'False', i.e. a Bool.
- So I would do:
[...]
return 1 ;True
return 0 ;False
}
I would never comment the value of True. Comments for code that is self-explanatory are just noise. It takes very little to learn what value True returns.return True ;1
As for the DirExist example: DirExist is built-in in v2, and it returns the first matching directory's file attributes, like FileExist. Your implementation is faulty; if you pass a file pattern that matches both a file and a directory, the result may be incorrect.
Re: Return from function - True or 1?
InStr(FileExist(Dir), "D") is what's outlined in the docs as a substitute for DirExist. how is it faulty and in which particular case would that fail? a folder cannot have the same name as a file.lexikos wrote:... Your implementation is faulty; if you pass a file pattern that matches both a file and a directory, the result may be incorrect.
Re: Return from function - True or 1?
- Thanks lexikos. Here's a fix, I'll add it to the library if no-one can see a problem with it.
commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=29689- The previous version was the original from here:
AutoHotkey-Future/DirExist.ahk at master · cocobelgica/AutoHotkey-Future · GitHub
https://github.com/cocobelgica/AutoHotk ... rExist.ahk
- Btw I asked here whether the Type backport could be made to identify Floats correctly.
commands as functions (AHK v2 functions for AHK v1) - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 22#p210822
- (Re. True/False. My main point was that 'return True' and 'return', in the OP, should become 'return True' and 'return False'.)
- I'm tempted to indicate 1/0 over True/False because that's what will be 'printed' if you use MsgBox.
- I suppose people could find the value in the documentation, my instinct was that is was sufficiently difficult that a comment might be worthwhile. 'True' is an awkward search term for the documentation, it's hard to find the info. I couldn't remember the page. And even on the right page, you have to search for it repeatedly until you get to the proper entry.
Variables and Expressions - Definition & Usage | AutoHotkey
https://autohotkey.com/docs/Variables.htm
commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=29689
Code: Select all
DirExist(FilePattern)
{
Loop Files, %FilePattern%, D
return A_LoopFileAttrib
}
AutoHotkey-Future/DirExist.ahk at master · cocobelgica/AutoHotkey-Future · GitHub
https://github.com/cocobelgica/AutoHotk ... rExist.ahk
- Btw I asked here whether the Type backport could be made to identify Floats correctly.
commands as functions (AHK v2 functions for AHK v1) - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 22#p210822
- (Re. True/False. My main point was that 'return True' and 'return', in the OP, should become 'return True' and 'return False'.)
- I'm tempted to indicate 1/0 over True/False because that's what will be 'printed' if you use MsgBox.
- I suppose people could find the value in the documentation, my instinct was that is was sufficiently difficult that a comment might be worthwhile. 'True' is an awkward search term for the documentation, it's hard to find the info. I couldn't remember the page. And even on the right page, you have to search for it repeatedly until you get to the proper entry.
Variables and Expressions - Definition & Usage | AutoHotkey
https://autohotkey.com/docs/Variables.htm
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Return from function - True or 1?
@Flipeador
1. Do you mean that we don't need to use simple return in the functions and, instead, we should always use return False, return 0, or return ""?
2. Could you provide an example where we should return the empty string? I want to add it to my notes.
This thread is 1 year old, and now I completely agree with your ideas. I want to better understand them.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.
1. Do you mean that we don't need to use simple return in the functions and, instead, we should always use return False, return 0, or return ""?
2. Could you provide an example where we should return the empty string? I want to add it to my notes.
Return to “Off-topic Discussion”
Who is online
Users browsing this forum: No registered users and 28 guests