I. Introduction

Structured programming can be simply understood as passing and returning parameters by defining structs.

We recommend using structured definitions to manage input/output when necessary, especially in the code design of the controller and service layers.

1. Pain Points of Unstructured controller

Structured Programming - 图1

  • It is difficult to determine the data structure of interface input/output. Most scenarios involve hard-coded parameter reception names in the code, prone to errors leading to unforeseen issues.
  • Interface parameters often only define an HttpRequest/HttpContext object pointer, making it difficult to determine if the interface succeeded or failed.
  • The processes of parameter reception, validation, and conversion are tedious.
  • Generating and maintaining interface documentation is extremely challenging.

2. Pain Points of Unstructured service

Structured Programming - 图2

  • When there are many method parameters, the definition is awkward, and usage is cumbersome.
  • When the number and type of method parameters are uncertain, any arbitrary change (like adding a parameter) is non-compatible, leading to high modification costs.
  • Method parameter annotations are inconvenient, resulting in most business projects lacking method parameter annotations.

II. Structured Programming

1. Structured Improvements for controller

Advantages of Structuring:

  • By structurally managing interface input/output parameters, hard-coding parameter names for reception is no longer needed, reducing maintenance costs and avoiding errors due to hard-coded parameter names.
  • Enables automated parameter reception, conversion, and validation, thereby improving productivity.
  • Standardizes interface writing.
  • Makes interface management as convenient as ordinary function management, allowing error handling by returning error and standardizing the unified error mechanism.
  • Automates interface documentation generation, ensuring synchronized maintenance of interface structure definitions and documentation.

Example of Structuring:

Structure Definition:

Structured Programming - 图3

Method Usage:

Structured Programming - 图4

2. Structured Improvements for service

Advantages of Structuring:

  • When there are many method parameters, structs elegantly manage these parameters.
  • When the number and type of method parameters are uncertain, adding parameters is compatible with method invocation.
  • Provides more convenient annotations for struct properties, enhancing code maintenance quality.

Example of Structuring:

Structured Programming - 图5

III. Precautions

  • When using structured management for input/output parameters in the service layer methods, any parameter within the struct will be considered optional. It’s necessary to reasonably assess feasibility based on business scenarios.