Why Think About Program Design Before Coding?

请给我六个小时的时间来砍树,我将用前四个时间削斧头。亚伯拉罕·林肯。 这个 该声明也适用于软件开发。 事实是,要开发出色的软件,您必须多加考虑 围绕软件组件的设计和交互。 为什么要花时间在程序设计上? 如果你花时间考虑 your program design you will spend less time coding. Following 亚伯拉罕·林肯's quote, you will spend 33.3% of your time coding。 这个 is because after program design you know all the classes and methods which you have to code and how they will change the state (data) of your program. It will help you think about your program as a group of modules interacting with each other thereby making your program modular. Modularizing your program will ease task assignment during development. It will help you write better code as you will have to think about program decomposition and step wise refinement. All in all, thinking about program design before coding will make you a better software engineer and programmer.

Why Use Tic-Tac-Toe As Our Case Study?

如何设计计算机程序
Tic-Tac-Toe is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. The following example game is won by the first player, X (En.wikipedia.org, 2018):

如何设计计算机程序

Players soon discover that the best play from both parties leads to a draw. Hence, tic-tac-toe is most often played by young children (En.wikipedia.org, 2018).
I choose Tic-Tac-Toe as our case study because it is relatively easy to program and programming it will give me the chance to talk
about some key software engineering techniques. You can find the code at the following link:
Fork me on Github

Strategies For Designing Better Programs

Decomposition

  • As computer scientists, we know that to solve a big problem we need to break it into smaller problems and solve these smallerproblems. In software design we do exactly the same thing. To develop the Tic-Tac-Toe game, I decomposed it into the following pieces:
  1. 初始化玩家-获取玩家的名字,为他们分配绘画符号和ID。初始化游戏网格-绘制Tic-Tac-Toe网格,并为Players设置初始回合。等待玩家轮到自己如果有玩家玩过,请检查获胜情况如果有胜利,请询问玩家是否要继续比赛。如果他们想继续玩,请重置游戏网格并重置玩家的回合(返回2),否则声明游戏的获胜者并退出程序。如果没有获胜,则交替转弯并回到3。

这是我开发该软件的最初策略。 当我开始编写代码时,我不得不改变这种策略,我意识到游戏中大多数玩家的动作将是MouseClick事件,而我将必须通过实现事件侦听器来处理它们。

Programming Paradigm

  • 在处理软件项目时,建议在编写代码时选择要使用的编程范例。 对于井字游戏,我使用的是面向对象的编程范例。 这意味着,我围绕类和接口设计了整个程序。
  1. TicTacToe类-此类包含我们程序的主要方法。GameBoard类-此类实现我们的游戏板并处理其上的所有交互。玩家类别-此类代表游戏中的玩家,并且还存储该玩家的属性。

除了这些类之外,这些类还共享一个GameConstants接口。 该接口指定所有常量 游戏所需。

Good Names and Method Length

  • When programing make sure your classes' names, methods' names, variables' and constants' names reflect what the class represents or what they do. Good class, method, variable and constant names will reduce the amount of commenting that you have to do. All my class names reflect what each of these classes either do or stand for.好的方法名称示例-initGrid(初始化井字游戏网格),checkForWin(玩家玩完后检查玩家是否获胜),drawCircle(在网格单元上绘制圆圈),drawCross(在网格单元上绘制十字) )和recordScore(记录特定玩家的得分)。良好变量名称的示例-scoreBoard(用于跟踪玩家得分的数组),boardState(用于跟踪板状态的Grid单元的数组)良好常量名称的示例-NUM_OF_PLAYERS(表示游戏中的玩家数量),NUM_GRID_ROWS(游戏网格中的行数),NUM_GRID_COLUMNS(游戏网格中的列数)

Version control

每当您在处理项目时,请确保首先使用初始化存储库吉特或您选择的任何其他版本控制工具。 每当您在代码中添加新功能时,请确保登台并提交这些更改。 登台和提交的频率很高,在登台和提交更改之前,请不要等到达到一个重要的里程碑。

Code Review

请同伴检查您的代码,并告诉您如何使其更好。 如果您想查看我的代码,请通过以下电子邮件向我发送请求:

Contacts:

Follow me on Twitter

Fork me on Github

Ëmail Me: [email protected]

from: https://dev.to//vladimirfomene/how-to-design-computer-programs-3ibp

相关文章: