开发者

Code structure: should I use lots of functions to increase readability?

开发者 https://www.devze.com 2023-02-02 16:18 出处:网络
My question has Bash and PowerShell scripts in mind, but I suppose it applies to other languages as well.

My question has Bash and PowerShell scripts in mind, but I suppose it applies to other languages as well.

It is my understanding that the purpose of a function is to perform the same (or a very similar) task multiple times. This decreases the amount of code in the script and it also makes it easier to maintain.

With that in mind, if you discover that your script only calls a function one time then there's no reason for that function to exist as a function. Inste开发者_开发问答ad, you should take the function's code and place it in the location where that function is being called.

Having said all that, here's my question:

If I have a complicated script, should I move each section of code into its own function even though each function will only be called once? This would greatly increase the script's readability because its logic (the functions) would all be at the top of the script and the flow of execution would be at the bottom of the script. Since 50 lines of code would be represented by just 1 line, it would be much easier to understand what the script is doing.

Do other people do this? Are there disadvantages to this approach?


Having functions also increases readability. So a bash script might look better and be easier to follow if it reads:

getParams()

startTask()

doSomethingElse()

finishTask()

# implement functions below

even if the function implementations are simple, it reads better.


Code readability is indeed a major concern, usually (nowadays) more important than sheer amount of code or performance. Not to mention that inlining function calls may not necessarily have noticeable performance benefits (very language specific).

So lots of developers (I venture to say that the better of the breed :-) create small functions/methods like you describe, to partition their code into logically cohesive parts.


A function does a well-defined task. If you have a mega function that does 5 different things, it strongly suggests it should be calling 5 smaller functions.


It is my understanding that the purpose of a function is to perform the same (or a very similar) task multiple times.

Well, it is my understanding that a function is a discrete entity that performs a specific, well defined task.

With that in mind, if you discover that your script calls a given function AT LEAST ONCE, then it's doing its job.


Focus on being able to read and easily understand your code.

Having clear, readable code is definitely more a payoff than being afraid of function calls overhead. That's just a premature optimisation.

Plus, the goal of a function is to accomplish a particular task. A task can be a sub-task, there's nothing wrong with that!


Read this book

http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

Here are some quotes from the book.

"Small! The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that."

"FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL.THEY SHOULD DO IT ONLY."


As far as my knowledge is concerned, a function represents a Sequence of steps which become a part of larger program. Coming to your question, I strongly agree that function(s) improve readability and re-usability. But at the same time breaking every thing into pieces might not be a good practice. Finally, I want to give one statement : "Anything In Excess Is Not Beneficial!"

0

精彩评论

暂无评论...
验证码 换一张
取 消