Tip: Use Constants to Improve Code Readability

Wayne Golding has been a Xojo developer since 2005. He operates an IT Company Axis Direct Ltd www.axisdirect.nz which primarily develops applications using Xojo that integrate with Xero www.xero.com. Wayneâs hobby is robotics where he uses Xojo to build applications for his Pi2 often implementing IoT for remote control.

Guest Post Tip: Formatted Text/Strings in Xojo

Writing long concatenated text in Xojo (or any programming language) tends to make the code difficult to read. Typically there is a block of code that has line extenders (_), a single line that goes on forever or a block of code that appends text to an existing variable. All are hard to read. And when you add SQL statements to the mix it can become difficult to easily see what you are trying to achieve.

I’ve started using Constants for this reason.

This window shows an SQL Select statement in an easy to read format. It is the source of a Prepared SQL Statement for a MS SQL Server query, but it could be for any of the supported DB servers.

To get this window you simply create a new Constant, then paste your text into the Default value field. After this you’ll see an ellipse button appear, clicking that will show the Edit Value window above which is essentially a free format text editor where you can format your text to suit your own requirements.

I also use this technique as a source for merging.

In this case:

Typing this into the editor is far easier than creating the same or worse as a concatenated string. I use a series of Replace instructions to create a personalised email using the Constant as my template.

Hint: Use ReplaceLineEndings(text, <p>) to create a BodyHTMLText value suitable for email.

This code block:

Invites Geoff to join the forum