Jump to content




Eight Good Coding Habits


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

#21 shiphorns

  • Members
  • 50 posts
  • LocationBoston, MA

Posted 30 January 2013 - 07:42 PM

When I got started last week programming the mining turtles, I realized very quickly that one important habit is to ALWAYS handle all possible return values of the API calls. The difference between a robust turtle script and a fragile one is how thoroughly all the contingency cases are handled. I see a lot of new coders just calling the functions like turtle.forward() and just hoping they succeed, rather than checking that they do...

#22 Frederikam

  • Members
  • 112 posts
  • LocationDenmark

Posted 30 January 2013 - 11:51 PM

Code Kiddie (That title fits my coding very well) approves this.

#23 digipenguin

  • Members
  • 15 posts

Posted 31 January 2013 - 11:42 AM

I also have to strongly reccommend that you use an external editor, such as Notepad++.

It allows for better highlighting and also helps to enforce all of these 8 coding principles.

#24 pooluke41

  • New Members
  • 1 posts

Posted 01 February 2013 - 10:05 AM

This thread is gonna help me alot. :wub:

#25 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 02 February 2013 - 12:24 PM

Something that I didn't see on this list, but I think should definitely be there is how pertinent variable and method names are in a script.

This rule kind of goes without saying, but a lot of people seem to write code which has very simple or just entirely irrelevant variable and function names (like 'x' or 'derp', for example) in their scripts. Using a relevant naming convention improves readability a ton and also helps the programmer to understand their own code better.

Also, a naming convention such as camel-cap for variable/function names is usually a good idea. However, naming conventions/styles are entirely up to the programmer.

#26 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 02 February 2013 - 01:52 PM

View PostGrim Reaper, on 02 February 2013 - 12:24 PM, said:

Something that I didn't see on this list, but I think should definitely be there is how pertinent variable and method names are in a script.

This rule kind of goes without saying, but a lot of people seem to write code which has very simple or just entirely irrelevant variable and function names (like 'x' or 'derp', for example) in their scripts. Using a relevant naming convention improves readability a ton and also helps the programmer to understand their own code better.

Also, a naming convention such as camel-cap for variable/function names is usually a good idea. However, naming conventions/styles are entirely up to the programmer.

I changed #3 from "Always return a value" to "name your functions/variables something that makes sense". Now that I think about it, returning a value is not really always needed.

#27 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 02 February 2013 - 01:54 PM

when i cant think of a name i just find an achronym or short name for it
like this:
local Mx,My=term.getSize()
local Cx,Cy=math.floor(Mx/2),math.floor(My/2)
Max X,Max Y
Center X, Center Y
see here (at the beginning of all render functions)

#28 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 02 February 2013 - 01:57 PM

View PostPixelToast, on 02 February 2013 - 01:54 PM, said:

when i cant think of a name i just find an achronym or short name for it
like this:
local Mx,My=term.getSize()
local Cx,Cy=math.floor(Mx/2),math.floor(My/2)
Max X,Max Y
Center X, Center Y

I do that too at times, but when it comes to bigger projects I like my variables/functions to be named pretty clearly. The time it takes to type them out is negligible in comparison to the time it takes to read an entire function again in order to figure out its purpose :)

#29 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 02 February 2013 - 02:13 PM

its universal in that case, for other things i have more descriptive names
local tpe,id,data,dist=os.pullEvent()
local rData=infutil.decompress(data) or {}
see here
its still confusing if you dont know what it does though >_>

#30 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 03 February 2013 - 01:32 AM

personally I just use the shortest pertinent names possible, they often do not make much sense but it makes coding it a lot faster and easier for me as I tend to think faster than I type/speak which confuses things sometimes. afterwards I just replace all instances with correct names

#31 KillaVanilla

  • Members
  • 303 posts

Posted 03 February 2013 - 03:46 PM

A tip I've had to learn the hard way several times over: Organize your code. Especially your function definitions/bodies. Keep them close together. Seriously, if you're working on one part of your program, you're gonna want to keep those parts close by and organized; if you spend 30 seconds just trying to find a function, you need to fix that. Also, put most of your program-wide variables at the very top (or at least close to it). Control+X/Control+V is all you need for this.

For example, instead of having:
local frequency = 0
local function receive()
    ...
    ...
    ...
    ...
    ...
    ...
end

local function escape()
    ...
    ...
    ...
    ...
    ...
    ...
end
local ingredients = {}

local function wheee()
    ...
    ...
    ...
    ...
    ...
    ...
end

-- insert 350 lines of other functions here


local function makeCake()
    ...
    ...
    ...
    ...
    ...
    ...
end

local function send(freq)
    ...
    ...
    ...
    ...
    ...
    ...
end

local function blam()
    ...
    ...
    ...
    ...
    ...
    ...
end
local cakes = {}
have:

local cakes = {}
local ingredients = {}
local frequency = 0
local function makeCake()
    ...
    ...
    ...
    ...
    ...
    ...
end

local function send(freq)
    ...
    ...
    ...
    ...
    ...
    ...
end
local function receive()
    ...
    ...
    ...
    ...
    ...
    ...
end

local function escape()
    ...
    ...
    ...
    ...
    ...
    ...
end

local function wheee()
    ...
    ...
    ...
    ...
    ...
    ...
end
local function blam()
    ...
    ...
    ...
    ...
    ...
    ...
end
-- insert 350 lines of other functions here


#32 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 08 February 2013 - 08:56 AM

A text editor with code folding helps tremendously in that regard as well--I can simply fold the functions I am not working with and easily concentrate on the ones I am working with. Especially helpful when there are common functions needed by multiple groups of other functions, making them unable to be very close to all of the functions that need it.

