This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Engineer's profile picture

What is better?

Started by Engineer, 20 March 2013 - 02:49 AM
Engineer #1
Posted 20 March 2013 - 03:49 AM
Hello,

I am creating an OS (Who isnt?), but I am miles away from complete. I ask for your opinion, do I need to make 1 big file of messy code, or organized code and sub programs that run from the main?

Please leave your opinion about this, and don't forget to do the poll!

Also, what do you want to see back in my OS?

- Engineer
PixelToast #2
Posted 20 March 2013 - 03:53 AM
i would prefer a single file, and then separate files for apps, apis, etc
Engineer #3
Posted 20 March 2013 - 04:17 AM
i would prefer a single file, and then separate files for apps, apis, etc
Hmm.. I am considering that, lets wait on more suggestions :P/>
robhol #4
Posted 20 March 2013 - 04:17 AM
My approach in my GUI system was to do all the programming in separate files because it's neater that way. I then wrote a shell script that glues it all together.
theoriginalbit #5
Posted 20 March 2013 - 06:56 AM
The best method would be to have main program, APIS that do most of the leg work for main program and then programs in separate files that are the applications to run.

To put in an example.
SpoilerCCTube files:
Main File >>>>
  • cctube : 160 lines of code
APIS >>>>
  • ccConfig : 230 lines of code
  • CCTFiles : 35 lines
  • CCTGui : 1090 lines
  • CCTHelp : 50 lines
  • CCTLogger : 64 lines
  • CCTNetworking : 244 lines
  • CCTState : 97 lines
  • CCTStream : 273 lines
  • CCTUpdate : 51 lines
  • CCTUtils : 267 lines
  • CCTVideoReader : 199
  • Controls : 1259
  • Crc : 87
  • DeltaBuffer : 78
  • FrameImage : 255
  • FrameRenderer : 64
  • Json : 673
  • Loader : 45
  • PacketStream : 195
  • StreamWatcher : 75
  • StringX : 798
So as you can see, there are lots of APIs, lots of high level abstractions there, breaking functionality down to its simplest and most reusable code, to the point that the main program file has very little code.
Actually its more like 61 lines of code the other 99 is just a default program template that all my programs use that implement things like Blue Screen of Death and some basic functions that all my programs use.
Mads #6
Posted 20 March 2013 - 07:05 AM
Have one file, which controls everything, then have other files, like APImanager.lua or Hardware.lua or something, and then include them in the main file. Look at any game written in Lua(non-CC), and you will see what I mean.
Engineer #7
Posted 20 March 2013 - 07:27 AM
I think I will go with BIT's approach
Sammich Lord #8
Posted 20 March 2013 - 07:30 AM
Think of it as a game. When you make a game you aren't going to put everything in one file. You are going to have a file for everything you want to do.

You can also think of it as web development. If I want to connect to MySQL constantly I will just include a file that runs all the functions that it requires to connect to MySQL.

So overall, it is better to have one file to control other files.
Noodle #9
Posted 20 March 2013 - 07:37 AM
Well
In native Lua the "OOP" is easier.
In this version of Lua.. CCLua you have to do os.loadAPI()

Native:

-- File 1
require file2
file2.blah()
-- File 2
file2 = {}
function file2.blah()

end

CCLua

-- File 1
file2 = os.loadAPI("file2")
file2.blah()
-- File2
function blah()

end
Well at least I think that's right..
Engineer #10
Posted 20 March 2013 - 07:39 AM
Well
In native Lua the "OOP" is easier.
In this version of Lua.. CCLua you have to do os.loadAPI()

Native:

-- File 1
require file2
file2.blah()
-- File 2
file2 = {}
function file2.blah()

end

CCLua

-- File 1
file2 = os.loadAPI("file2")
file2.blah()
-- File2
function blah()

end
Well at least I think that's right..

In CCLua you can do:

