Simba College-unity-you 0 basic C # series (iv) Functions and encapsulation

Source: Internet
Author: User
Tags goto modifier random seed

1.

Simba College: a fair and aboveboard.

The National Day holiday ended, my mood is this:

You always get up early and don't get up early.
Sleeping alone till Dawn
You dream of that copy with no regrets.
I know you don't even want to go to work.
You always get up early and don't get up early.
Holiday is always short, work is too difficult
Take a sick leave and sleep another day.

Today we talk about C # functions and encapsulation

You have a lot of information on this topic, everyone who has written code and who has not written the code knows these things.
And I don't have a lot of new angles, remember this first.

It's not math, it's not math, it's not math.

I contacted the earliest language is basic, at that time the function is not so popular, not the visual Basic,dos era contact. There are some system functions can be called, their own small program is less than the definition of functions, perhaps can not be defined, anyway, I learned that would be completely unaware.

What about basic? Isn't this a C # article? Just to tell you that there was a language that was not so dependent on functions and encapsulation, functions are not required. But C #, or C-system language, is based on functions and encapsulation. So you might think, what about a code that doesn't have to function or encapsulate? The past basic has a line number that can be goto one line and then execute. We rely on this goto, dividing the code into chunks and jumping around.

This is essentially a package without grammatical guarantees, essentially a function. In C language, you can also use Goto, actually compiled into machine language, it is goto to Goto.

To organize a function together is called encapsulation. A function that specifically marks a function as a piece of the syntax of a program.

Each line of code is your precious asset, such as clothes, two or three pieces of time, you can just put it there. Dozens of pieces of time, not sorting out a bit of trouble. Hundreds of pieces, you may have to split the room before the racks.

This requirement is the demand for encapsulation, which is naturally occurring as the complexity of the system increases.

If you do not feel the need for a moment, then do not consider the research function, research encapsulation, first to use a function to write the entire program, has been so, has been so. You can also become a unique programmer if you don't feel the need for encapsulation in the end.

C # 's function syntax is such a "modifier" type function name ("Parameter 1", "Parameter 2", ...) {...}

It's not too much to say or say, as we all know.

How to design a function?

The most important thing is that you can see the kind of function that sees a 500 line, a strong impulse to think about how to rearrange it into several functions?

This impulse is a learning function design method, which is the key to learning encapsulation.

You may have found a lot of good programmers, but they can't say anything, they can write good code, but can not explain why to write this, the application of the method or principle.

This kind of programmer is "instinctive", they learned the basic grammar, followed that sort of impulse, step by step toward excellence. I think that instinct is the most relaxed type, love and understanding, is your greatest help.

However, the world is always unsatisfactory, there are always some people need a little help to find their own entry point.

So let's talk about how the function is encapsulated.

Start with the name, then write the call, as for the implementation, say it.

All good function design is from the beginning of the name, and then write the calling code, as for the implementation, hehe, love who write who write, if you do not have an assistant, that is you write. Now you may have a deep suspicion of this, it's okay. Let's talk about naming first.

You may have heard several naming laws, such as the famous Hungarian nomenclature. What we're talking about is the mission of naming law. The mission of nomenclature is to make a name clear, accurate and easy to identify. Just. This is, Jane. Single.

    1. in English. I do not resist pinyin, I said is using the English alphabet, do not use Chinese or Martian text, input is too annoying, 26 English letters without any input method can be knocked out with the keyboard, which is the natural advantage. No matter what you are a people who love their own culture, I believe you do not want to resist this advantage, you can build a 5,000-button keyboard, each key corresponding to a common Chinese characters, I very much doubt how long it takes you to use such a keyboard. As for the use of pinyin, it is only a low problem, just let you look like a groundhog, say back, casually check the dictionary to get the English to be cautious, don't casually disorderly use, to check a thorough, make clear contextual usage, otherwise also is a groundhog.

    1. Regardless of the nomenclature, he consists of two parts of the word and delimiter, with two world-wide delimiters, underscores, and hump methods. For example M_gold dosth don't invent your own special separation method. Words do not matter, with shorthand must use the conventional, shorthand to take a few characters is not to mess, you set the rules, how others may understand, such as inte you know is a what? Use an integer or int. Cha, you know what it is? Use character or char.

    1. The function name is the verb-object phrase, the variable name and the type name are attributive modifier clauses. Well, first to the Chinese teacher on the column incense, I am not sure I said right.

      Void hit () This is not a good name, because there is no object, according to the context also infer not to fight who. Void hit peas () it's better.

      Void Circle () It's not a big problem, it's easy to infer that you're circling. Here in Chinese just to explain, don't pick me oh.

      int gold coins int Beans gold coins Depending on the context, they are all Chinese, you have to consider from the point of view of the language is clearly not. Name, it is the word of the thing.

The name is finished.

Let's go back to the question of whether to write the call first or write the implementation first. This question has an interesting phenomenon, most novice programmers think that they should write the implementation first, when they become the veteran, it is unified that should write the call first.

Let's consider such a question. Before the program runs, does the programmer know the order in which the program runs. The answer is to know.

Let's think about such a question again. A function that does not know the details of its implementation, whether the programmer can use it, the answer is yes.

Let's consider this question again. What do we see when the program structure is looking at the end of the structure we want to observe? Do we need to see its function implementation? The answer is no need.

Programming is the program before running the first plan how he ran, planning program how to run to design the structure of the program, is to design who call who, do not know the implementation of the details of the function does not affect our design program

So

Always write the call first, then write the implementation, is the program of the Iron

I would like to say that this is the end of the conversation, but still have to look after the students who are not high-savvy.

Or is it inevitable? Several function design principles:

Function not too long, more than 100 lines worth thinking about, empty line does not count.

All design principles are basically experience principles, 100 80 200 You may also hear various versions, no matter, all the same, are a number of experience. The starting point of this principle is not to write too long, not easy to read

The function name has decided what the function does, focusing on the matter, regardless of the parameter, return value implementation to be loyal to this matter, otherwise, go to another function.

When reading a good function is not necessary to see its implementation can understand its role, a function that does not match the name is a pit, although each program is in the deceptive and the pit grew up, but remember this principle in advance, will provide you with some help.

The consistency of the function is that the input of the function is consistent and the output should be consistent.

function does not add to his irrelevant state, this one is difficult to understand, so we say separately

The random number function surface looks like each time the input is empty, the return is different, actually is not so, the random number function has an invisible input, random seed. As with random seeds, the random number function returns just the same.

All functions are like this, there is no state of uncertainty in the computer, all States are determined, only like random seed, time, such as code readers imperceptible invisible state.

This means that the invisible state of the function should be carefully designed to keep it from being externally easy to observe.

For example, you write a function addrandhp (10), and then its behavior is to add 5 to 15 points of blood, in 10 plus plus or minus 5 range. This will have a hidden state between plus and minus 5 random.

ADDRANDHP (5,15) is designed in such a way that it is easy to understand its behavior from the outside.

The function should report the error as early as possible, such as checking that the input is valid first.

In the execution of the function error, should give normal abnormal feedback, with throw xxx, which is also controversial, there are different methodologies. But Microsoft's class library is as early as possible to throw an exception, we follow the style of Microsoft.

Unconsciously may be the zero base series to do more complex, maybe I will be suitable for writing this kind of force effect of the text, introspection.

Farewell, Hakuna Matata Tata.

Simba College-unity-you 0 basic C # series (iv) Functions and encapsulation

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.