0

New programmer here! I'm creating my first script on my own, and I have a particular function that is quite large, as in 50 lines.

I understand that theoretically a function can be as large as you need it to be, but etiquette-wise, where is a good place to stay under?

I'm using Python 2.something if that makes a difference.

Lentro
  • 45
  • 5
  • @DanielRoseman this question is a _very_ poor fit for Programmers - it would be quickly voted down and closed over there, see http://meta.programmers.stackexchange.com/questions/6483/why-was-my-question-closed-or-down-voted/6491#6491 Recommended reading: **[What goes on Programmers.SE? A guide for Stack Overflow](http://meta.programmers.stackexchange.com/q/7182/31260)** – gnat Aug 05 '15 at 08:47
  • I use to rely on what pylint or pyflakes say when analysing my code. Both are quite straight forward and most modern editors have plugins for it. For example, flake8 by default will check the McCabe complexity of every function (https://en.wikipedia.org/wiki/Cyclomatic_complexity). – fernandezcuesta Aug 05 '15 at 08:50

1 Answers1

1

A good rule of thumb (and it's more a guideline of thumb really) is that you should be able to view the entire function on one screen.

That makes it a lot easier to see the control flow without having to scroll all over the place in whatever editor you're using.

If you can't understand fully what a function does at first glance, it's probably a good idea to refactor chunks of code so that the more detailed steps are placed in their own, well-named, separate function and just called from this one.

However, it's not a hard-and-fast rule, you'll adapt your approach depending on your level of expertise and how complex the code actually is.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953