Return from function - True or 1?

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Return from function - True or 1?

Re: Return from function - True or 1?

Post by john_c » 10 Sep 2019, 13:15

@Flipeador
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.
This thread is 1 year old, and now I completely agree with your ideas. I want to better understand them.

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.

Re: Return from function - True or 1?

Post by jeeswg » 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 ... 37&t=29689

Code: Select all

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

Re: Return from function - True or 1?

Post by swagfag » 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.

Re: Return from function - True or 1?

Post by lexikos » 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.

Re: Return from function - True or 1?

Post by nnnik » 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.

Re: Return from function - True or 1?

Post by A_AhkUser » 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

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?

Post by jeeswg » 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

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
}
- However, I usually advocate efficient code and good comments, so I might do this if I wanted to be extra clear:

Code: Select all

;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.

Re: Return from function - True or 1?

Post by lexikos » 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.

Re: Return from function - True or 1?

Post by jeeswg » 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

Re: Return from function - True or 1?

Post by john_c » 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

Re: Return from function - True or 1?

Post by jeeswg » 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

Re: Return from function - True or 1?

Post by Flipeador » 20 Jul 2018, 15:05

@gregster You have understood perfectly what I was referring to. Yes, I used the wrong word.

Re: Return from function - True or 1?

Post by gregster » 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...

Re: Return from function - True or 1?

Post by Flipeador » 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.

Re: Return from function - True or 1?

Post by Flipeador » 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).

Re: Return from function - True or 1?

Post by gregster » 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.

Re: Return from function - True or 1?

Post by jeeswg » 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.

Re: Return from function - True or 1?

Post by gregster » 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

Re: Return from function - True or 1?

Post by Flipeador » 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.

Re: Return from function - True or 1?

Post by jeeswg » 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)
}

Top