Posted 21 April 2012 - 08:06 AM
EDIT: Updated code down below! or http://pastebin.com/0ENkZaH1
The below post occurred in a separate [Question] thread but was OT a bit so I moved it to a new one. I started playing around with CC and Lua two days ago to re-teach me some coding basics. I first started coding 15 years ago when I made TI-83 games and utilities in middle school and then took two C++ courses in high school. Since then I haven't touched a compiler so I'm virtually starting from scratch. I decided to try my hand at creating what I intend to make a fairly indepth program that uses a wide variety of different programming techniques and functions in order to learn along the way. The program will act primarily as a user database with passwords and permissions unique to each user. Once logged in based on the permissions the user will eventually have access to a menu of options (open a specific door, send items through a different pipe, create new users, delete users, change password, etc etc). Currently it is in the fairly early stages but since OminousPenguin stepped up and gave me some advice in another thread I felt now was a good time as any to open the floor up to the professional's critiques and suggestions to make my code more efficient and quell any bad habits! One obvious thing: I know I am not defining any of my variables as local and this is generally bad practice but I'm using it as sort of a debug technique so once the program as crashed I can look behind the scenes using the lua program in CC to check variables / tables etc. If you have a better way of doing this let me know though but I do understand that unless necessary a variable should be defined as local! :)/>/> Also, I'm not necessarily asking you to just make my code better…. I'd prefer it if you started out with theory and pointing out an example of where I could improve and maybe some vague recommendations so I can try and figure the specifics on my own.
Mods: This MAY be the wrong forum to post in? If so sorry and please feel free to move it if you feel necessary! Maybe it should be in the programs section I don't know but I figured most of the Pro's who I want to critique this would be here.
Here is the original post (of which I asked to be continued in here) that started this:
And here is my code in all it's ugly glory:
Some things that I know are wrong / not working as intended that maybe can get the ball rolling. I haven't really tried working / searching for a solution to these as they are secondary or tertiary to me vis a vi functionality so I've held off in favor of larger problems so far so I'm sure a solution wouldn't be THAT difficult to search for and find but I'm adding them here in case you have any ideas
1) in homescreen() lines 8-21: I intend to make a border at the top of the screen made out of *. There has to be a better way of the awkward and manual way I go about it here.
2) in homescreen() line 12: I call os.time() and then print it. This however is Minecraft time and I would prefer it to call the realworld time. I've seen and tried some examples (such as one that accesses a php website and takes the time from there) that didn't work so well .
3) In home() beginning at line 48: I ask it for a key input for a Y/N menu which works, but when it goes to the next io.read() whichever key I pressed is there in the command prompt. Not sure why this is??
I'm more looking for big picture things (Some of OminousPenguin's are great examples; use more loops!!!) that fix bad habits but I just put these up here to spur discussion. Thanks in advance!!
The below post occurred in a separate [Question] thread but was OT a bit so I moved it to a new one. I started playing around with CC and Lua two days ago to re-teach me some coding basics. I first started coding 15 years ago when I made TI-83 games and utilities in middle school and then took two C++ courses in high school. Since then I haven't touched a compiler so I'm virtually starting from scratch. I decided to try my hand at creating what I intend to make a fairly indepth program that uses a wide variety of different programming techniques and functions in order to learn along the way. The program will act primarily as a user database with passwords and permissions unique to each user. Once logged in based on the permissions the user will eventually have access to a menu of options (open a specific door, send items through a different pipe, create new users, delete users, change password, etc etc). Currently it is in the fairly early stages but since OminousPenguin stepped up and gave me some advice in another thread I felt now was a good time as any to open the floor up to the professional's critiques and suggestions to make my code more efficient and quell any bad habits! One obvious thing: I know I am not defining any of my variables as local and this is generally bad practice but I'm using it as sort of a debug technique so once the program as crashed I can look behind the scenes using the lua program in CC to check variables / tables etc. If you have a better way of doing this let me know though but I do understand that unless necessary a variable should be defined as local! :)/>/> Also, I'm not necessarily asking you to just make my code better…. I'd prefer it if you started out with theory and pointing out an example of where I could improve and maybe some vague recommendations so I can try and figure the specifics on my own.
Mods: This MAY be the wrong forum to post in? If so sorry and please feel free to move it if you feel necessary! Maybe it should be in the programs section I don't know but I figured most of the Pro's who I want to critique this would be here.
Here is the original post (of which I asked to be continued in here) that started this:
@OminousPenguin -
As this program is MOSTLY (although having a neat little user database on my server is also useful) to teach me to program after a 15 year hiatus I welcome any and all suggestions / tips to improve efficiency or what have you! I was going to actually create a thread asking for just pure and simple recommendations such as this as I'm always a fan of limiting the focus of individual threads but thank you for your input none the less. But while we're here lets go down your suggestions:
1) I see your point and calling a function too often with no actual change or results is never a good idea and it definitely seems that way now, such as here:Obviously homescreen() has just been called at the beginning of home() and then immediately after in newuser() without any screen changes, however I plan to add in functionality that will call newuser() (and other such functions) at other times which will require a homescreen() flush so to speak. Granted I may have missed an occurrance that is currently and will forever be superfluous so feel free to cite specific example! Or maybe I just missed your point entirely :)/>/> Aside: It may look strange because it wasn't till after I wrote most of this code that I realized you do not have to call a function immediately after ending it (derp!) which is how every example worked in the tutorial I looked at so the function organization is a bit funky. If I were to re write this code (and I probably will do this at some point) I would have functions such as newuser() and login() defined outside of any other functions.Spoiler
function home() homescreen() if #userlist == 0 and status == "Logged Out" then admin = true function newuser() homescreen() if admin == true then print("Please register Admin:") write("Enter Username:") else print("Please register User:") write("Enter Username:") end
2) Thank's for the heads up! I had not noticed that (mostly because the code as it was wouldn't even run that far and I was preoccupied with the previous error!) but it is now fixed! login() is now called after the nil check and it works as intended (for the most part; see earlier section to MysticT about new bug…)
3) Valid point. changed the call to x in line 85 to 3-a which is all x is anyways as you point out.
4) I'm guessing you mean that some of my if statements could be made shorter with a for or while loop? Correct me if I'm wrong. If that is the case, in my mind the if statement seems simpler I suppose? But lets challenge that. Give me an example of where you would use a loop instead of what I have but don't say anything about how you would implement and I will challenge myself to make it more efficient by using a loop (best way for me to learn eh?)
Again thanks for your suggestions! I always welcome constructive criticism! Especially early on to help defeat long term bad habits. I might make this into a separate "Tear my code apart!!" thread in order to avoid OT discussions so reply there if you could!
And here is my code in all it's ugly glory:
Spoiler
version = "v.01"
userlist = {}
status = "Logged Out"
currentuser = "New User"
a=0
function homescreen()
term.clear()
term.setCursorPos(1,0)
print(string.rep("*", 50))
print("*Running OutSec "..version.." ".. textutils.formatTime( os.time(), false)) --currently displays Minecraft time, not real time
print("*Status: "..status)
if status == "Logged In"
then
term.setCursorPos(20,3)
print("User: " ..currentuser)
end
print(string.rep("*", 50))
term.setCursorPos(1,10)
end
homescreen()
function home()
homescreen()
if #userlist == 0 and status == "Logged Out"
then
admin = true
function newuser()
homescreen()
if admin == true
then
print("Please register Admin:")
write("Enter Username:")
else
print("Please register User:")
write("Enter Username:")
end
newusername = io.read()
write("Enter New Password:")
newpassword = io.read()
homescreen()
print("Username: "..newusername)
print("Password: "..newpassword)
print("Confirm?[Y/N]")
local event, k1 = os.pullEvent("key")
if k1 == 21 --y
then
table.insert(userlist, newusername)
userlist[newusername] = {}
table.insert(userlist[newusername], "password")
userlist[newusername].password = newpassword
table.insert(userlist[newusername], "adminstatus")
userlist[newusername].adminstatus = admin
home()
elseif k1 == 49 then --n
newuser()
end
end
newuser()
elseif #userlist > 0 and status == "Logged Out" then
function login()
homescreen()
write("Please enter Username:")
loginid = io.read()
if userlist[loginid] == nil
then
print("Invalid Username. Please try again.")
sleep(2)
login()
end
end
login()
function password()
homescreen()
if a>0
then
term.setCursorPos(1,9)
print("Incorrect Password. ("..3-a.."/3 attempts remaining.)")
end
write("Please enter password:")
loginpass = io.read()
if loginpass == userlist[loginid].password
then
status = "Logged In"
currentuser = loginid
print("Entry Authorized.")
sleep(2)
homescreen()
else
a = a+1
if a < 3
then
password()
else
a=0
homescreen()
print("Too many failed attempts. Please wait.")
sleep(2)
login()
end
end
end
password()
end
end
home()
Some things that I know are wrong / not working as intended that maybe can get the ball rolling. I haven't really tried working / searching for a solution to these as they are secondary or tertiary to me vis a vi functionality so I've held off in favor of larger problems so far so I'm sure a solution wouldn't be THAT difficult to search for and find but I'm adding them here in case you have any ideas
1) in homescreen() lines 8-21: I intend to make a border at the top of the screen made out of *. There has to be a better way of the awkward and manual way I go about it here.
2) in homescreen() line 12: I call os.time() and then print it. This however is Minecraft time and I would prefer it to call the realworld time. I've seen and tried some examples (such as one that accesses a php website and takes the time from there) that didn't work so well .
3) In home() beginning at line 48: I ask it for a key input for a Y/N menu which works, but when it goes to the next io.read() whichever key I pressed is there in the command prompt. Not sure why this is??
I'm more looking for big picture things (Some of OminousPenguin's are great examples; use more loops!!!) that fix bad habits but I just put these up here to spur discussion. Thanks in advance!!