1.4.2.1 read Functional Programs

Source: Internet
Author: User
Tags virtual environment

1.4.2.1 read Functional Programs

 

We have seen an example in listing 1.1, that is, using an immutable type. Our conclusion is that the immutable type makes the code more readable. In this section, we will consider two pieces of code that can be used in our function-based games.

There are two examples in listing 1.8, involving two game roles (player and monster ). The first example shows how the Monster [1] [ai in the original article should be this 1] How to move one step and then determine whether the player is in danger. The second example shows how to fire.

 

Listing 1.8 code snippets form a functionalgame (C #)

 

VaR movedmonster = monster. Merge mstep (); [1]

VaR indanger = player. iscloseto (movedmonster); [2]

(...)

VaR hitmonster = monster. hitbyshooting (GUNSHOT); [3]

VaR hitplayer = player. hitbyshooting (GUNSHOT); [4]

(...)

 

In the first part of the code, execute the monster step [1] to get the new status of the monster, and then determine whether the player is close to the position of the monster after calculation;

The second part deals with shooting in the virtual environment. The Code creates a value, indicating the updated Monster [3], and the other value, indicating the player's new status [4].

All objects in this function-based game are unchangeable. Therefore, you cannot modify this object and other objects when calling object methods. If we understand this, we can get some comments on the previous example. In the first section, we first call the monster's javasmstep method [1]. This method returns a new monster, And we specify it to the variable movedmonster; In the next line [2], we checked whether the player was near the monster, so it was in danger.

We can see that the second line of code depends on the first one. If we change the order of the two lines of code, the program cannot be compiled because movedmonster is not declared in the first line.

If it is implemented in an imperative style, this method usually does not return any results, and it only modifies the status of the monster object. In this case, we can change the code sequence and the code can be compiled, but it will change the meaning of the program and the program behavior is incorrect.

When shooting in the game, the health value is modified. The second line of code creates a new monster [3] and new player [4] object with the updated attribute. The two lines of code are independent, so we can change the order. So, will this operation change the meaning of the program? It seems that it should not. All objects are immutable and will not change. Surprisingly, in imperative versions, gunshot can change its meaning if it is variable. The first of these objects may change some attributes of gunshot. The behavior of these objects depends on the order of the two statements.

Listing 1.8 is quite simple, but it already shows that immutability can eliminate many possible difficulties. In the next section, we will look at another important example, but now let's take a look at some of the content behind this book.

 

Refactoring and unit testing

 

As you know, immutability helps us understand what programs are doing, and therefore helps us refactor code. Another meaningful functional refactoring is changing the code execution. The code of the program may run at the first arrival, or be delayed and executed only when the result is required. Immutability is very important to F # program development methods, and also makes refactoring in C # easier. We will discuss refactoring in Chapter 11th.

Another advantage of immutability is the creation of unit tests for functional programs. Because of the immutability, the only thing a method can do is to return results. Therefore, the only thing we need to test is whether the method returns the correct results for a given parameter. This topic will also be discussed in chapter 11th.

 

We have discussed how functional programming is more effective. It has been mentioned that immutability is very important for easier Writing of parallel programs. In the next section, we will briefly discuss this topic and other related topics.

1.4.2.1 read Functional Programs

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.