Coder-friendly syntax for class functions and methods

Automatically translated from English

Using $1, $2, and $n variables in your code not only makes the code less readable, it can also increase confusion since the human brain can find it difficult to remember what these variables correspond to. The solution is to create a variable with an understandable name and assign it the value of $1, $2, or $n.

4D v18 R5 has good news, you can now name your parameters when declaring:

  • a project method,
  • a trigger
  • a database method
  • a form method
  • a constructor of a class
  • a function of a class.

 

Let’s see how!

In 4D v18 R4, we showed you a new syntax to declare your variables:

var $name : Text
var $birthdate : Date
var $file : 4D.File

The new syntax for methods and functions is very similar.

New declaration for classes

After the name of the function, add your various input parameters separated by a semicolon in parenthesis:

Class Constructor($firstname : Text; $lastname : Text; $birthdate : Date)
 This.lastName:=$lastname
 This.firstName:=$firstname
 This.birthdate:=$birthdate

Function setAdress($streetNumber : Text; $streetName : Text; $city : Text)
 This.streetNumber:=$streetNumber
 This.streetName:=$streetName
 This.city:=$city

For the output parameter, just add the “->” symbol, then the name and type as the input parameter.

Function getFullname -> $fullname : Text
 $fullname:=This.firstName+" "+Upper(This.lastName)

Function sendMessage($text : Text) -> $message : Text
 $message:="@"+This.firstname+": "+$text

New declaration for methods

The same principle applies to methods with a new keyword: #DECLARE.

  • An example with a “highlightRow” project method:

#DECLARE($listboxName : Text; $rowNum : Integer)
LISTBOX SELECT ROW(*; $listboxName; $rowNum)

  • An example with the On Web Authentication database method:

#DECLARE($url : Text; $header : Text; \
  $BrowserIP : Text; $ServerIP : Text; \
  $user : Text; $password : Text) \
  ->$RequestAccepted : Boolean

$entitySelection:=ds.User.query("login=:1"; $user)
// Check hash password...
// ...

Debugger

The name of the parameter is also visible in the call chain pane of the debugger. Here’s an example with a class function:

Bonus

If you need an additional reason to use the new syntax, take a look at the image below. Yes! The declaration of the function is displayed in the status bar exactly as it is for 4D commands.

Vanessa Talbot

• Product Owner •

Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.

Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.