os.loadAPI( "myAPI" )
myAPI.myfunction()
PixelToast #11
Posted 20 March 2013 - 07:41 AM
:o/> havent seen you in awhile noodle
i never made a program so huge that it needed multiple files, i generally just use apis instead of splitting the code up
Noodle #12
Posted 20 March 2013 - 07:44 AM
Well
In native Lua the "OOP" is easier.
In this version of Lua.. CCLua you have to do os.loadAPI()

Native:

-- File 1
require file2
file2.blah()
-- File 2
file2 = {}
function file2.blah()

end

CCLua

-- File 1
file2 = os.loadAPI("file2")
file2.blah()
-- File2
function blah()

end
Well at least I think that's right..

In CCLua you can do:

os.loadAPI( "myAPI" )
myAPI.myfunction()
Ahh, didn't know..
theoriginalbit #13
Posted 20 March 2013 - 07:46 AM
i never made a program so huge that it needed multiple files, i generally just use apis instead of splitting the code up
But really, what is an api in the context of CCLua but code that was split up from main code to run particular tasks.
Engineer #14
Posted 20 March 2013 - 07:56 AM
Is it weird if an API loads another API? :S
theoriginalbit #15
Posted 20 March 2013 - 07:59 AM
Is it weird if an API loads another API? :S
Not at all. in cctube that is the entire purpose of Loader. it loads all the apis in the correct order (since some require others), based off the files listed in CCTFiles.
Engineer #16
Posted 20 March 2013 - 08:04 AM
Is it weird if an API loads another API? :S
Not at all. in cctube that is the entire purpose of Loader. it loads all the apis in the correct order (since some require others), based off the files listed in CCTFiles.

Hmm.. I have like one API that uses 2 APIS. Should I put before the functions the loadAPI( "name" ) and then after all the functions unloadAPI( "name" )?
I dont really know but I think it should not work because you call functions, not the programs.

This is my last question for now, I will ask questions here or in ask a pro if I get stuck again.
theoriginalbit #17
Posted 20 March 2013 - 08:08 AM
Hmm.. I have like one API that uses 2 APIS. Should I put before the functions the loadAPI( "name" ) and then after all the functions unloadAPI( "name" )?
I dont really know but I think it should not work because you call functions, not the programs.

This is my last question for now, I will ask questions here or in ask a pro if I get stuck again.
no need to unload really. but there is no problems loading multiple apis from another. sometimes it is required. however, just load all your apis from your main program, all other apis will have access to them as they are loaded into _G then if you really wish unload them all at the end of your main program you can do so. The only other time you would ever need to unload an api is if you were reloading the api after say an update or something, instead of rebooting the computer.
nutcase84 #18
Posted 20 March 2013 - 08:15 AM
Do whatever is easier. I have one main file for Nut OS(other than the img. files and programs like firewolf), and it's still nice and clean(to me, anyway).
kintick #19
Posted 20 March 2013 - 08:20 AM
In general, having multiple files is better as if you do any bug-fixes then only the specific files need to be re-downloaded.

also it helps split up your code so if you have:

- one file for the user account administrations,
- one file for program handling
- one file…

it means that you know where each bit is so you don't need to do a lot of Ctrl-F ing to find parts of the code you require…

having multiple files has really helped me. it really depends on your individual coding style and the project in question,

for an OS i recommend many files, for a simple program that say runs a printer etc… one file would be better.

also for something like paste-bin, one file is better.

play around with different things and I hope that your project goes well :)/>
Engineer #20
Posted 20 March 2013 - 08:37 AM
In general, having multiple files is better as if you do any bug-fixes then only the specific files need to be re-downloaded.

also it helps split up your code so if you have:

- one file for the user account administrations,
- one file for program handling
- one file…

it means that you know where each bit is so you don't need to do a lot of Ctrl-F ing to find parts of the code you require…

having multiple files has really helped me. it really depends on your individual coding style and the project in question,

for an OS i recommend many files, for a simple program that say runs a printer etc… one file would be better.

also for something like paste-bin, one file is better.

