Jump to content




Help (again) cleaning up a code

lua

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

#1 Himself12794

  • Members
  • 56 posts

Posted 06 February 2014 - 10:33 PM

So I made another code for my turtle OS, and, again, just know that it can be made so much shorter and so much more nicer looking. I finished the information gathering functions, and I think they look horrible (at least they work!), so once I again I ask you all what I can do to improve it, things I can do to make it shorter, etc.

The reason I always ask is because all of my coding is self-taught and I have no idea what a professional code should look like.

Here's the code, which gathers the information needed for constructing an Industrial Blast Furnace:
Spoiler

Most of my focus is on making the code shorter and simpler, but input on code organization is fully welcome as well.
Hopefully, based on the repeated input I get, I'll need less and less help, and perhaps can provide some of my own.
Thanks in advance!

Edited by Himself12794, 06 February 2014 - 10:37 PM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 February 2014 - 12:15 AM

theres things throughout that you can fix. These are the 5 6 things that stood out to me immediately when I had a quick scan of your code.

#1 is localising your variables

#2 your for loops
local i=1
for i, 16 do
  count=count+turtle.getItemCount(i4)
end
since you do not use i outside the for loop once it is complete there is no need to perform a forward declaration. if you do it like so
for i = 1, 16 do
  count=count+turtle.getItemCount(i4)
end
i is localised to the for loop.

#3 This
if dichotomy('Yes','No') then
  diggingMode=true
else
  diggingMode=false
end
can simply be this
diggingMode = dichotomy('Yes', 'No')
conditionals are booleans, so why test a conditional to then assign a boolean, its just waste of a fork and CPU cycles.

#4 while waiting for the user to input items you can actually wait for the turtle_inventory event (assuming you're using CC 1.55 or above)

#5 these
if event=="key" and key==203 then sel=not sel end
if event=="key" and key==205 then sel=not sel end
if event=="key" and key==28 then break end
could actually just be
if event == "key" then
  if key == 203 or key == 205 then
	sel = not sel
  else if key == 28 then
	break
  end
end
this reduces the amount of conditionals that need to be evaluated by removing unnecessary event checking.

EDIT: Found another one
#6 this
if casings>34 and heat>3880 then
  write(3880)
  write(' K')
  write('  Too many casings! Only 34!')
elseif casings>34 and heat<=3880 then
  write(heat)
  write(' K')
  write('  Too many casings! Only 34!')
elseif casings<34 then
  write(heat)
  write(' K')
  write('  Not enough casings! 34 Needed!')
else
  write(heat)
  write(' K')
end
can be simplified to the following
write(math.min(heat, 3880)..' K')
if casings > 34 then
  write('  Too many casings! Only 34!')
elseif casings < 34 then
  write('  Not enough casings! 34 Needed!')
end

Edited by theoriginalbit, 07 February 2014 - 12:33 AM.


#3 Himself12794

  • Members
  • 56 posts

Posted 07 February 2014 - 12:45 AM

View Posttheoriginalbit, on 07 February 2014 - 12:15 AM, said:

theres things throughout that you can fix. These are the 5 6 things that stood out to me immediately when I had a quick scan of your code.

#1 is localising your variables

#2 your for loops
local i=1
for i, 16 do
  count=count+turtle.getItemCount(i4)
end
since you do not use i outside the for loop once it is complete there is no need to perform a forward declaration. if you do it like so
for i = 1, 16 do
  count=count+turtle.getItemCount(i4)
end
i is localised to the for loop.

#3 This
if dichotomy('Yes','No') then
  diggingMode=true
else
  diggingMode=false
end
can simply be this
diggingMode = dichotomy('Yes', 'No')
conditionals are booleans, so why test a conditional to then assign a boolean, its just waste of a fork and CPU cycles.

#4 while waiting for the user to input items you can actually wait for the turtle_inventory event (assuming you're using CC 1.55 or above)

#5 these
if event=="key" and key==203 then sel=not sel end
if event=="key" and key==205 then sel=not sel end
if event=="key" and key==28 then break end
could actually just be
if event == "key" then
  if key == 203 or key == 205 then
	sel = not sel
  else if key == 28 then
	break
  end
end
this reduces the amount of conditionals that need to be evaluated by removing unnecessary event checking.

EDIT: Found another one
#6 this
if casings>34 and heat>3880 then
  write(3880)
  write(' K')
  write('  Too many casings! Only 34!')
elseif casings>34 and heat<=3880 then
  write(heat)
  write(' K')
  write('  Too many casings! Only 34!')
elseif casings<34 then
  write(heat)
  write(' K')
  write('  Not enough casings! 34 Needed!')
else
  write(heat)
  write(' K')
end
can be simplified to the following
write(math.min(heat, 3880)..' K')
if casings > 34 then
  write('  Too many casings! Only 34!')
elseif casings < 34 then
  write('  Not enough casings! 34 Needed!')
end
Oh yes! That's exactly the type input I'm looking for. I'm going to start looking at more advanced users codes and perhaps I can learn some styling lessons. I'm gonna have to start crediting you in my codes that you help with.
Also, is there a way I can do the program without having to declare this twice?
    local count={
        standard=turtle.getItemCount(1),
        reinforced=turtle.getItemCount(2),
        advanced=turtle.getItemCount(3),
        lava=turtle.getItemCount(4)+turtle.getItemCount(5)==2,
        nichrome=turtle.getItemCount(6)>=4,
        kanthal=turtle.getItemCount(7)>=4
    }
I tried passing it out of the function like I did with heat, but it did not work, even when I removed the local statement from in front of it.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 February 2014 - 12:49 AM

View PostHimself12794, on 07 February 2014 - 12:45 AM, said:

I'm gonna have to start crediting you in my codes that you help with.
technically not needed since anyone who helps in Ask a Pro is freely giving help and advice, if they want credit they shouldn't be helping. but thank you for the sentiment.

View PostHimself12794, on 07 February 2014 - 12:45 AM, said:

Also, is there a way I can do the program without having to declare this twice?
Yeah of course, just define it at the top of your program — just as you did with diggingMode. any function below its definition will have access to it.

#5 Himself12794

  • Members
  • 56 posts

Posted 07 February 2014 - 12:55 AM

View Posttheoriginalbit, on 07 February 2014 - 12:49 AM, said:

View PostHimself12794, on 07 February 2014 - 12:45 AM, said:

Also, is there a way I can do the program without having to declare this twice?
Yeah of course, just define it at the top of your program — just as you did with diggingMode. any function below its definition will have access to it.
I was going to do that, but if I do I can't get the update if the amount of items change. I have it so it changes every time there is a difference in the inventory.

Edited by Himself12794, 07 February 2014 - 12:57 AM.


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 February 2014 - 01:03 AM

View PostHimself12794, on 07 February 2014 - 12:55 AM, said:

I was going to do that, but if I do I can't get the update if the amount of items change. I have it so it changes every time there is a difference in the inventory.
you could make a little function to refresh the values inside of the table, which you invoke when you want the latest info.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users