設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 696|回復: 0
打印 上一主題 下一主題

6. Writing Correct and Good Programs

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2016-1-1 17:33:18 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
6.  Writing Correct and Good Programs
It is important to write programs that produce the correct results.  
It is also important to write programs that others (and you yourself three days later) can understand,
so that the programs can be maintained - I call these programs good programs.
Here are the suggestions:

  • Follow established convention so that everyone has the same basis of understanding.
  • Format and layout of the source code with appropriate indents, white spaces and white lines. Use 3 or 4 spaces for indent, and blank lines to separate sections of codes.
  • Choose good names that are self-descriptive and meaningful, e.g., row, col, size, xMax, numStudents. Do not use meaningless names, such as a, b, c, d. Avoid single-alphabet names (easier to type but often meaningless), except common names likes x, y, z for co-ordinates and i for index.
  • Provide comments to explain the important as well as salient concepts. Comment your codes liberally.
  • Write your program documentation while writing your programs.
  • Avoid un-structured constructs, such as break and continue, which are hard to follow.
  • Use "mono-space" fonts (such as Consola, Courier New, Courier) for writing/displaying your program.
Programming Errors
There are generally three classes of programming errors:

  • Compilation Error (or Syntax Error): can be fixed easily.
  • Runtime Error: program halts pre-maturely without producing the results - can also be fixed easily.
  • Logical Error: program completes but produces incorrect results. It is easy to detect if the program always produces wrong result. It is extremely hard to fix if the program produces the correct result most of the times, but incorrect result sometimes. For example,
    // Can compile and execute, but give wrong result – sometimes!
    if (mark > 50) {
       cout << "PASS" << endl;
    } else {
       cout << "FAIL" << endl;
    }

    This kind of errors is very serious if it is not caught before production. Writing good programs helps in minimizing and detecting these errors.
    A good testing strategy is needed to ascertain the correctness of the program. Software testing is an advanced topics which is beyond our current scope.
Debugging Programs
Here are the common debugging techniques:

  • Stare at the screen! Unfortunately, errors usually won't pop-up even if you stare at it extremely hard.
  • Study the error messages! Do not close the console when error occurs and pretending that everything is fine. This helps most of the times.
  • Insert print statements at appropriate locations to display the intermediate results. It works for simple toy program, but it is neither effective nor efficient for complex program.
  • Use a graphic debugger. This is the most effective means. Trace program execution step-by-step and watch the value of variables and outputs.
  • Advanced tools such as profiler (needed for checking memory leak and function usage).
  • Proper program testing to wipe out the logical errors.
Testing Your Program for Correctness
How to ensure that your program always produces correct result, 100% of the times?
It is impossible to try out all the possible outcomes, even for a simple program.
Program testing usually involves a set of representative test cases, which are designed to catch the major classes of errors.  
Program testing is beyond the scope of this writing.

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-6-1 11:24 , Processed in 0.241084 second(s), 18 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表