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

[Poll] Generally preferred way to organize/view code?

Started by Nhorr, 22 February 2016 - 02:25 PM
Nhorr #1
Posted 22 February 2016 - 03:25 PM
I'm writing some code, and I've gotten to the point where I want to be both efficient but at the same time have the code easily readable not just for myself, but others.

So, to get an idea, which of these three code samples is the best in both efficiency and readability?

1. Standard
local function menuBar()
  term.setCursorPos(1,1)
  term.clearLine()
  term.setBackgroundColor(deskColors["menuColor"])
  term.setTextColor(deskColors["menuTextColor"])
  term.setCursorPos(2,1)
  if s==0 or s==nil then
	term.write("1|Start   2|Options   3|NhUI")
  else
	term.write("-|Start   -|Options   -|NhUI")
  end
end

2. Semi-condensed
local function menuBar()
  term.setCursorPos(1,1)
  term.clearLine()
  term.setBackgroundColor(deskColors["menuColor"])
  term.setTextColor(deskColors["menuTextColor"])
  term.setCursorPos(2,1)
  if s==0 or s==nil then term.write("1|Start   2|Options   3|NhUI") else term.write("-|Start   -|Options   -|NhUI") end
end

3. What I'm calling "Relation Grouping"
local function menuBar()
  term.setCursorPos(1,1) term.clearLine()
  term.setBackgroundColor(deskColors["menuColor"]) term.setTextColor(deskColors["menuTextColor"])
  term.setCursorPos(2,1) if s==0 or s==nil then term.write("1|Start   2|Options   3|NhUI") else term.write("-|Start   -|Options   -|NhUI") end
end

Feel free to leave your choice and own personal opinions/suggestions in the comments. I'd love to hear em.
Alternatively: http://strawpoll.me/6889818

Thanks in advance for the feedback!
Edited on 22 February 2016 - 04:23 PM
Dog #2
Posted 22 February 2016 - 03:58 PM
I would personally go with number 2 and change the last line to

term.write((s == 0 or s == nil) and "1|Start   2|Options   3|NhUI" or "-|Start   -|Options   -|NhUI")
Nhorr #3
Posted 22 February 2016 - 05:23 PM
I would personally go with number 2 and change the last line to

term.write((s == 0 or s == nil) and "1|Start   2|Options   3|NhUI" or "-|Start   -|Options   -|NhUI")

Thanks for the feedback. Yeah, I need to practice implementing ternary operators more, I only just recently learned about them but they make a lot of code more organized and efficient.
Konlab #4
Posted 23 March 2016 - 12:02 PM
I think number one looks the most readable
Edited on 23 March 2016 - 11:03 AM
Lignum #5
Posted 23 March 2016 - 12:29 PM

In many editors, you'll find subtle margins on the right of the code, as seen here. These serve as a guide to how long a line of code should become. So if you find yourself writing an expression that exceeds these bounds, it's probably wise to expand it across multiple lines instead.

Always keep in mind that while compact code may seem clever to you, it's likely hard to read for other people. So don't be afraid to take up more lines than you need. If you're still worried that your code may cause confusion, use comments.
CrazedProgrammer #6
Posted 24 March 2016 - 12:35 PM
I write mostly the standard way, but if there's an if statement with only one command in it I'll cut it down to one line:

if x < 1 then x = 1 end
apemanzilla #7
Posted 24 March 2016 - 08:10 PM
I write mostly like style 1, but I also use do blocks to break up different sections of code, which lets me use editors to fold the code.
Eg:

do
  --setup stuff
end

do
  -- main logic
end

do
  -- cleanup
end
Edited on 24 March 2016 - 07:14 PM
Bomb Bloke #8
Posted 24 March 2016 - 11:34 PM
I write mostly the standard way, but if there's an if statement with only one command in it I'll cut it down to one line:

Back when I started learning BASIC (somewhere over twenty years ago), spreading it out over multiple lines seemed the exception rather than the rule. You even had to use an extra keyword if you wanted to do that, whereas if you phrased the entire block as a single line you didn't even have to "end" it!

Hence it's always bugged me to see people spacing out really simple blocks over multiple lines.

Always keep in mind that while compact code may seem clever to you, it's likely hard to read for other people. So don't be afraid to take up more lines than you need.

I take the opposite view; verbose code is annoying to read because I spend all my time scrolling through rubbish that could've been packed into a much smaller space. What's "hard to read" hinges more on the experience of the reader anyways.

That said, I do appreciate proper formatting. Reading code without proper paragraph structure is as annoying as reading regular text which lacks it.

My pet peeve would be coders who use functions as comments. They get this silly idea in their head that "code that needs comments is bad", and come up with the brilliant "workaround" of writing ten million functions that do nothing of consequence… but they can pretend it's more "readable" because the function names splattered everywhere explain their program flow.

This is a double-whammy, because not only does this make their code even longer, but you have to spend more time working the scroll bar to read it - as much of the reading is done in reverse order.

Another one's assignments. For some reason many coders just don't seem to "get" how to perform them in mass, and spread them out over a page.