18.3 Good programming styles Although you will develop advanced object-oriented software, some good programming styles that have been summarized in the process-oriented age (at least in a foreseeable future) will not be outdated. This section will teach you the rules of these four seas, these rules are not mandatory, but it is not doubt that you will make your life easily.
A good style means programming in an easy-to-maintain manner. Your code should be easy to make anyone understand it. This is not that other people need to see these code you have written (of course, this situation may happen), but said that when you need to debug or modify the error, you can quickly understand that you have developed. what. People always lose their self-control attempt to build certain things and ignore useful finishing work. The action of grass rate will cause infinite troubles, so you should always try our best to follow good programming styles.
Of course, "good" can't get easy, "good" usually means a lot of work. Here, good means: good name, reduce repetition, always comment, and separation of code and data.
18.3.1 Good name
What is a good name? The concept of good names in the world is very different from the concept of good name in our daily life. In our daily life, a good name tends to mean very much to scrutinize: or express your parents' good desire, or the dictionary explanation. But a good name in a daily life is very bad in the program, one such name cannot provide additional information other than the name.
In the programming world, it is determined whether a good name is able to provide more information with a minimum character. In Flash, we can also name the thing is very much, each button or movie clip instance, each text instance, you can need naming; each variable, each function, every class, name.
In Flash, when you name a thing, you should try to let this name reflect all of this important information. For example: Fishcountermc acts as a movie clip in a statistical fish tank is very nice. After many days, when you see this name again, you can get a lot of important information in an instant. First, one eye-catching, we can know that this name refers to a movie clip (MC represents movieclip), so this name is the name of a movie editing instance, secondly, we can see that this is a movie used to count Clip (according to Counter), in the end, we can inference this counter should be a statistical fish (according to FISH).
When naming for a variable, it will be useful to reflect the data type of this variable in the variable name. Childage_num as a name of a variable used to save your child's age, it is good, from Num this suffix we can realize that this variable should be saved is a digital data type.
Sometimes, in order to give things a more meaningful name, pass more information, you will find that the name is getting more and longer, this is not a good thing, too long name will also cause reading difficulties. Because too much of the names, you will make you can't see the logic of the program, so treat anything, you should keep an appropriate attitude, many times, you need to pass more information and prevent too long names Compromise between them.
18.3.2 Reduce repetition
To make things simple, every programming work should only appear once in your movie. If your same code appears two, then your update and modify the work will double. You will learn some ways to achieve this, for example, save the script in the library, saved in a function or introducing a movie from the outside. At any time, when you plan to copy and paste the code, a little voice should sound in your mind - "Stop!". There will always be a more concise approach waiting for you. Reduced repetition also means refining code, try to complete the same job with fewer code. I don't think about it, when you look back for your program for debug bug, every line of code you check must be translated in your mind, and less, you will feel better. Usually, you can do some things at all steps or less code. Compare two code segments in code 1 and code 2, they achieve the same effect, but the code in code 2 is more refined.
Code one:
On (Release) {setProperty ("Highlight", _x, getproperty ("Highlight", _x) 10); TellTarget ("Highlight") {gotoistStop (getProperty ("", _ currentframe) 1);}}
Code 2:
On (Release) {highlight._x = 10; highlight.nextframe ();
Those scripts in the code one are the same effect as the script in code 2, but there is no necessary complexity. This except that there is no more significance in addition to the worship of players who have a lot of choice, there is no more meaning.
It is true that there is no way to legally the best way, but there is no doubt that reducing duplicate and refining code is beneficial. Of course, you don't have to repeat the reduction, refining code is too completely thorough. The requirements for streamlined code should not be more important than readability. It is very easy to completely lose self-control and ultimately ending on a pile of code that you can't read. I will never pick up in a completed code segment that work - because, it is true that this is the best consideration. Second, your code is usually maintained by you, so the code you can read and understand is the most important. Do everything possible to use the code you understand. If this sometimes means your code, it can only be went by it. As time goes by, you will slowly see your code is gradually shortement.
Sometimes, when I examine those programs that are just a few months ago, I will also question the methods I have used - but this is just because I am always progressing. If you plan to wait until your technology is perfect, you will wait too long. So in this way, the time will prove that you can progress.
18.3.3 Always comment
In Flash, the comment is the text starting with / /. Note is ignored in Flash. Note is not the characteristics of Flash, open any computer book involved in programming, and you will find discussions in the importance of comments. Indeed, the comment is very important, how to emphasize it will not be. Comments can make you only know how per-segment of the code after several months or even years, and you can continue to develop and maintain the procedures; if you need, let others understand your code, he (she) will Look at your kindness, feel your heart, vowed to be a good boy like you (or a girl).
I think I am a bad boy, because I often don't know full annotations to my code until I let it run. But no matter what, did not continue to delay this, it is very important to me, because after I wrote it, I will do it about everything about this code. Without a comment, the understanding of the code will become more difficult. So, spend some time to comment your code, even if you have completed your code, the enthusiasm has declined. Compare the code that is not annotated in the code three in the code and the same number of code four but notes. Although you may not understand the details of these codes, if there is a problem here, you will be able to easily identify the part of the problem. Code 3:
OnClipevent (keyup) {if (key.getascii () == 13 | key.getascii () == 0) {return;} if (key.getascii () == 8) {i (cur.charat) -2) == "") {_Root.WordThistime -;} cur = cur.slice (0, cur.length-2) mbchr (8); if (_Root.wrongplace [_root.place-1] == "x") {_root.wrongplace.pop (); _root.wrongs ---;} _root.place> 0 && _Root.Place--; return;}}
Code 4:
OnClipevent (Keyup) {// ignores these character if (Key.Getascii () == 13 | key.getaScii () == 0) {return;} // If they click back the retracted key IF (key.getascii () = = 8) {// Delete a space? IF (cur.lend (cur.length-2) == "" "{ot.wordthistime -;} // Delete the last character cur = cur.slice (0, cur.length-2) MBCHR (8) ; // Do they have modified a mistake? IF (_root.wrongplace [_root.wrace-1] == "x") {_root.wrongplace.pop (); _root.wrongs--;} // retreat _Root.Place> 0 && _root.Place- -; // Leave return;}}
The code three is difficult to understand before being annotated. The code four shows that there are not many annotations that make things clear, even if you still don't understand the potential meaning of these codes.
18.3.4 Separation Code and Data
All programming designers should strive to keep code (simple to program scripts) and data (or specific projects, such as text and graphics). Separation. By maintaining the separation of code and data, you can make your programming results into other projects. Similarly, when you want to change the content -, for example, do the entire project with different languages - you only need to replace the data without having to touch (or destroy) those code. This is great idea but sometimes it is difficult to achieve.
Assuming that there are some graphics buttons in your Flash site, these graphics buttons will display a floating tool tips when the user places his mouse pointer to the button. If you hold the code (make the script that appear in the tool prompt) and data (the text that will appear in the tooltip in the tool prompt). You can easily transplant this feature to another language, and you do just use another language to replace tooltips. Ideally, you save all the texts of all the tools in the same location, which will make the transplantation work more easily. The main purpose of this ideology is that you should try to make a major change in any of the code or data will have another impact on another. You can treat code data as a modular form. There are other modular forms - including Flash LoadMovie (), which enables you to play separate .swf files in a larger movie. In addition to the coded data separation mentioned, there are many other advantages. First, by modular your Flash movie, users don't need to wait for the download of the entire movie. They can download those who are interested in those. In addition, modularization also enables a work that can be cleaned into several independent parts, making these portions can be developed simultaneously. Thinking such a situation, if your entire movie is constructed only by one basic file, you can only work at the same time. Therefore, it can be seen that there are many advantages in code data separation and some modular forms, and you should try to keep the problem as modular thinking.