Eight Good Coding Habits
#21
Posted 30 January 2013 - 07:42 PM
#22
Posted 30 January 2013 - 11:51 PM
#23
Posted 31 January 2013 - 11:42 AM
It allows for better highlighting and also helps to enforce all of these 8 coding principles.
#24
Posted 01 February 2013 - 10:05 AM
#25
Posted 02 February 2013 - 12:24 PM
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
Posted 02 February 2013 - 01:52 PM
Grim Reaper, on 02 February 2013 - 12:24 PM, said:
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.
#28
Posted 02 February 2013 - 01:57 PM
PixelToast, on 02 February 2013 - 01:54 PM, said:
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
#30
Posted 03 February 2013 - 01:32 AM
#31
Posted 03 February 2013 - 03:46 PM
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
Posted 08 February 2013 - 08:56 AM
#33
Posted 26 February 2013 - 03:12 PM
#34
Posted 05 March 2013 - 11:53 AM
baeshra, on 26 February 2013 - 03:12 PM, said:
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
Posted 05 March 2013 - 12:37 PM
shiphorns, on 05 March 2013 - 11:53 AM, said:
shiphorns, on 05 March 2013 - 11:53 AM, said:
shiphorns, on 05 March 2013 - 11:53 AM, said:
#36
Posted 06 March 2013 - 05:57 AM
TheOriginalBIT, on 05 March 2013 - 12:37 PM, said:
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.
TheOriginalBIT, on 05 March 2013 - 12:37 PM, said:
TheOriginalBIT, on 05 March 2013 - 12:37 PM, said:
shiphorns, on 05 March 2013 - 11:53 AM, said:
#37
Posted 06 March 2013 - 09:10 AM
#38
Posted 08 March 2013 - 12:04 PM
#39
Posted 08 March 2013 - 04:40 PM
Though 8 is mostly because I approve of gotos lol
shiphorns, on 06 March 2013 - 05:57 AM, said:
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
Posted 09 March 2013 - 01:11 AM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