play around with different things and I hope that your project goes well :)/>
Thanks a lot, I already made an installer, wich is modular, so that handles all the file downloading. :)/>
theoriginalbit #21
Posted 20 March 2013 - 01:42 PM
In general, having multiple files is better as if you do any bug-fixes then only the specific files need to be re-downloaded.
And that is known as Delta updates…. :)/>
PixelToast #22
Posted 20 March 2013 - 03:08 PM
In general, having multiple files is better as if you do any bug-fixes then only the specific files need to be re-downloaded.
usually bits of each file are modified
and keeping track of wich files are modified in an update is a pain
is it really hard to redownload a file thats probably under 300kb?
theoriginalbit #23
Posted 20 March 2013 - 03:10 PM
usually bits of each file are modified
and keeping track of wich files are modified in an update is a pain
is it really hard to redownload a file thats probably under 300kb?
Thats when implementing an intelligent updater is a good idea. the updater for cctube uses crc (checksum) checks to check for required updates so that it can perform a delta update.
PixelToast #24
Posted 20 March 2013 - 03:17 PM
its still MUCH easier to use a single file
for that you have to request a set of CRCs and compare them to all the files you have
theoriginalbit #25
Posted 20 March 2013 - 03:24 PM
6000 lines of code in one file can get quite hard to navigate and upkeep though.
and really, CRC's aren't expensive to calculate.
Ristyo #26
Posted 21 March 2013 - 02:49 PM
I suggest that u use different files to run it, but it also made it harder to install it on other computer, but if you are using different files then use :
shell.run("your program")
Engineer #27
Posted 22 March 2013 - 07:53 AM
I suggest that u use different files to run it, but it also made it harder to install it on other computer, but if you are using different files then use :
shell.run("your program")
It actually isnt that hard to install it on a computer, but HTTP is required for that. I might look in an auto - installer for non-HTTP but for that again you have to need acces to your computer folders.
I dont think that last thing is not that smart, because people will think its a virus etc. I mean really, you can read this on the internet so it shouldnt be hard to enable http. For the servers wich dont have that, there are auto-typers out there on the webs.

And thanks for the shell.run(" "), I actually knew that already but thanks for pointing that out :P/>

Edit: I am using os.loadAPI( name ), so I wont need shell.run.
Edited on 22 March 2013 - 06:55 AM
theoriginalbit #28
Posted 22 March 2013 - 08:02 AM
Edit: I am using os.loadAPI( name ), so I wont need shell.run.
Much better option!
PixelToast #29
Posted 22 March 2013 - 02:09 PM
or even better, dofile
theoriginalbit #30
Posted 22 March 2013 - 02:17 PM
except dofile doesn't load it into the global space meaning other apis that use that particular api would need to have their own dofile to work. loading the api into the global means they could just use it.
Mads #31
Posted 23 March 2013 - 05:15 AM
file_x.lua:

local function doStuff()
    print("doing random shit!")
end

return {
    printShit = doStuff;
}

main.lua:


local function require(path)
    local t = dofile(path)
    for k, v in pairs(t) do _G[k] = v
end

require("file_x.lua")
printShit()

Should work. Haven't tested, though.
Ristyo #32
Posted 24 March 2013 - 06:39 AM
I suggest that u use different files to run it, but it also made it harder to install it on other computer, but if you are using different files then use :
shell.run("your program")
It actually isnt that hard to install it on a computer, but HTTP is required for that. I might look in an auto - installer for non-HTTP but for that again you have to need acces to your computer folders.
I dont think that last thing is not that smart, because people will think its a virus etc. I mean really, you can read this on the internet so it shouldnt be hard to enable http. For the servers wich dont have that, there are auto-typers out there on the webs.

And thanks for the shell.run(" "), I actually knew that already but thanks for pointing that out :P/>

Edit: I am using os.loadAPI( name ), so I wont need shell.run.
i mean installing to other computer without pastebin and stuff instead using a floppy disk