Advantages of short Functions

Source: Internet
Author: User

Advantages of short Functions
Initial use of functions

When you first learned programming, you must remember why to use functions. That is, the repeated code is summarized into a function and used multiple times. There is no doubt that everyone is familiar with this, but is there any room for improvement? The answer must be yes!

PS. This article does not discuss object-oriented or design patterns, but focuses on functions inside the Class.

 

What's wrong?

Everyone knows that when a function is very long, it will cause reading difficulties. How did we solve it before?

Is there any other method? In fact, another method is the title of the article: Using Short functions.

Before discussing short functions, I would like to describe how to rewrite comments with a little space.

Passerby A: Nana? Isn't comment written at any time?

 

When should I write a comment?

Functions are mainly used for two purposes:

We will use it in the first place, and there is no alternative.

However, comments may not be the best solution for the second point.

Because if a piece of code is hard to understand, it can be either "not understanding English" or "complicated logic ".

 

Annotations are used to make up for their shortcomings, but why not solve them?

"Cannot understand English": Should Every programmer have a certain basic English? Pay attention to the naming rules to solve the problem.

"Complicated logic": if a piece of program logic is complicated and hard to understand, do you think you should try to explain it to others? Or do you have to sort out the code?

So how can we sort out your code? Short function?

 

Advantages of short functions-helps sort out code

I don't want to talk much about it. It's all programmers who can understand the code at a glance.

Here is another article: code is the best comment

The topic of this article is that code is the best annotation, but it is actually the advantage of short functions.

I will not repeat my work if I already have an article.

By the way, region's shrinking function in VS can achieve similar results. You can choose this function, but it is not recommended.

Next, I would like to discuss with you another advantage.

 

Advantages of short functions-optimized performance in. Net

I have seen this in "More Effective C #" and I think it is very good, so I recommend it to you.

Can I split a large function into many small functions to optimize the performance? This seems ridiculous. Yes, in fact, this performance improvement is insignificant, but if so, why not? What's more, this has another advantage.

Why can the performance of. Net be optimized when a long function is split into short functions?

 

First, you must understand:

The language under. Net is only compiled into an intermediate language. During running, it will be compiled into machine code again.

This intermediate language is compiled into a machine code based on a function. That is to say, this function is compiled every time a function is called.

 

Is it advantageous to split long functions into short functions? Is the amount of code different? A pure split will generate more code!

So there is another condition: When this long function has many branches.

1234567891011 public void Func1(){    if(xxxx)    {        // Code segment 1, hundreds of lines    }    else    {        // Code segment 2, hundreds of lines    }}

The above Code contains two major sections. Each call to this function actually only executes a part of the code.

Therefore, if the two code segments are extracted, less than half of the code will be compiled during the compilation process.

Now I understand when long functions can be divided into short functions to optimize performance.

 

Although this performance improvement is not obvious, if one of your functions has a large branch.

When you look at the code, you will certainly not know what part you are in during the process of scrolling.

Even if you do not consider performance, we recommend that you split the code into short functions!

 

Summary

The advantages of short functions, the defects of comments, and region...

I generally follow the following principles when writing code:

Finally, I hope this article will be useful to you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.