This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ProfessionalProcrastinator's profile picture

Best Way to Clean Up Code

Started by ProfessionalProcrastinator, 19 April 2014 - 02:14 AM
ProfessionalProcrastinator #1
Posted 19 April 2014 - 04:14 AM
I spent a few hours today churning out this replacement of edit. It is far from complete, but I have enough features that I think code cleanup should begin. It's large enough that edit is starting to bog down. What are some of your suggestions for cleaning up this code?

http://pastebin.com/Bv9dtFn6
kornichen #2
Posted 19 April 2014 - 07:39 AM
Looks nice until now. I would put comments over the functions and variable declaration and main loop if existing. That helps you to understand your own code when reading it later. Sounds crazy like: "Why should I not understand my own code later?" but I can promise you it's true. Just had a look at the KreOS 1.0 code yesterday and I am so happy that I wrote comments.
Lignum #3
Posted 19 April 2014 - 09:59 AM
Everything looks fine so far, however…
I would suggest adding a new line after, what I like to call, a "paragraph".

For example this function:
Spoiler

local function drawBottomLineDisplay()
  term.setCursorPos(1, height)
  local lnstr = "L"..currentLine..", C"..currentChar
  term.clearLine()
  local dispstatus = status
  if dispstatus:sub(1, 4) == "err:" then
	dispstatus = status:sub(5, #dispstatus)
	doSetColor(colors.white, colors.red, true)
  end
  term.write(dispstatus)
  doSetColor(colors.white, colors.black)
  term.setCursorPos(width-#lnstr, height)
  term.write(lnstr)
end

Would become:
Spoiler

local function drawBottomLineDisplay()
  --# These three functions work together to achieve
  --# one thing. That's what I call a "paragraph".
  --# That term doesn't actually exist in that sense, I believe.
  term.setCursorPos(1, height)
  local lnstr = "L"..currentLine..", C"..currentChar
  term.clearLine()

  --# You should always insert newlines before and after blocks like
  --# this if statement here.
  local dispstatus = status

  if dispstatus:sub(1, 4) == "err:" then
	dispstatus = status:sub(5, #dispstatus)
	doSetColor(colors.white, colors.red, true)
  end

  --# Achieves one thing...
  term.write(dispstatus)

  --# Achieves a different thing.
  doSetColor(colors.white, colors.black)
  term.setCursorPos(width-#lnstr, height)
  term.write(lnstr)
end

You should also put spaces before and after operators.

e.g.:

local lnstr = "L"..currentLine..", C"..currentChar

becomes

local lnstr = "L" .. currentLine .. ", C" .. currentChar
Edited on 19 April 2014 - 08:00 AM
viluon #4
Posted 19 April 2014 - 01:29 PM
And btw I recommend you using a different editor than "edit". Try Notepad++, it has a wonderful syntax highlighting and lots of features "edit" doesn't offer.
ProfessionalProcrastinator #5
Posted 19 April 2014 - 07:39 PM
viluon, if I wanted to use something other than edit, I would use Geany because ARCH LINUX MASTER RACE– sorry, that happens sometimes.

As for Lignum and Kornichen, thanks! Both of those suggestions make perfect sense, and I'll do that before doing much else, like adding syntax highlighting, etc.
Agoldfish #6
Posted 19 April 2014 - 09:58 PM
viluon, if I wanted to use something other than edit, I would use Geany because ARCH LINUX MASTER RACE– sorry, that happens sometimes.

As for Lignum and Kornichen, thanks! Both of those suggestions make perfect sense, and I'll do that before doing much else, like adding syntax highlighting, etc.
I think Sublime Text has a linux version. There is ComputerCraft highlighting for it.