💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 第九章 The pseudocode Programming Process 伪代码编程过程 ### Summary of Steps in Building Classes and Routines ### 创建一个类的步骤: - 创建类的总体设计 - 创建类的子程序 - 复审并测试整个类 ### 创建子程序的步骤: - 设计子程序 - 检查设计 - 编写子程序的代码 - 复审并测试代码 以上过程均是迭代进行. ### 为何使用伪代码编程过程 ? - 用类似英语的语句来精确描述特定的操作 - 避免使用目标编程语言中的语法元素. - 在本意的层面上编写伪代码.用伪代码去描述解决问题的方法的意图,而不是去写如何在目标语言中实现这个方法. - 在一个足够低的层次上编写伪代码,以便可以近乎自动地从它生成代码.  ### 通过伪代码编程过程创建子程序 - 检查先决条件 - 定义子程序要解决的问题 - 这一子程序将要隐藏的信息 - 传给这个子程序的各项输入. - 从该子程序得到的输出. - 在调用程序之前确保有关的前条件成立(如输入数据的取值位于特定范围之内,有关的流已经初始化,文件已经打开或者关闭,缓冲区已经填满或清空等) - 在子程序将控制权交回到调用程序之前,确保其后条件的成立(如输出数据位于特定范围之内,流已经初始化,文件已经打开或者关闭,缓冲区已填满或清空等). - 为子程序命名. - 决定如何测试子程序. - 在标准库中搜寻可用的功能. - 考虑错误处理. - 考虑效率问题. - 研究算法和数据类型. - 编写伪代码. - 考虑数据. - 检查伪代码. - 在伪代码中实验一些想法, 留下最好的想法(迭代进行). ### 编写子程序的代码 : - 从伪代码开始 - 写出子程序的声明 - 编写第一条和最后一条语句,然后将伪代码转换为高层次的注释 - 每条注释下面填充代码 - 检查代码 - 收尾工作 - 迭代以上过程,完成 ### 标准示例: ```java ====================pseudocode process======================== The routine outputs an error message based on an error code supplied by the caolling routine.The way it outputs the message depends on the current processing state, which itretrieves on its own .It returns a vlue indicating success or failure. set the default status to “fail” look up the message based on the error code if the error code is valid if doing interactive processing , display the error message interactively and declare success if doing commnad line processing, log the error message t othe command line and declare success if the error code isn’t valid, notify the user that an internal error has bean detected return status information ============================End Pseudocode Process========================== /* The routine outputs an error message based on an error code supplied by the caolling routine.The way it outputs the message depends on the current processing state, which *itretrieves on its own .It returns a vlue indicating success or failure. */ Status ReportErrorMessage(ErrorCode errorToReport){ // set the default status to “fail” Status errorMessageStatus = Status_Failure; // look up the message based on the error code Message errorMessage = LookupErrorMessage( errorToReport); // if the error code is valid if( errorMessage.validCode()){ // determine the processing method ProcessingMethod errorProcessingMethod = CurrentProcessingMethod(); // if doing interactive processing, display the error message // interactively and declare success if(errorProcessingMethod == ProcessingMethod_Interactive){ DisplayInteractiveMessage( errorMessage.getText()); errorMessageStatus = Status_Success; } // if doing command line processing, display the error message to command // line and declare success else if ( errorProcessingMethod == ProcessingMethod_CommandLine){ CommandLine messageLog; if( messageLog.Status() == CommandLineStatus_OK){ messageLog.AddToMessageQuene(); errorMessageStatus = Status_Success; } else{ // cann’t do anything because the routine is already // error processsing } } else{ // cann’t do anything because the routine is already error processing } // if the error code isnot valid, notify the user that // an internal error has bean detected else { DisplayInternactiveMessage(“Internal Error : Invalid error code in ReportErrorMessage()”); } // return status information return errorMessageStatus; } ``` // CHECKLIST: The Pseudocode Programming Process ### 核对表:伪代码编程过程 - 是否检查过已满足所有的先决条件? - 定义好这个类要解决的问题了吗? - 高层次的设计是否足够清晰? 能给这个类和其中的每一个子程序起一个好的名字吗? - 考虑过该如何测试则合格类及其中每一个子程序了吗? - 关于效率的问题,你主要从稳定的接口和可读的实现这两个角度考虑吗? 还是主要从满足资源和速度的预期目标的角度考虑过呢? - 在标准函数库或其他代码库中寻找过可用的子程序或者组件了吗? - 在参考书中查找过有用的算法吗? - 是否用详细的伪代码设计好每一个子程序呢? - 你在脑海里检查过伪代码吗? 这些伪代码容易理解吗? - 关注过那些可能让你重返设计的警告信息了吗?(比如关于全局数据的使用,一些看上去更适合放到另一个类或子程序中的操作等) - 是否把伪代码正确地翻译成代码? - 你反复使用了伪代码编程过程了吗?有没有根据需要把一些子程序拆分成更小的子程序? - 在作出假设的时候有没有对它们作出说明? - 已经删除掉那些冗余的注释了吗? - 你是否采用了几次迭代中的最好的那次结果?还是在第一次迭代之后就停下来? - 你完全理解你的代码吗?这些代码容易理解吗? ### 中文要点: - 创建类和子程序通常都是一个迭代的过程。在创建子程序的过程中获得的认识常常会反过来影响类的设计。 - 编写好的伪代码需要使用易懂的英语,要避免使用特定程序语言中才有的特性,同时要在意图的层面上写伪代码(即描述该做什么,而不是要怎么去做)。 - 伪代码编程过程是一个行之有效的做详细设计的工具,它同时让编码工作更容易。伪代码会直接转化为注释,从而确保了注释的准确度和实用性。 - 不要只停留在你所想到的第一个设计方案上。反复使用伪代码做出多种方案,然后选出其中最佳的一种方案再开始编码。 - 每一步完成后都要检查你的工作成果,还要鼓励其他人帮你来检查。这样你就会在投入精力最少的时候,用最低的成本发现错误。 ### English Key Points: - Constructing classes and constructing routines tends to be an iterative process. Insights gained while constructing specific routines tend to ripple back through the class’s design. - Writing good pseudocode calls for using understandable English, avoiding features specific to a single programming language, and writing at the level of intent—describing what the design does rather than how it will do it. - The Pseudocode Programming Process is a useful tool for detailed design and makes coding easy. Pseudocode translates directly into comments,ensuring that the comments are accurate and useful. - Don’t settle for the first design you think of. Iterate through multiple approaches in pseudocode and pick the best approach before you begin writing code. - Check your work at each step and encourage others to check it too. That way, you’ll catch mistakes at the least expensive level, when you’ve invested the least amount of effort.