扫码阅读
手机扫码阅读

一次典型的重构

219 2024-10-04

我们非常重视原创文章,为尊重知识产权并避免潜在的版权问题,我们在此提供文章的摘要供您初步了解。如果您想要查阅更为详尽的内容,访问作者的公众号页面获取完整文章。

查看原文:一次典型的重构
文章来源:
麦哲思科技任甲林
扫码关注公众号
Article Summary

Background Description:

The author decided to refactor a C# program calculating arithmetic expressions as an example for an upcoming talk on refactoring. The original code computed expressions with brackets and four arithmetic operations. The program was simplified by removing brackets, assuming the first character is a digit, and considering only valid expressions.

Refactoring Techniques Used:

  1. Renaming Variables: The original program used abbreviated pinyin for variable names, making them hard to understand. The refactored code uses meaningful English names.
  2. Extracting Methods: The original code used one large method, while the refactored code is split into four methods, improving understandability and reusability.
  3. Extracting Classes: A new class was introduced to encapsulate the calculation methods.
  4. Algorithm Optimization: The original logic was inconsistent and complicated, especially in handling multiplication and division.
  5. Correcting Logical Errors: The original program had hidden logical errors, evident when inputting expressions like "3*3/3/3".
  6. Adding Simple Comments: The original program lacked comments.

Original Code:

The original code is provided, showing a lack of clarity in variable naming and structure. It uses a single method to handle all operations and lacks comments.

Refactored Code:

The refactored code introduces a new class with private members to store operators and numbers. It includes methods to divide the expression into operators and numbers, determine if a character is an operator, and rebuild the expression after each operation. The code handles multiplication and division first, followed by addition and subtraction. Clear variable names and structure are used to enhance readability and maintainability.

想要了解更多内容?

查看原文:一次典型的重构
文章来源:
麦哲思科技任甲林
扫码关注公众号

麦哲思科技(北京)有限公司总经理 敏捷性能合弄模型评估师 认证的Scrum Master 认证的大规模敏捷顾问SPC CMMI高成熟度主任评估师 COSMIC MPC,IAC 成员,中国分部主席

425 篇文章
浏览 133.5K
加入社区微信群
与行业大咖零距离交流学习
软件研发质量管理体系建设 白皮书上线