Return from function - True or 1?

Talk about anything
lexikos
Posts: 6132
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Return from function - True or 1?

20 Jul 2018, 21:12

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.
User avatar
jeeswg
Posts: 4994
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Return from function - True or 1?

28 Jul 2018, 19:04

- 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:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

- However, I usually advocate efficient code and good comments, so I might do this if I wanted to be extra clear:

Code: [Select all] [Download] GeSHi © Codebox Plus

;function returns 1 (True) or 0 (False)
DirExists(Dir)
{
return !!InStr(FileExist(Dir), "D")
}
- 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.
A_AhkUser
Posts: 866
Joined: 06 Mar 2017, 16:18
Location: France

Re: Return from function - True or 1?

28 Jul 2018, 21:36

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:

Code: [Select all] [Download] GeSHi © Codebox Plus

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
}
User avatar
nnnik
Posts: 3236
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Return from function - True or 1?

29 Jul 2018, 07:33

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.
Recommends AHK Studio
lexikos
Posts: 6132
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Return from function - True or 1?

29 Jul 2018, 23:08

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 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?
return True ;1
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.

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.
swagfag
Posts: 1107
Joined: 11 Jan 2017, 17:59

Re: Return from function - True or 1?

30 Jul 2018, 07:01

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.

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.
User avatar
jeeswg
Posts: 4994
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Return from function - True or 1?

30 Jul 2018, 08:04

- 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.php?f=37&t=29689

Code: [Select all] [Download] GeSHi © Codebox Plus

DirExist(FilePattern)
{
Loop Files, %FilePattern%, D
return A_LoopFileAttrib
}
- The previous version was the original from here:
AutoHotkey-Future/DirExist.ahk at master · cocobelgica/AutoHotkey-Future · GitHub
https://github.com/cocobelgica/AutoHotkey-Future/blob/master/Lib/DirExist.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.php?f=37&t=29689&p=210822#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

Return to “Offtopic”

Who is online

Users browsing this forum: No registered users and 8 guests