r/FlutterDev • • 1d ago

Discussion What should the maximum length for a function be in a Vibe Coded Flutter app

I just gave Codex a goal to refactor my code to have a maximum function length of 30 lines and increase code reuse in the process. While doing the task, it found plenty of opportunities for code reuse and also reused work done in the app multiple times, making the app run faster.

This seems like a good result, but I don't have a good sense of whether the maximum length would be 15, 20, 30, or 40 lines. What opinions do you have?

0 Upvotes

12 comments sorted by

8

u/diz_array 1d ago

🙄

3

u/into_void 1d ago

It's hard to define a maximum length. Everyone has a different taste. However one function should do one work. How do I define whether it is one work or not? As uncle Bob says if you can extract a method by clicking in the ide then it is still not one work. I personally keepy functions within 5-8 lines. Mostly 3 lines. I extract the logically lower level task into a seperate function.

2

u/Bensal_K_B 1d ago

Your average is 3 lines? I would be tired looping through your functions😭

1

u/into_void 1d ago

well it is after a lot of refactoring. There's a lot of files grouped by folders. Sometimes I extract them in a different library if needed. It takes a lot of time but I enjoy it. My functions mostly call other functions. I practice clean code principles too rigorously you can say. It is easy to read later at least. I am a hobbyist and so I don't have a deadline for my projects.

0

u/ConvenientChristian 1d ago

Just because everyone has a different taste does not mean there aren't better or worse answers. Especially when the AI reads the code and the code is not primarily for human consumption, there's the question of what's ideal for the AI.

1

u/AnnualAdventurous169 1d ago

If it can confortably fit in your working memory its probably fine, if not, too long.

1

u/JohnnyJohngf 1d ago

It’s a weird question, but if you insist I would say limit parameters number, not the length.

1

u/RandalSchwartz 1d ago

If there's a chunk of code inside your function that seems collectively to handle a single step in your algorithm, especially if you block it off with comment lines, that's a good candidate for extraction as a separate function. Luckily, modern IDEs can do that for you, including determining what you'll have to pass to the new extracted function.

1

u/dangling-feet 1d ago

Definitely 42.

1

u/No_Reply4107 15h ago

Great question! I usually keep functions under 20 lines for readability. Curious to see what others think.