Pregunta técnica

HMG en Español

Moderator: Rathinagiri

Post Reply
jorge.posadas
Posts: 78
Joined: Mon May 19, 2014 7:43 pm
DBs Used: DBF, SQLite, MS-SQL, ACCESS, MariaDB (en proceso)
Location: Tizayuca, Hgo. México
Been thanked: 3 times
Contact:

Pregunta técnica

Post by jorge.posadas » Tue Jul 17, 2018 10:43 pm

Grupo

¿Qué es mejor usar?
1- IF .NOT. MyFunction()
::
::
ENDIF

o esta

2. IF !MyFunction
::
::
ENDIF
es decir es mejor usar .NOT. o !

De antemano agraezco el comentario, y la pregunta surge porque en SQL cuando se valora una variable es mejor usar IF LEN(cvariable) = 0 a usar IF(cVariable) = '''
Cordialmente

Jorge Posadas Ch
Tizayuca, Hgo.
M é x i c o .
Movil +52 5514272013
SKYPE: jorge.posadasch
Email: jorge.posadas@gmail.com

User avatar
mol
Posts: 3069
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 179 times
Been thanked: 96 times
Contact:

Post by mol » Wed Jul 18, 2018 6:04 am

There is a problem with such a construction for testing empty string:

Code: Select all

IF(cVariable) = ''
In my opinion, it should look like this:

Code: Select all

IF cVariable = ''
but, this statement always return .t.
It's better to use

Code: Select all

if len(cVariable) == 0
or

Code: Select all

if !(cVariable=="")
else
...

User avatar
Anand
Posts: 320
Joined: Tue May 24, 2016 4:36 pm
DBs Used: DBF
Has thanked: 64 times
Been thanked: 44 times

Post by Anand » Wed Jul 18, 2018 9:31 am

Agree with Mol.

better use 'If Empty(cVariable)' code to check if a variable is empty.

Using '=' is a mistake as the logic of it is to stop checking after reaching end of 'right side' text.
So, 'abcd' = 'ab' is true
but 'abcd' == 'ab' is false
So 'abcd' = '' will be always true
but 'abcd' == '' will be false.

Regards,

Anand
Image

User avatar
mol
Posts: 3069
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 179 times
Been thanked: 96 times
Contact:

Post by mol » Wed Jul 18, 2018 11:55 am

EMPTY function can be problematic in some cases, too.
If your string consists only from space characters (chr(32)), this function return .T.

User avatar
dragancesu
Posts: 579
Joined: Mon Jun 24, 2013 11:53 am
DBs Used: DBF, MySQL, Oracle
Location: Subotica, Serbia
Has thanked: 15 times
Been thanked: 111 times

Post by dragancesu » Thu Jul 19, 2018 6:56 am

The main thing is to write a function

1. if you are using: IF myfunc()
then the function should be written to return TRUE or FALSE,
It's better for me because it's solved in one place

2. if you are using: _variable = myfunc()
then
IF _variable ...

see http://www.oohg.org/cl53/ng11d14f.html

Post Reply