Whats a good resource for writing "good" lua code?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
monkyyy
Citizen
Posts: 52
Joined: Fri Mar 16, 2012 5:29 pm

Whats a good resource for writing "good" lua code?

Post by monkyyy »

Its one thing to bodge together something small with trial and error with banging my head against the lua api ; it's quite another to take on a major project with some semblance of sanity 3 minutes into it.

Anyone got anything worth reading on how to do the latter?
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Whats a good resource for writing "good" lua code?

Post by Tjakka5 »

Most of it comes from practice and somewhat personal preference. (I for one like to Camelcase modules)

Other than that, Kikito has a nice website with a few things about writing proper code. check it out here: http://kiki.to/
User avatar
jojomickymack
Prole
Posts: 45
Joined: Tue Dec 26, 2017 4:52 pm

Re: Whats a good resource for writing "good" lua code?

Post by jojomickymack »

This is probably known by everyone here already, but the PIL (programming in lua) book is free online and highly relevant. Also, why not buy a copy and support the lua project? - http://www.lua.org/donations.html
http://www.lua.org/pil/contents.html

Chapter 16 and 17 contains the most important part of what 'good lua' consists of - and I see the pattern below verbatim in lua source code.

Code: Select all

function Account:new (o)
  o = o or {}   -- create object if user does not provide one
  setmetatable(o, self)
  self.__index = self
  return o
end
don't be afraid of setmetatable or __index - it's the key to lua's prototype model - definitely worth memorizing. If that snippit makes sense to you, you're a "good lua coder".
User avatar
Rucikir
Party member
Posts: 129
Joined: Tue Nov 05, 2013 6:33 pm

Re: Whats a good resource for writing "good" lua code?

Post by Rucikir »

luacheck is a tool for linting and static analysis of Lua code. While it won't check the aestetics of your code, it can check that you don't use global variables, don't shadow variables, and several more useful options. It is really handy.
User avatar
kbmonkey
Party member
Posts: 138
Joined: Tue Sep 01, 2015 12:19 pm
Location: Sydney
Contact:

Re: Whats a good resource for writing "good" lua code?

Post by kbmonkey »

Separate game logic from the view code for major projects. Use busted and try your hand at test-driven development. Your brain will thank you when your major project doesn't resemble a bowl of spaghetti.
Ostego160
Prole
Posts: 14
Joined: Tue Oct 17, 2017 7:18 pm

Re: Whats a good resource for writing "good" lua code?

Post by Ostego160 »

In my basic understanding, I break what we do down into two categories: proficiency in the language and design patterns.

These two things are frequently mutually exclusive as a design pattern can be applied in many languages.

On that note: I find there is plenty of information on language proficiency, not so much on patterns. As a novice myself, I find my shortcomings are unrelated to language proficiency and more to do with design patterns (where I think the gold is). I keep two reference books always on hand (besides Programming in Lua): Design Patterns: Elements of Reusable Object-Oriented Software and the more recent Game Programming Patterns.

This answered many of my 'how' questions. Too many tutorials will focus on how to draw a box or write 'hello world', not how to write an observer/listener command system (example). And let's face it, there are some great tutorials on YouTube but most are quite terrible. However, those books are organized and will teach you about how to organize your software and some industry standards instead of having to reinvent the wheel.

Hope this helps!
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Whats a good resource for writing "good" lua code?

Post by Roland_Yonaba »

I believe the Lua Style Guide might be a good reference here. Olivine-Labs also shared their own Lua Style Guide. Both of them are not meant to be authorative, but they both present good habits and idioms which are commonly adopted by the Lua community.
Post Reply

Who is online

Users browsing this forum: No registered users and 55 guests