Pregunta técnica

HMG en Español

Moderator: Rathinagiri

Post Reply
jorge.posadas
Posts: 172
Joined: Mon May 19, 2014 7:43 pm
DBs Used: DBF, SQLite, MS-SQL, ACCESS, MariaDB (en proceso)
Location: Morelia, Mich. México
Contact:

Pregunta técnica

Post by jorge.posadas »

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

POSADAS SOFTWARE
Jorge Posadas Ch.
Programador independiente
Morelia, Mich.
M é x i c o .
Movil +52 44 3734 1858
SKYPE: jorge.posadasch
Email: posoft@gmx.com
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Pregunta técnica

Post by mol »

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: 595
Joined: Tue May 24, 2016 4:36 pm
DBs Used: DBF

Re: Pregunta técnica

Post by Anand »

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
Regards,

Anand

Image
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Pregunta técnica

Post by mol »

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: 920
Joined: Mon Jun 24, 2013 11:53 am
DBs Used: DBF, MySQL, Oracle
Location: Subotica, Serbia

Re: Pregunta técnica

Post by dragancesu »

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