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.
- 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]
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]- However, I usually advocate efficient code and good comments, so I might do this if I wanted to be extra clear:
[code]
;function returns 1 (True) or 0 (False)
DirExists(Dir)
{
return !!InStr(FileExist(Dir), "D")
}
[/code]- 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.