Code Naming Conventions


As projects grow, confusion can eventually build up with regards to the number of vars kicking about and it can get quite messy to remember what refers to what. Thought I’d post some recommendations for naming conventions that can make it easier to clearly identify where a vars (or functions) come from within your project.

My personal preference is something like this:
(e.g. CstKtn3Pie could represent Castle -> Kitchen -> Node 3 -> Apple Pie,
or Ktn3Pie could represent Kitchen -> Node 3 -> Apple Pie)

Naming tips:

  • Try keep abbreviation sections in the var name down to 3 characters each otherwise names can get insanely long (CstKtn3Pie is a lot more manageable than CastleKitchenNode003ApplePie!).
  • Ideally, you should be able to look at a var name and recognise exactly where it comes from in your code or guess what it should be called if needed in other parts of the code.
  • Also, even though LUA var types don’t need to be declared, it can be helpful to pre-append the var with a single char representing the contents of the var (‘s’ for string, ‘i’ for integer, etc. - e.g. iCstKtn3PieNo might be used to represent the number of pies at node 3 in the castle kitchen).
  • Name functions appropriately (e.g. ftnCstKtnLever for a function that operates a secret lever in the castle kitchen). You may not have to state the node in a case where the function could be called from more than one particular node number (e.g. maybe the secret lever could be accessed from two different nodes).
  • For globally used vars or functions, you might use ‘Gbl’ instead of the room (e.g. iGblNodRes to represent Global Node Resolution that would hold an integer value of 2048).

Might seem overkill but, as your game project grows in size, it can make navigating and writing code much clearer and prevent you from accidentally reusing an already existing var name.

There are plenty of naming convention standards out there - this is just one that appealed to me that, so far, has worked well the node-based adventures. Does anyone else have other coding styles or tips they recommend?


Sorry to burst your bubble but I rarely use complex conventions in the script code myself. In a way, it goes against the spirit of Lua.

In the case of Dagon, what I’m doing it’s quite simple: rooms begin with a capital letter (i.e.: “Cafeteria”) but nodes within that room have no caps and a number in the end (i.e.: “cafeteria1”, “cafeteria2”, etc). You don’t tend to have more than 5 or 6 nodes per room, so this isn’t quite difficult to manage. Of course, you can add an extra hint if you need it and remove the number: “cafeteria_entrance”, “cafeteria_middle”, and so on.

I will need to post more code to further illustrate this, but my suggestion is: don’t complicate yourself with conventions in the script. If needed, make really long var names. NEVER abbreviate (this is also true for some conventions in typed languages such as Objective C). You don’t need to make a distinction between global and local either.

Particularly in the case of Lua, it’s advised against adding prefixes to vars, precisely because they have no type! This:

[code]string = “Hello!”

string = 0[/code]

Is perfectly valid. Confusing at first, yes, but once you let yourself “go”, there are no problems whatsoever :wink: