qaisjp

Lua indentation 101

Mar 25th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. -- Indenting your code - Programming 101
  2. -- Indenting your code makes things more clear for people reading your code
  3.  
  4.  
  5. -- This indented code is much easier to understand
  6. if is_admin then
  7.     if player_vehicle then
  8.         destroyElement(player_vehicle)
  9.         outputChatBox("Your vehicle has been destroyed", player)
  10.     else
  11.         outputChatBox("You are not in a vehicle", player)
  12.     end
  13. end
  14.  
  15.  
  16. -- than this unindented code
  17. if is_admin then
  18.     if player_vehicle then
  19.     destroyElement(player_vehicle)
  20.     outputChatBox("Your vehicle has been destroyed", player)
  21. else
  22.     outputChatBox("You are not in a vehicle", player)
  23.     end
  24. end
  25.  
  26.  
  27. -- Both bits do the following:
  28. -- If the user is an admin and in a vehicle, destroy the vehicle (and tell them this)
  29. -- If the user is an admin and in NOT a vehicle, tell them this
  30. -- BUT If the user is not an admin, don't do anything
  31.  
  32.  
  33. -- But the second snippet is BADLY indented!
  34. -- It looks like:
  35. -- If the user is an admin, destroy the vehicle and tell them this
  36. -- If the user is not an admin, tell them they are not in a vehicle
  37.  
  38.  
  39. -- When new "scopes" are created you should indent.
  40. -- When you use local variables those variables are only available in those scopes.
  41. -- New "scopes" are created when (this list in incomplete) you use:1
  42.     -- "for .. do .. end",
  43.     -- "repeat .. until ...",
  44.     -- "while .. do ... end",
  45.     -- "do .. end" etc
  46.  
  47. -- Use the tab key (above caps lock on the left) to indent.
  48. -- Make sure you use Sublime Text or Notepad++ to edit your code.
  49. -- DO NOT use plain notepad.
Advertisement
Add Comment
Please, Sign In to add comment