#33 baeshra

  • Members
  • 27 posts

Posted 26 February 2013 - 03:12 PM

As a beginner in this field I know I have an odd coding style. How do most people organize their longer scripts? I tend to lay my code out under functions which allows me to re-order it easily, is there a better way to go about this?

#34 shiphorns

  • Members
  • 50 posts
  • LocationBoston, MA

Posted 05 March 2013 - 11:53 AM

View Postbaeshra, on 26 February 2013 - 03:12 PM, said:

As a beginner in this field I know I have an odd coding style. How do most people organize their longer scripts? I tend to lay my code out under functions which allows me to re-order it easily, is there a better way to go about this?

It depends on what scale of organization you're talking about. In a small program, it makes sense to put code in a function to organize it, especially if it's called from more than one place. Lua is a procedural programming language, so it is natural to organize using functions. There are some people experimenting with pseudo-OOP organization, but this is largely an academic challenge for them, and it's a bit of banging a square peg into a round hole if you ask me. That is to say, Lua was not designed for OOP, so the results of that experimentation tend to be ugly and cumbersome manipulation of metatables (reminds me a bit of ActionScript 1.0 OOP attempts with prototypes). You shouldn't go that route IMHO. If you're working on a really large project, then at some point it may be worth your while to identify code than you'll want to reuse in other programs, extract it into code libraries that you include with require, or if they are really generally-useful functions that you can generalize to be not specific to your programs, make them into APIs for you or others to use (if you have the access to install lua files on your server).

#35 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 March 2013 - 12:37 PM

View Postshiphorns, on 05 March 2013 - 11:53 AM, said:

That is to say, Lua was not designed for OOP, so the results of that experimentation tend to be ugly and cumbersome manipulation of metatables
It actually WAS designed for OO, that is why we can manipulate metatables.


View Postshiphorns, on 05 March 2013 - 11:53 AM, said:

You shouldn't go that route IMHO.
I'm assuming you're another one of those people that prefers procedural programming over OO any day?

View Postshiphorns, on 05 March 2013 - 11:53 AM, said:

extract it into code libraries that you include with require
Doesn't exist, don't suggest things to newbies that don't exist, just causes headaches for us all later http://puu.sh/2cdch

#36 shiphorns

  • Members
  • 50 posts
  • LocationBoston, MA

Posted 06 March 2013 - 05:57 AM

View PostTheOriginalBIT, on 05 March 2013 - 12:37 PM, said:

View Postshiphorns, on 05 March 2013 - 11:53 AM, said:

That is to say, Lua was not designed for OOP, so the results of that experimentation tend to be ugly and cumbersome manipulation of metatables
It actually WAS designed for OO, that is why we can manipulate metatables.

No, it was not "designed for OO", it was designed to be very flexible. This is why Lua does not have built-in syntax for classes, inheritance, encapsulation and access modifiers, etc. You can implement many aspects of object oriented design, but it's cumbersome and you have to reinvent the wheel a bit to do it, writing the OOP infrastructure yourself. While I think that many people consider Lua capable of supporting OOP concepts, it's a bit misleading to say that it was designed for OOP.

View PostTheOriginalBIT, on 05 March 2013 - 12:37 PM, said:

View Postshiphorns, on 05 March 2013 - 11:53 AM, said:

You shouldn't go that route IMHO.
I'm assuming you're another one of those people that prefers procedural programming over OO any day?
No sir, I'm a game UI programmer by trade and I mostly work in C++ and AS 3.0. I'm an OOP guy through and through, and only use Lua when I have to.

View PostTheOriginalBIT, on 05 March 2013 - 12:37 PM, said:

View Postshiphorns, on 05 March 2013 - 11:53 AM, said:

extract it into code libraries that you include with require
Doesn't exist, don't suggest things to newbies that don't exist, just causes headaches for us all later http://puu.sh/2cdch
Ugh, that is unfortunate. Hopefully the CC guys are working on this, but it does explain why we have so many programs here that are giant 1-file monoliths.

#37 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 06 March 2013 - 09:10 AM

There are dofile and os.loadAPI

#38 Roadhouse699

  • Members
  • 25 posts
  • LocationThe End, shooting the ender dragon because fantasy sucks.

Posted 08 March 2013 - 12:04 PM

I feel so good about myself, I do almost all this stuff!

#39 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 08 March 2013 - 04:40 PM

I highly agree with points 6 and 8.
Though 8 is mostly because I approve of gotos lol

View Postshiphorns, on 06 March 2013 - 05:57 AM, said:

No sir, I'm a game UI programmer by trade and I mostly work in C++ and AS 3.0. I'm an OOP guy through and through, and only use Lua when I have to.

Ugh, that is unfortunate. Hopefully the CC guys are working on this, but it does explain why we have so many programs here that are giant 1-file monoliths.

What sort of libraries do you use out of interest?
I've used windows forms, but beyond that I've only used the ones I've written for use in XNA games.

There is OS.loadAPI. If that doesn't suit you, you can always write your own API loader/importer. Since code files in lua are run as functions I make pretty much all my APIs return a local table that the user can assign to whatever variable they wish (like you can in C#, sort of).
Btw, you'd hate my monoliths. Thank god lua doesn't have to compile.

I still have to disagree about lua not being used for OO though. Sure it's not true OO without a load of mangling or having access to the backend to implement classes, but if you aren't using and treating tables like objects, you're missing out on a lot of that flexibility and structure (if you look beyond the fact a flexible structure is a bit of an oxymoron).

#40 3ydney

  • Members
  • 53 posts
  • LocationAdelaide, Australia.

Posted 09 March 2013 - 01:11 AM

Great tips Bubba!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users