0

How big is too big for a function?

I have this function that has many lines of code (200+). I always thought a function was just big enough to do something. Take a look at function aubPSX-this is a scaled down version from what's on my laptop. https://code.sololearn.com/WtpT4jX145e5/?ref=app

26th Feb 2019, 3:24 PM
James Pinkerton
James Pinkerton - avatar
10 Antworten
+ 3
Well it takes multiple arrays (loaded with words-some arrays have over 30 words) and places 6 examples in a textarea for each art category I have (I have 3 main text areas each with the 6 examples). The 3 huge functions work now (had some issues with no repeating), I just want to know if they are too big? Also, if they are too big how would I split them apart. Note, they need a Reply on this
26th Feb 2019, 7:52 PM
James Pinkerton
James Pinkerton - avatar
+ 2
That would be nice...lol I'm still learning to code/script/program... It's very crude looking right now... yes I agree 100%, but I'm just trying to get it functioning for me so I can start creating artwork as quickly as possible (I want to quit my day job). It is a Art UnBlocker app/program so I can think of things to draw, paint, etc. (I would like to perfect it so other artists can benefit from it as well). I can always improve it later on
26th Feb 2019, 8:02 PM
James Pinkerton
James Pinkerton - avatar
+ 2
The problem is: If you look at your code three months from now, will you still understand it? My impression is: Probably not. It takes experience to figure it out, and there is a matter of taste, too: '5 lines max per function', 'Should fit in one screen', '100 lines' - I've read all these recommendations. Maybe it would help you to read a bit of other people's code and try to figure out what *you* find pleasant to read. Because in a few weeks your code will look just like the code of a stranger to you.
26th Feb 2019, 8:07 PM
HonFu
HonFu - avatar
+ 2
Line 372 to 460 look to me like you switch just one value (the array index) and everything else is the same. So let a separate function repeat this with different array indexes as args and you've gotten rid of a lot of text.
26th Feb 2019, 8:11 PM
HonFu
HonFu - avatar
+ 2
Ohhh, okay...! So make it as simple to read as I can. That way it's not so big. It will run faster and it will be a smaller size file too. Thank you I'm not 100% sure on how to do it yet, but i have a starting point now. Thank you again
26th Feb 2019, 8:17 PM
James Pinkerton
James Pinkerton - avatar
+ 2
There's DRY style and WET style in coding: Don't Repeat Yourself and Write Everything Twice. If you identify similar or identical operations, make that one operation and execute it multiple times - that's the whole point of functions in the first place. I would also recommend to make shorter lines (typical recommendation: 80 letters max, but I prefer less when possible) and format with indentation to make it more readable. Sometimes a bit more can be better: If it's easier to understand that way. For example you should choose names that a reader can immediately understand, even if you write a bit more that way.
26th Feb 2019, 8:22 PM
HonFu
HonFu - avatar
+ 1
Can you describe the job(s) your function does?
26th Feb 2019, 4:25 PM
HonFu
HonFu - avatar
+ 1
It's probably a matter of taste, too, but for me: Definitely too big. I also find the lines very long which adds to the complicated look. I have trouble understanding what exactly is happening but at first glance there are several similar code blocks, only with different values. Is it maybe possible to write the block only once and run it with different values? (So: Function?)
26th Feb 2019, 7:58 PM
HonFu
HonFu - avatar
+ 1
So i would have multiple smaller functions to handle that bulk of code?
26th Feb 2019, 8:18 PM
James Pinkerton
James Pinkerton - avatar
+ 1
Okay.. Looks like I have a lot more to do.... Again, thank you
26th Feb 2019, 8:24 PM
James Pinkerton
James Pinkerton - avatar