Lua conventions

From OpenComputers: Rebooted
Revision as of 08:08, 24 August 2026 by imported>OCDoc Import (Imported from legacy OpenComputers documentation at ocdoc.cil.li)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lua Code Conventions

  • Be consistent.
  • Indent using spaces, two wide. Do indent properly.
  • Try to limit width of the code to 80 chars.
  • Don't add spaces between braces/brackets and what's in them.
  • Do use brackets in functions calls even if it's not necessary.
  • Name variables for what they are, don't include type markers in them, i.e. do not use Hungarian notation.
  • Nice to have: sort your requires alphabetically (OCD!)
  • Feel free to comment your code. Any additional memory needed to load your file due to heavy use of comments is released soon after loading is complete.
  • If you have to validate arguments, use the built-in checkArg method. Homogenous error messages are a good thing! Its usage is checkArg(n, value, type1, ...), where n is the number of the argument, value is the value of the argument and type1 and so on are the allowed types for the argument, as retrieved via type(value). The number is used in the error message like so: "bad argument #n (type1 expected, got type(value))". So for example, to require the first argument to be a number you'd do checkArg(1, arg, "number").

Bad Code:

function f(sArg1 , ... )
   assert(type(sArg1)== "string", "me wants a strign!")
    if sArg1 then
        local nResult = 1
        --do some more stuff
        return nResult
  end
end
if  f ( "a" )  ==1 then
print"asd"
end

Good Code:

function f(name, ...)
  checkArg(1, name, "string")
  if name then
    local result = 1
    -- We extrapolate the b-spline of the non-euclidean space to
    -- determine the fraction of potential failures encountered.
    return result
  end
end
if f("a") == 1 then
  print("asd")
end

Contents

Section Links
OpenComputers
General Install and Use OpenOS - Crossmod Interoperation - Lua Conventions - Computer Users
Blocks Category Documentation
Blocks Computers Computer Case - Server Rack - Microcontrollers
Display Hologram Projector - Keyboard - Screen
Power and Networking Access Point - Capacitor - Net Splitter - Power Converter - Power Distributor - Relay
Extensions Adapter - Cable - Disassembler - Disk Drive - Geolyzer - Motion Sensor - Redstone I/O - Transposer - 3D Printer
Robotics Assembler - Charger - Drone - Robot - Waypoint
Items Category Documentation
Items Components Debug Card - Data Card - Graphics Card - Internet Card - Linked Card - Network Card - Redstone Card - Wireless Network Card
Storage Devices Floppy Disk - EEPROM - Hard Disk Drive - Memory
Upgrades Angel Upgrade - Battery Upgrade - Chunkloader Upgrade - Crafting Upgrade - Database Upgrade - Experience Upgrade - Generator Upgrade - Hover Upgrade - Inventory Controller Upgrade - Inventory Upgrade - Leash Upgrade - Navigation Upgrade - Piston Upgrade - Sign I/O Upgrade - Solar Generator Upgrade - Tank Upgrade - Tank Controller Upgrade - Tractor Beam Upgrade - World Sensor Upgrade
Containers Card Container - Upgrade Container - Disk Drive
Others Analyzer - Texture Picker - Component Bus - CPU - APU - Drone Case - Manual - Microcontroller Case - Nanomachines - Remote Terminal - Terminal Server - Server - Tablet Case - Tablet - Crafting Materials - Ink Cartridge - Scrench - Hover Boots - Grog - Loot Disks
APIs Category Documentation
APIs OpenOS buffer - colors - component - computer - event - filesystem - uuid - internet - keyboard - note - process - rc - robot - serialization - shell - sides - term - text - thread - transforms - unicode
Lua Libraries coroutine - package - io - os
Components Category Documentation
Components Core Components 3D Printer - Abstract Bus - Access Point - Chunkloader - Computer - Crafting - Data Card - Database - Debug - Drone - Drive - EEPROM - Experience - Filesystem - Generator - Geolyzer - GPU - Hologram - Internet - Inventory Controller - Leash - Microcontroller - Modem - Motion Sensor - Navigation - Net Splitter - Piston - Redstone - Redstone in Motion - Robot - Screen - Sign - Tank Controller - Tractor Beam - Transposer - Tunnel - World Sensor
Others Component Access - Signals
Cross-Mod Integration Applied Energistics
Addon Mods Category Documentation
Addon Mods Addons Computronics
Supporting OpenComputers Thut's Concrete - HBM's Nuclear Tech Mod
Tutorials Category Documentation
Tutorials Mod Specific Basic Computer - Writing Code - Hard Drives - Autorun and Startup scripts
Modding Custom Architectures - IMC Messages - API changes in OC 1.3 - API changes in OC 1.4 - Build and Run master-MC1.7.10 from source
Programs OPPM - install
Others Custom Operating Systems
Other Languages Page
Simplified Chinese 简体中文(Simplified Chinese)