Eight Good Coding Habits
#41
Posted 27 March 2013 - 03:49 AM
Also, a very important thing is to stay with your style. And when I adopt a function from somewhere else, it really bugs me when the coder uses a style different from mine. I then just HAVE to change his sometable[ Myvariable ]+5 statements to the (in my opinion) more visually pleasing someTable[myVariable] + 5
#42
Posted 28 March 2013 - 08:56 PM
My contribution to this would be to say that any code that does not have unit tests is broken by design.
#43
Posted 28 March 2013 - 09:00 PM
airtonix, on 28 March 2013 - 08:56 PM, said:
My contribution to this would be to say that any code that does not have unit tests is broken by design.
#44
Posted 11 April 2013 - 12:53 PM
gui = {
"/-------\",
"| |",
"\-------/",
}
And then iterating through the table and printing each index. While this isn't a bad habit, I find it much simpler to do:
print [[ /------------\ | | \-------------/ ]]
It's a lot shorter and really saves time.
EDIT: Meh, the code highlighting screwed up my boxes, but I think you get the point
#45
Posted 11 April 2013 - 03:42 PM
Dlcruz129, on 11 April 2013 - 12:53 PM, said:
gui = {
"/-------\",
"| |",
"\-------/",
}
And then iterating through the table and printing each index. While this isn't a bad habit, I find it much simpler to do:
print [[ /------------\ | | \-------------/ ]]
It's a lot shorter and really saves time.
EDIT: Meh, the code highlighting screwed up my boxes, but I think you get the point
local w,h = term.getSize()
local foo = {
{text = "+", col = colours.red, pos = math.random(1,w)},
{text = "*", col = colours.blue, pos = math.random(1,w)},
{text = "+", col = colours.orange, pos = math.random(1,w)}
}
for _,v in pairs(foo) do
term.setTextColour(v.col)
term.setCursorPos(v.pos,math.random(1,h))
term.write(v.text)
end
Just saying
#46
Posted 11 April 2013 - 04:10 PM
SuicidalSTDz, on 11 April 2013 - 03:42 PM, said:
Dlcruz129, on 11 April 2013 - 12:53 PM, said:
gui = {
"/-------\",
"| |",
"\-------/",
}
And then iterating through the table and printing each index. While this isn't a bad habit, I find it much simpler to do:
print [[ /------------\ | | \-------------/ ]]
It's a lot shorter and really saves time.
EDIT: Meh, the code highlighting screwed up my boxes, but I think you get the point
local w,h = term.getSize()
local foo = {
{text = "+", col = colours.red, pos = math.random(1,w)},
{text = "*", col = colours.blue, pos = math.random(1,w)},
{text = "+", col = colours.orange, pos = math.random(1,w)}
}
for _,v in pairs(foo) do
term.setTextColour(v.col)
term.setCursorPos(v.pos,math.random(1,h))
term.write(v.text)
end
Just saying
Yeah, but for simple single-color textboxes and whatnot, multi-line strings are the best way to go.
#47
Posted 14 July 2013 - 06:52 PM
#48
Posted 14 July 2013 - 09:06 PM
airtonix, on 14 July 2013 - 06:52 PM, said:
pcall should suit your needs for unittesting. Not exactly a unittest, but it would protect you from errors and would, at the same time, allow you to identify any bit of code that does not run.
#49
Posted 20 July 2013 - 02:38 PM
#50
Posted 20 July 2013 - 06:20 PM
strayzer, on 20 July 2013 - 02:38 PM, said:
I very, very disagree. I see where you are coming from with naming them, but really only use functions when you need a piece of code several times. To name it I would do something like:
-- This is the "function's" name term.write( "This is " ) local s = "doing" term.write( "what this code is " .. s )
I suppose then you are sometimes doing things like:
local function p() local s = "Hi!" term.write( s ) end local function r( name ) p() -- The one and only call in the whole script term.write( name ) endOf course using some more (complicated) code.
In my opinion it is bad practise, but thats me. I think this is better:
function pr( name )
local s = "Hi!"
term.write( s .. name )
end
#51
Posted 20 July 2013 - 10:58 PM
Engineer, on 20 July 2013 - 06:20 PM, said:
strayzer, on 20 July 2013 - 02:38 PM, said:
I very, very disagree. I see where you are coming from with naming them, but really only use functions when you need a piece of code several times. To name it I would do something like:
-- This is the "function's" name term.write( "This is " ) local s = "doing" term.write( "what this code is " .. s )
I suppose then you are sometimes doing things like:
local function p() local s = "Hi!" term.write( s ) end local function r( name ) p() -- The one and only call in the whole script term.write( name ) endOf course using some more (complicated) code.
In my opinion it is bad practise, but thats me. I think this is better:
function pr( name ) local s = "Hi!" term.write( s .. name ) end
Seeing this makes me think how sometimes good practise and optimisation are at odds.
Dunno why, just does
#52
Posted 21 July 2013 - 05:50 AM
#53
Posted 21 July 2013 - 10:53 AM
#54
Posted 21 July 2013 - 12:07 PM
Bubba, on 21 July 2013 - 10:53 AM, said:
Short code does not mean optimised code.
Sometimes 4 really long lines of code can generate faster bytecode than 2 short lines.
How fast functions are depends on their setup, for example a vararg function is slower than an explicit params function, but using a vararg function can be faster than passing a table to a function depending on how much indexing is involved.
Generally functions aren't a problem though, they only start to become a problem when you start giving them upvalues (each upvalue adds about 2 instructions to the function.
JLua is a lot different to CLua, and I believe it does some JIT compiling, in which case it's impossible to say anything other than CLua almost always runs faster.
#55
Posted 21 July 2013 - 03:16 PM
#56
Posted 11 August 2013 - 06:57 AM
#57
Posted 11 August 2013 - 07:14 AM
Bubba, on 21 July 2013 - 10:53 AM, said:
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











