Interactive fiction in Lua – Part 02

  Second part – variable naming and starting out

When you code you want to set up an easy-to-read naming convention for your game. There are a number of Lua style guides for suggestions, but at the end of the day it’s up to you design the structure of your code.

Personally I like to write a few short notes at the start of my program to keep me organized. Choose whatever naming conventions you like but make sure to keep order in your naming rules. Example:

–– Variable naming and notes:
–– global variables = g_camelCaseNames
–– functions = silent_snake_case()
–– FIXME = Issues that need to be fixed
–– DEBUG = Temporary debugging code; delete when not needed
–– DELETEME = Old functions or sections that are due for deletion

It is my preference to make global variables visible by prefixing them with ”g_”. While this convention makes the variables longer, it also pressures me into reducing the number of global variables overall, which is a good thing.

Having the global variables clearly visible at an early stage is preferable for me rather than having the hassle of changing their names at a later stage. It reduces the cluttering of the namespace, especially when you are working in an environment that gives suggestions for the variable you are typing.

 Method independence

It is helpful to aim at making the code as independent of methods as possible. When you change the architecture of an object property, a player stat or how you handle queries for game data, it is generally preferable to have the method being handled by a designated function independently.

The reason for this is that you will likely change the methods of data access during development, perhaps several times, and you want to make the actual nitty-gritty details of the method handling centralized;

Instead of re-writing, let’s say, 100 lines of code when changing your data structure, you would rather just change a central function and its method. Or at least, reduce the workload and the risks for bugs being introduced as far as possible.

In the next post I will continue with example functions for object ID’s and examples of method independence.

Leave a Reply