Don’t be afraid of undefined values (Blog)

Good to know: https://blog.4d.com/object-notation-improvement-after-customer-feedback/

But the last part, I found missleading:

Finally, you can also pass undefined values to Case of and If statements. In this case, 4D considers an undefined value as False:

If ($o.b)
// if $o.b is undefined this code is not executed

End if

This works only for boolean Values. You will get an error if $o.b is not undefined or Bool. (eg. $o.b:=“xyz”)

Better to use:

If ($o.b=Null)
// if $o.b is undefined this code is not executed 
... 
End if

or use:

If (Undefined($o.b))
// if $o.b is undefined this code is not executed 
... 
End if

And a good practice is:

 C_OBJECT($o)
 
 Case of
    :($o.test=Null)//trap
    :($o.test="xyz")
       ...//and do what you will 
 End case

or

  $o:=New object("test2";"abc")
   //use the (OB Get ;Is ...) 4D magic...
 If (OB Get($o;"test1";Is text)#"")  //False  (undefined -> gives empty string)
    Trace
 End if 
 If (OB Get($o;"test2";Is text)#"")  //True
    Trace
 End if 

The OB GET command will not throw an error if you declare the type, as I did. Without the type it will.
(Kirk Brooks https://forums.4d.com/Post/FR/34662884/1/34664046#34664046)

1 Like

thanks for the review…

True. Same for dot notation…

 If (OB Get($o;"test1";Is text)#"") 

or

if (String($o.test1)#"")

String of undefined/Null gives “”
String of “hello” give “hello”
String of 5 gives “5”

The same misleading example you can find in the docs. I commented this in livedoc some time ago:

https://livedoc.4d.com/4D-Language-Reference-17.4/Objects-Language/Using-object-notation.300-4883128.en.html#3540527

A quote:

This example works in the case of $o.a is a boolean only:

C_OBJECT($o)
 If($o.a) // false
 End if
 Case of
    :($o.a) // false
 End case

Have a look at this (german) topic too:

https://discuss.4d.com/t/objekte-und-undefiniertes/12251

Thank you Thomas, String($o.test1) is far more readable than OB Get with Type.

So my best practice to test for Object Properties will be:

 C_OBJECT($o)

if (String($o.name)#"") // if not undefined and not Empty String
       ...//  do what you will 
End if

if (Num($o.idNumber)#0) // if not undefined and not 0
       ...//  do what you will 
End if

User Comments at the Documentation? Great idea! I haven´t noticed it before. Usually I use a local downloaded hmtl Doc, but this would be a big benefit. But I can´t see any Comments at the mentioned URL, neither EN or DE…

I don’t see Lutz example too. Sometimes comments are moved from user comments into the doc page itself by the doc team, maybe that’s what happened.

When you’re connected on livedoc, you can see on that page some :left_speech_bubble:, they correspond to user’s comments; blue = “added by another user”, red = “added by me”.

Here’s a page with a user comment - it’s not very modest to quote myself, but it’s just to show that, as comments are not translated, if you want french/english versions, you’ll have to add it twice.

Options when you add a comment are: for me, public, for doc team. There’s a quite short delay when it’s public before it becomes visible.

I agree user comments in documentation is a great idea… with near to zero advertising :face_with_raised_eyebrow: . AFAICR, the 1st time I heard about it was from Olivier Deschanels, maybe during a world tour. From previous forum, that thread talks about that, 2011.

1 Like

If sometimes you need next a obGet($o;“test2.x.y.z”;Is text)
without guaranty that always whole path exists (sometimes x not exist, sometimes y…)
And sometimes you have a long paths from $o.a.b.c…until.z

C_OBJECT($o)
C_TEXT($txt)
$o:=New object("test2";"abc")  // only example
  // in some real use cases it can be not know which contents the obj has
  // sometimes mainObj.subObj exist and sometimes mainObj ist empty/null
  // example any shopAPI which send only needed contents,
  // noProductDescription or their subitems when no description is defined for only one product...
$txt:=zObjGetByPropertyPath ($o;"test2.x.y.z";Is text)
  // -> you got always a value type text back as if is wished in optional 3rd parameter
  // so you never got throw an error or do any error hiding with onErrCall
  // this method returns always the optional wished destination type
  // and you can do any blind fetch with a PropertyPath-String to any existing or non existing obj/subObj
  // in this example the whole SubPath x.y.z did not exist
  // but the methode clearified this for you without any hiding Err by onErrCall
  // The method is build with simple obGet in a Loop
  // and destination-type of the last obGet is like third parameter
  // and all obGet before last entry in path adviced to give fix "Is object"
  // so all steps in the loop can be done without any type-errors.
1 Like

It seems 4D doesn’t like my comments :frowning:. I just detected this above my comment:

image

“not validated” means “not visible to the users” apparently.
And to be sure, you are logged in?

yes, I am logged in.

Just too addition my last post (but i am sure all of you know this or know this better than me):

// The method example call "yObjGetByPropertyPath ($o;"test2.x.y.z";Is text)"
// did exacly the same if you write a syntax like this

$txt:=OB Get(OB Get(OB Get($o;"x";Is object);"y";Is object);"z";Is text)

// but this syntax is not good readable do(do(do(...)))
// and when you had more sub-levels than it is a cryptic syntax
// for machine no problem it works,
// but for human people this level in a level hierarchy can not saved manage/controll
// Not good reading code is the most reason for programming bugs and do not see it...