From cb924ba45072bac644090da5763fdee4b9c6f81b Mon Sep 17 00:00:00 2001 From: "Li, Amazing Ang" Date: Mon, 28 Nov 2022 16:37:05 +0800 Subject: [PATCH] review translation 10 --- Languages/en/09_Constant_en/readme.md | 10 +++++---- Languages/en/10_InsertionSort_en/readme.md | 26 ++++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Languages/en/09_Constant_en/readme.md b/Languages/en/09_Constant_en/readme.md index 2412a09a3..eabc6f1e5 100644 --- a/Languages/en/09_Constant_en/readme.md +++ b/Languages/en/09_Constant_en/readme.md @@ -11,13 +11,15 @@ Codes and tutorials are open source on GitHub: [github.com/AmazingAng/WTFSolidit ----- -In this section, we will introduce two keywords in Solidity, `constant` and `immutable`. After the state variable declares these two keywords, you cannot change the values after the contract is compiled and it will help to save on `gas`. In addition, only the numeric variables can declare them as `constant` and `immutable`; the `string` and `bytes` variables can be declared as `constant`, but not as `immutable`. +In this section, we will introduce two keywords in Solidity, `constant` and `immutable`. After the state variable are declared with them, you cannot change the values after the contract is compiled. They will restrict the modication of variables and save `gas`. + +Value-typed variables can declare them as `constant` and `immutable`; `string` and `bytes` can be declared as `constant`, but not as `immutable`. ## Constant and immutable ### Constant -The `constant` variable must be initialized when declared and cannot be changed after that. Any attempt at changes will cause the contract to fail its compilation step. +The `constant` variable must be initialized when declared and cannot be changed afterwards. Any modification attempt will cause errors at compilation. ``` solidity // The constant variable must be initialized when declared and cannot be changed after that @@ -29,7 +31,7 @@ The `constant` variable must be initialized when declared and cannot be changed ### Immutable -The `immutable` variable can be initialized at declaration or in the constructor, so it is more flexible to work with. +The `immutable` variable can be initialized at declaration or in the constructor, so it is more flexible. ``` solidity // The immutable variable can be initialized in the constructor and cannot be changed later @@ -58,7 +60,7 @@ You can initialize the `immutable` variable using a global variable such as `add ## Verify on Remix -1. After the contract is deployed, initialized values of the `constant` and `immutable` variables can be obtained through the `getter` function on the Remix. +1. After the contract is deployed, initialized values of the `constant` and `immutable` variables can be obtained through the `getter` function. ![9-1.png](./img/9-1.png) diff --git a/Languages/en/10_InsertionSort_en/readme.md b/Languages/en/10_InsertionSort_en/readme.md index 90d59e86f..7023ec6f6 100644 --- a/Languages/en/10_InsertionSort_en/readme.md +++ b/Languages/en/10_InsertionSort_en/readme.md @@ -1,4 +1,4 @@ -# WTF Solidity Tutorial: 10. Control Flow, and Solidity Implementation of Insertion Sort +# WTF Solidity Tutorial: 10. Control Flow Recently, I have been revisiting Solidity, consolidating the finer details, and writing "WTF Solidity" tutorials for newbies. @@ -15,7 +15,7 @@ In this section, we will introduce control flow in Solidity, and how to use Soli ## Control Flow -Solidity's control flow is similar to other languages, mainly including the following parts: +Solidity's control flow is similar to other languages, mainly including the following components: 1. `if-else` @@ -71,7 +71,9 @@ function doWhileTest() public pure returns(uint256){ 5. Conditional (`ternary`) operator -The `ternary` operator is the only operator in Solidity that accepts three operands:a condition followed by a question mark (?), then an expression to execute if the condition is true followed by a colon (:), and finally the expression to execute if the condition is false (`... ? ... : ...`). This operator is frequently used as an alternative to an if...else statement. +The `ternary` operator is the only operator in Solidity that accepts three operands:a condition followed by a question mark (`?`), then an expression `x` to execute if the condition is true followed by a colon (`:`), and finally the expression `y`to execute if the condition is false: `condition ? x : y`. + +This operator is frequently used as an alternative to an `if-else` statement. ```solidity // ternary/conditional operator @@ -85,11 +87,11 @@ In addition, there are `continue` (immediately enter the next loop) and `break` ## `Solidity` Implementation of Insertion Sort -### N.B.: Over 90% of people who write the insertion algorithm with Solidity will get it wrong. +**Note**: Over 90% of people who write the insertion algorithm with Solidity will get it wrong at first try. ### Insertion Sort -The problem solved by the sorting algorithm is to arrange an unordered set of numbers from small to large, such as sorting `[2, 5, 3, 1]` to `[1, 2, 3, 5]`. Insertion Sort (`InsertionSort`) is the simplest sorting algorithm and the first algorithm many people learn. The idea of `InsertionSort` is very simple: from front to back, compare each number with the number in front of it, and if it is smaller than the number in front, switch their positions. +The problem solved by the sorting algorithm is to arrange an unordered set of numbers from small to large, such as sorting `[2, 5, 3, 1]` to `[1, 2, 3, 5]`. Insertion Sort (`InsertionSort`) is the simplest sorting algorithm and the first algorithm many people learn in their computer science class. The idea of `InsertionSort` is very simple: from begin to the end, compare each number with the number in front of it. If it is smaller than the number in front, switch their positions. The schematic is shown below: @@ -113,7 +115,7 @@ def insertionSort(arr): ### Bug shows up after rewriting to Solidity! -Python implimentation of Insertion Sort can be completed in only 8 lines. Thus, we rewrite it into Solidity code, by converting functions, variables, loops, etc. accordingly, and only need 9 lines of code: +Python version of Insertion Sort takes up 8 lines. We can rewrite it into Solidity by converting functions, variables, loops accordingly. It only takes up 9 lines of code: ``` solidity // Insertion Sort (Wrong version) @@ -132,17 +134,17 @@ Python implimentation of Insertion Sort can be completed in only 8 lines. Thus, } ``` -Then we put the modified version into Remix and run by entering `[2, 5, 3, 1]`. *BOOM!* There are bugs! After correcting it for a long time, I still could not find where the bug is. I went to Google to search for "Solidity insertion sort", and found that the insertion algorithm tutorials written with Solidity on the Internet are all wrong, such as: [Sorting in Solidity without Comparison](https://medium.com/coinmonks/sorting-in-solidity-without-comparison-4eb47e04ff0d) +But when we compile the modified version and try to sort `[2, 5, 3, 1]`. *BOOM!* There are bugs! After long-time debugging, I still could not find where the bug is. I googled "Solidity insertion sort", and found that the insertion algorithm written with Solidity are all wrong, such as: [Sorting in Solidity without Comparison](https://medium.com/coinmonks/sorting-in-solidity-without-comparison-4eb47e04ff0d) -Remix decoded the mistake with the following output: +Errors occured in `Remix decoded output`: - ![10-1](./img/10-1.jpg) +![10-1](./img/10-1.jpg) ### Correct Version of Solidity `InsertionSort` -After a few hours, with the help of a friend in the `Dapp-Learning` community, we finally found the problem. The most commonly used variable type in Solidity is `uint`, which is a positive integer. If it takes a negative value, an error `underflow` will be reported. In the insertion algorithm, the variable `j` may get `-1`, resulting in the corresponding error. +After a few hours, with the help of a friend in the `Dapp-Learning` community, we finally found the problem. The most commonly used variable type in Solidity is `uint`, which represent a positive integer. If it takes a negative value, an error `underflow` will be reported. In the above code, the variable `j` may get `-1`, resulting in the corresponding error. -So, we just need to add 1 to `j` so that it cannot take a negative value. The correct version is: +So, we just need to add 1 to `j` so that it can never take a negative value. The correct version of Insertion Sort: ```solidity // Insertion Sort(Correct Version) @@ -167,7 +169,7 @@ Result of the correct code: ## Summary -In this lecture, we introduced control flow in Solidity and learn about the Insertion Sort algorithm in Solidity. Solidity looks simple but there are many pitfalls hidden. Every month, there are projects that lose tens of millions or even hundreds of millions of dollars because of these small bugs. Master the basics and keep practicing to write better Solidity code. +In this lecture, we introduced control flow in Insertion Sort algorithm in Solidity. Solidity looks simple but have many traps. Every month, projects lose millions of dollars because of small bugs in their smart contract. To write a safe contract, we need to master the basics of the Solidity and keep practicing.