|
| 09 Jan 2014 03:19 PM |
say I had 5 variables is it better to write each one on a line or,
local a,b,c,d,e = "hi","mexican","robert","justin","kate upton"
or
a = "hi" b = "mexican" c = "robert" d = "justin" e = "kate upton"
and also is stacking ends attractive or just make scripts look bad?
end end end
as apposed to;
end end end
Now obviously these all work and mostly depends on the type of coder youre but Im trying to make my code look neat and very readable. And also is using ; at the end of lines of code good or does it just look bad? I only use it when im scripting one line of code like
repeat wait(); until Workspace:findFirstChild("KateUpton");
I just want other scripters opinions. :) |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 09 Jan 2014 03:23 PM |
1) I prefer variables on their own lines - it's clearer and easier to edit, though you do need to write local multiple times. The only time I do multiple variables on one line is for functions with multiple return values or swapping (a, b = b, a).
2) Ends on 1 line is weird. You wouldn't write "} } } }" in another language. You use ends with indentation to show the end of a block - if they're on 1 line then you can't do that.
3) I use ; on the end of lines, but I don't like it when somebody uses them in a wrong place, e.g. "end;" without it being in the context of something like "local func = function () --[[ code ]] end;" |
|
|
| Report Abuse |
|
|
|
| 09 Jan 2014 03:48 PM |
This is what I am aiming for
local a,b = "De","Pa";
elFunctionDor = function randomState(a,b) local d = math.random(1,2);
if d % 2 == 1 then
return a;
else
return b;
end end
local c = elFunctionDor(); if c == "De" then print("Alagagor"); else print("No Alagagor"); end c = nil;
Keep in mind thats a random code but I will make all functions a variable for easy access I guess you could say, then stack ends, and use ; on the end of variables and returns and prints. |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
|
| 09 Jan 2014 04:23 PM |
Creating several variables on the same line is messy. Especially when you're doing a lot of them. You will end up having to count to get the correct value, and you're most likely going to run into errors anyway.
In fact, you should pretty much never chain such things. For example, if your function return 7 values, and so you use 7 variables on one line to get them all, then you should rather just have the function return a table of the values, and index the table afterwards, much cleaner, and makes the code prettier.
Stacking ends shouldn't be even be an option if you properly indent your code.
function f(bool) if bool then print("POTATOES") else print("OH NOES!") end end
You can clearly see where each scope is, so it's easy to see what belongs where, and where things start and stop.
As for semicolons, that's personal preference. Many programming languages require semicolons, and so putting a semicolon after a statement simply becomes a habit. I personally don't use them, even though I also use a lot of programming languages that do require them, but meh. I feel it looks prettier without them.
Although, there are certain situations where you simply need semicolons. Which where you don't use them, lua will just yell "ambiguous syntax" at your face and call it a day. This happens if you do something like:
f() (f)()
Lua doesn't know if you want to do f()(f)(); or if you want to do f(); (f)(); Because both are perfectly valid, and since lua is otherwise very dynamic and nice with how you format things, it doesn't know what to do in this situation. So, adding a semicolon after the first line will stop the error, or you can also collapse it into one line which will also stop the error. |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 09 Jan 2014 04:34 PM |
I generally define multiple variables on the same line if they're related in some form, like this:
local timeout, timeoutmode = 3, 't' --3 seconds timeout for clients to connect, 't' = total mode
or this:
local band, rshift, char = bit.band, bit.rshift, string.char function getlength(msg) local len = #msg local ret = "" ret = ret..char(band(rshift(n, 24), 0xFF)) ret = ret..char(band(rshift(n, 16), 0xFF)) ret = ret..char(band(rshift(n, 8), 0xFF)) ret = ret..char(band(n, 0xFF)) return ret end
See how those variables are all used in the function? I do that to make it a little easier and take up less space overall. It's not that hard to read if there's less than five variables being defined in the same line, any more than that then I would start chunking it into groups of five or put them all on separate lines. |
|
|
| Report Abuse |
|
|
|
| 09 Jan 2014 04:36 PM |
| So basically just go with what other coders do and don't try and make your own format? I peobably won't stack ends anymore but I willbstackbvariables, only when the script is completely make less lines. I also think ; are overrated I guess but they look cool and make your code look way more complex and makes your look more educated then you already are, same with using underscores in variables I think they look cool |
|
|
| Report Abuse |
|
|
|
| 09 Jan 2014 07:38 PM |
@president Yup, that's how you impress a technology-oblivious person... c:
ᶁᶖᶏᶆᶗᶇᶁᶀᶅᶐᶁᶒ Ⱳᶏᶊ ԣᶒᶉᶒ |
|
|
| Report Abuse |
|
|