Article Preview
Buy Now
FEATURE
Writing Better Code
A few tips to get better code in Xojo
Issue: 18.6 (November/December 2020)
Author: Christian Schmitz
Author Bio: Christian Schmitz is the creator of the Monkeybread Software Xojo/Real Studio Plugins.
Article Description: No description available.
Article Length (in bytes): 13,319
Starting Page Number: 12
Article Number: 18602
Related Link(s): None
Excerpt of article text...
Today I offer you a few tips for better Xojo code. Perhaps you can add a few of those to your routines.
Comment First
Before you start writing the first line of code for a method, you should write a comment. The comment should not repeat what you can read in the code later, but answer the question of why you need the method, what it does, andwhy it does it this way. Basically lay out the purpose of the method and when to call it.Declare invariants and assumptions. Like if you get a parameter, e.g. a
FolderItem
, you can document that you expect it to be notnil
, valid, and either point to file, folder, or both. The caller may check to make sure some parameters are in range. Or declare that you check the parameter and raise exceptions. Document which exceptions may be raised by the method.Next, you may think about the blocks in the method and write comments about each part of the method. Before you code, you may have described all parts in comments first. This is
comment-driven development and is very helpful if you develop with several people. Each one needs to know why you chose some ways and not others to code a method.Please do not use abbreviations as not everyone may know years later what you mean. In general, you can expect that you read code several times more often than you write it.
...End of Excerpt. Please purchase the magazine to read the full article.