Data Access Worldwide Knowledge Base
Article ID 2007 Article Title INFO: The right location of parentheses in an expression is important Article URL http://www.dataaccess.com/kbasepublic/kbprint.asp?ArticleID=2007 KBase Category VDF (GENERAL) Date Created 05/19/2004 Last Edit Date 06/01/2004
Article Text
The purpose of this small knowledge base article is to reinforce that the compiler cannot validate whether the intention of the expression was programmed correctly.
As an example we want to take some action when the entered customer name is blank. One does this by comparing the value of a variable (field from a datafile) with an empty string. If you want to be sure that the database field (or variable) is empty you might want to do a TRIM function call.
If (Trim (Customer.Name) = "") Begin
DoSomething
End
If you misplace the closing parentheses for the fieldname/variable at the end of the expression the compiler will still see a valid expression. So look at the following expression and notice the difference between this one and the first code snippet.
If (Trim (Customer.Name = "")) Begin
DoSomething
End
The result of expression one and expression two is however different. In the first case the DoSomething is executed when the field value is really empty. In the second case the DoSomething is always executed no matter if there is data in the field or not. This because (TRIM (TRUE)) is always true like (TRIM (FALSE)) is true. Even when the "=" will be replaced with "<>" the result will be true.
The IF works on a boolean expression and the test whether there is data in a string can also be tested with:
If (sData) ....
Technically correct, it would be better if above was written as:
If (sData <> "") ....
It is much cleaner and the intention is much more clearly stated to whoever is reading the code (compiler/runtime/human).
So watch out for the placement of parentheses in expressions.
The first example code can also be written as:
If (Customer.Name = "") Begin
// Do something
End
This is because our DataFlex runtime considers the trailing spaces in a database field as empty - non existing - data.
Web Links Related to this Article
DAW Knowledge Base article 1754: INFO: Using the Appropriate IF Clause with Different Datatypes
URL=http://www.dataaccess.com/KBasePublic/KBPrint.asp?ArticleID=1754
Email this Article
Email this Article to a Colleague
Send Feedback on this Article to Data Access Worldwide
Copyright ©2010 Data Access Corporation. All rights reserved.
The information provided in the Data Access Technical Knowledge Base is provided "as is" without warranty of any kind. Data Access Corporation disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Data Access Corporation or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if Data Access Corporation or its suppliers have been advised of the possibility of such damages. Some states do not allow the exclusion or limitation of liability for consequential or incidental damages so the foregoing limitation may not apply.