As people pointed, this improves readability. A person reading process_url()
may see more clearly what is the general process to deal with URLs just by reading a few method names.
The problem is that other people may think those functions are used by some other piece of the code, and if some of them need to be changed they may choose to preserve those functions and define new ones. This means some code becomes unreachable.
There are several ways to deal with this. First is documentation and comments in the code. Second, tools that provide coverage tests. In any case, to a great extent this depends on the programming language, these are some of the solutions you can apply depending on the programming language:
- object oriented languages can allow to define some private methods, to ensure they are not used elsewhere
- modules in other languages may specify which functions are visible from the outside, etc.
- very high level languages like Python may eliminate the need to define several functions because they would anyway be simple one liners
- other languages like Prolog may require (or strongly suggest) the definition of a new predicate for every conditional jump.
- in some cases it's common to define auxiliar functions inside the function that uses them (local functions), sometimes these are anonymous functions (code closures), this is common in Javascript callback functions.
So in short, splitting in several functions is usually a good idea in terms of readability. It may not be really good if the functions are very short and this creates the goto
effect or if the names are not really descriptive, in that case reading some code would require jumping among functions, which may be messy. About your concern about scope and usage of these functions, there are several ways to deal with it that are in general language dependent.
In general the best advice is to use common sense. Any strict rule is very likely to be wrong in some case and in the end it depends on the person. I would consider this readable:
process_url = lambda url: dict(re.findall('([^?=&]*)=([^?=&]*)', url))
Personally I prefer a one liner even if it is slightly complex rather than jumping and searching across several files of code, if it takes me more than three seconds to find some other part of code I may not even know what was I checking anyway. People who do not suffer from ADHD may prefer more explanatory names that they can remember, but in the end what you are always doing is balancing the complexity in the different levels of the code, lines, paragraphs, functions, files, modules, etc.
So the keyword is balance. A function with one thousand lines is a hell for anyone reading it, because there is no encapsulation and the context becomes just too huge. A function split into one thousand functions each one of them with one line in it may be worse:
- you have some names (that you could have provided as comments in the lines)
- you are (hopefully) eliminating global variables and do not need to worry about the state (having referential transparency)
- but you force readers to jump back and forth.
So there are no silver bullets here, but experience and balance. IMHO the best way to learn how to do this is reading a lot of code written by other people an analyzing why is it hard for you to read it and how to make it more readable. That would provide valuable experience.
process
,call
,doIt
. Similarly for value names:data
,object
,thing
,component
,vector
,list
, etc.