Jump to content




Eight Good Coding Habits


  • You cannot reply to this topic
56 replies to this topic

#41 Ferdi265

  • Members
  • 21 posts

Posted 27 March 2013 - 03:49 AM

I follow most of these habits, especially the one about indenting my code (oCd seems to help with coding)

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 airtonix

  • Members
  • 18 posts

Posted 28 March 2013 - 08:56 PM

I wonder if Lua has the equivilant of Pythons PEP8.

My contribution to this would be to say that any code that does not have unit tests is broken by design.

#43 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 28 March 2013 - 09:00 PM

View Postairtonix, on 28 March 2013 - 08:56 PM, said:

I wonder if Lua has the equivilant of Pythons PEP8.

My contribution to this would be to say that any code that does not have unit tests is broken by design.
http://lua-users.org/wiki/UnitTesting

#44 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 11 April 2013 - 12:53 PM

One thing I see people doing a lot is:

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 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 11 April 2013 - 03:42 PM

View PostDlcruz129, on 11 April 2013 - 12:53 PM, said:

One thing I see people doing a lot is:

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 ;)
I would agree except in this case:

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 :P

#46 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 11 April 2013 - 04:10 PM

View PostSuicidalSTDz, on 11 April 2013 - 03:42 PM, said:

View PostDlcruz129, on 11 April 2013 - 12:53 PM, said:

One thing I see people doing a lot is:

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 ;)
I would agree except in this case:

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 :P

Yeah, but for simple single-color textboxes and whatnot, multi-line strings are the best way to go.

#47 airtonix

  • Members
  • 18 posts

Posted 14 July 2013 - 06:52 PM

I sometimes wish there was a unittest framework for computercraft, but considering how the testable outputs would involve an object moving around in 3d gamespace, I can imagine how large a task it would be to create such a framework.

#48 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 14 July 2013 - 09:06 PM

View Postairtonix, on 14 July 2013 - 06:52 PM, said:

I sometimes wish there was a unittest framework for computercraft, but considering how the testable outputs would involve an object moving around in 3d gamespace, I can imagine how large a task it would be to create such a framework.

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 strayzer

  • New Members
  • 1 posts

Posted 20 July 2013 - 02:38 PM

considered myself a proficient coder until i read this and realized i am guilty of every single habit. Another good practice that I would recommend is ALWAYS use functions and name them correctly.I often find that even with simple code i confuse myself trying to figure out which function does what as i have the terrible habit of naming them the letter that my finger is hovering over at the time. :wacko:

#50 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 20 July 2013 - 06:20 PM

View Poststrayzer, on 20 July 2013 - 02:38 PM, said:

ALWAYS use functions

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 )
end
Of 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 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 20 July 2013 - 10:58 PM

View PostEngineer, on 20 July 2013 - 06:20 PM, said:

View Poststrayzer, on 20 July 2013 - 02:38 PM, said:

ALWAYS use functions

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 )
end
Of 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 Fifi

  • Members
  • 6 posts

Posted 21 July 2013 - 05:50 AM

Well, I think using functions even if not necessary is acceptable only when the code don't have to be 100% optimized and the code simplicity would extremly suffer when everything will be write in a single code block. Although I still prefer vertical separation and comments instead of faking the need for refactorization. It's not a good practice to make a function of everything.

#53 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 21 July 2013 - 10:53 AM

If by optimized, people mean short, I would have to disagree. To me, optimization is a combination of readability, number of operations, and ease of customization later on. In Lua especially, there is no reason to program only in one block of code for non-repetitive tasks simply because you may at one point want to make the task repetitive and functions make that extremely easy. Also, functions have essentially no effect on speed in CC Lua, and I rather doubt that they have much of effect in a regular Lua environment either.

#54 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 21 July 2013 - 12:07 PM

View PostBubba, on 21 July 2013 - 10:53 AM, said:

If by optimized, people mean short, I would have to disagree. To me, optimization is a combination of readability, number of operations, and ease of customization later on. In Lua especially, there is no reason to program only in one block of code for non-repetitive tasks simply because you may at one point want to make the task repetitive and functions make that extremely easy. Also, functions have essentially no effect on speed in CC Lua, and I rather doubt that they have much of effect in a regular Lua environment either.
By optimised I mean goes fast.
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 Tjakka5

  • Members
  • 256 posts

Posted 21 July 2013 - 03:16 PM

W00T, I got perfect habits!

#56 willdude123

  • Members
  • 8 posts

Posted 11 August 2013 - 06:57 AM

Good tips- goes for all coding, not just CC

#57 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 11 August 2013 - 07:14 AM

View PostBubba, on 21 July 2013 - 10:53 AM, said:

If by optimized, people mean short, I would have to disagree. To me, optimization is a combination of readability, number of operations, and ease of customization later on.
Agreed. yet at the same time disagreed... There are times where good readability can make a large negative impact on the performance of a program. I definitely agree that programs should be readable to an extent, however sometimes readability must be sacrificed slightly for a more optimal solution... Also I have found that in most cases more optimised code is in fact shorter, and mostly still nice and readable, so I can definitely understand peoples confusion/correlation between optimised and lines of code. Normally things like number of operations is the main aspect of optimised code. One last thing is readability cannot really go into the general criteria of optimised programming as, well, everyone is different. My idea of readable code I can guarantee would be very different to yours, or most other peoples... Most people have their own "flare" and programming style that is guaranteed that others will not find readable... This is the sole purpose behind development companies implementing and forcing their employees to use specific naming conventions, habits and practises...





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users