theoriginalbit, on 28 September 2013 - 08:40 AM, said:
Then you're naming your functions wrong. You should not have to scroll to a function to see what it does, the name of the function should be concise yet descriptive enough that you know exactly what it is.
BigTwisty was talking about a function called main. What does main do then?
theoriginalbit, on 28 September 2013 - 08:40 AM, said:
I whole-heartedly agree with what BigTwisty said, functions can made for a lot more readable code, large slabs of code are far less readable than several function calls to adequately named functions. Also one of the easiest ways to develop something is by applying functional decomposition to a problem. Functional decomposition essentially ends up having only 1 task per function, if there is more than approx 15-30 lines to a function you should consider breaking it up further until you no longer can.
But then, why if you have a function, and you call it once. No, that does not at readability at all, if you are done with that section of the code, feel free to have blank line under it and then continue to code. I do agree though that one long code should be broken up, by whitelines, not functions.
theoriginalbit, on 28 September 2013 - 08:40 AM, said:
Your point of "you shouldn't use functions to call it once, because it's pointless" is a bit of a (to put it nicely) silly comment, how do you know what code you're only going to ever use once? You don't, a future version or expansion upon your program could very well use that function in another place. It is much better to break individual features or tasks into their own function just incase you want to use it again later rather than having to try reorganise your code at a later stage. If you haven't already noticed, quite a lot of my programs and APIs I actually need to type very little of. why? because I have everything broken into functions, so if I need to write code to perform a certain task, instead of writing it again, I just go to a previous project and copy/paste the function.
Well if you have like in the OP's code, an intro(duction) function, then you are going to call it once, because it is a introduction. So there is no need for that function because you show it once in the program!
Well, if you have such an API, it is
modular. I mean, you make wrappers around other functions that does stuff for you, it is good to do that though, dont get me wrong there. But I really, really hate to see just a function and called once in the whole code. If you are going to add features, do it smart, make that modular as well. Make some sort of API to interact with the main program, but not every function be declared and only called once, there is no point in that. Like with a project of mine, I have a checking tree:
local tree = {
["folder"] = {
"screenUtils"; "otherCrap"; --etc.
["NotAFolder"] = true;
}
I made it so you only have to extend that one table, and it will do all the work for you. That is because the checking isnt hardcoded, this tree is hardcoded. I really prefer that kind of style of programming, but thats me I guess.
theoriginalbit, on 28 September 2013 - 08:40 AM, said:
A function is not designed to shorten code. A function allows us to think of our program as a bunch of sub-steps, each sub-step can be its own function and when any program or sub-step seems too hard, just break it down further into sub-steps! (this is functional decomposition!). Functions allow us to reuse code instead of rewriting it. Functions are very good at allowing us to test small parts of our program in isolation from the rest of the program. And of course functions allow us to keep our variable namespace clean (since local variables only "live" as long as the function does).
But then you are using a function just like goto, which I have heard is bad. But my preference, is to have as less functions as possible. Otherwise every program out there will look like:
-- All function declared
while true do
main()
end
And I believe that is silly. Why don't you put the main function just into the loop anyway...
theoriginalbit, on 28 September 2013 - 08:40 AM, said:
If you avoid using functions simply so you don't have to scrolling your IDE/Text Editor then you're doing something wrong, and you should also consider investigating a feature in any editor that does code called "code folding"
I have to admit that reason was bit silly