浏览代码

Refactor interface.sol for better readability and adherence to best practices (#1565)

This Pull Request refactors the [Solidity code
](https://github.com/hyperledger/solang/blob/main/docs/examples/polkadot/interface.sol)in
the repository to enhance readability and align with best practices. The
changes include renaming contracts and functions for improved clarity,
adding comments for documentation, and following the SPDX license
identifier convention.

Key Changes:

- Renamed contracts and functions to follow naming conventions.
- Added comments for documentation.
- Followed the SPDX license identifier convention.

Signed-off-by: Mohammad Mahdi Keshavarz <arkonomii@gmail.com>
Mohammad Mahdi Keshavarz 2 年之前
父节点
当前提交
6022e71d37
共有 1 个文件被更改,包括 21 次插入25 次删除
  1. 21 25
      docs/examples/polkadot/interface.sol

+ 21 - 25
docs/examples/polkadot/interface.sol

@@ -1,42 +1,38 @@
-interface operator {
-    function op1(int32 a, int32 b) external returns (int32);
+// SPDX-License-Identifier: MIT
 
 
-    function op2(int32 a, int32 b) external returns (int32);
+// Interface for an operator that performs an operation on two int32 values.
+interface Operator {
+    function performOperation(int32 a, int32 b) external returns (int32);
 }
 }
 
 
-contract ferqu {
-    operator op;
+contract Ferqu {
+    Operator public operator;
 
 
-    constructor(bool do_adds) {
-        // Note: on Solana, new Contract() requires an address
-        if (do_adds) {
-            op = new m1();
+    // Constructor that takes a boolean parameter 'doAdd'.
+    constructor(bool doAdd) {
+        if (doAdd) {
+            operator = new Adder();
         } else {
         } else {
-            op = new m2();
+            operator = new Subtractor();
         }
         }
     }
     }
 
 
-    function x(int32 b) public returns (int32) {
-        return op.op1(102, b);
+    // Function to calculate the result of the operation performed by the chosen operator.
+    function calculate(int32 a, int32 b) public returns (int32) {
+        return operator.performOperation(a, b);
     }
     }
 }
 }
 
 
-contract m1 is operator {
-    function op1(int32 a, int32 b) public override returns (int32) {
+// Contract for addition, implementing the 'Operator' interface.
+contract Adder is Operator {
+    function performOperation(int32 a, int32 b) public pure override returns (int32) {
         return a + b;
         return a + b;
     }
     }
-
-    function op2(int32 a, int32 b) public override returns (int32) {
-        return a - b;
-    }
 }
 }
 
 
-contract m2 is operator {
-    function op1(int32 a, int32 b) public override returns (int32) {
-        return a * b;
-    }
-
-    function op2(int32 a, int32 b) public override returns (int32) {
-        return a / b;
+// Contract for subtraction, implementing the 'Operator' interface.
+contract Subtractor is Operator {
+    function performOperation(int32 a, int32 b) public pure override returns (int32) {
+        return a - b;
     }
     }
 }
 }