Code Complete閱讀筆記(一)

    代碼大全也讀了好幾個月了,一開始讀中文版,到現在慢慢嘗試着讀,確實感受到了“每天進步一點點”的魅力。遺憾的是沒有從一開始就做閱讀記錄,總有不能盡興和思路不清之感。確實,就像項目需要版本控制系統,讀書也需要時時記錄變化。所以,今天新開一貼,作爲自己的閱讀記錄之用。

    初步的想法是記錄讀到哪裏,有什麼疑問,以及原文中精煉的表述。


    2014-01-27    190   Chapter 8 : Defensive Programming

    問題:JAVA的斷言是如何實現的?如果要寫一個自己的斷言類,如何實現?在生產環境下斷言一定要關閉嗎?



    2014-01-28    199   Chapter 8 : Defensive Programming

   斷言和錯誤處理有什麼區別?

    ——Use error-handling code for conditions you expect to occur; use assertions forconditions that should never occur。

   問題:處理錯誤有幾種方式?每種方式各在什麼情況下適用?都有什麼優點和缺點?

    ——These examplesalso illustrate that error processing generally favors more correctness or morerobustness.



    2014-01-29    208   Chapter 8 : Defensive Programming
    ——Throw exception only for conditions that are truly exceptional.
    ——Throw exceptions at the right level of abstraction.
    問題:Java應該只拋出Exception的子類嗎?在其他語言中呢?
    ——Handling errors with exceptions just because your language provides exception handling is a classic example of programming in a language rather than programming into a language.
    問題:隔離倉思想:在類的級別如何表現?倉外和倉內的程序應該使用斷言還是錯誤處理?進攻式編程/防禦師編程是反義詞嗎?
    ——Sometimes the best defense is a good offense. Fail hard during development so that you can fail softer during production.    
    問題:如何使用ant和make?



   2014-02-02   213   Chapter 8:Defensive Programming
    ——Users don’t appreciate anything that results in the loss of their work, regardless of how much it helps debugging and ultimately improves the quality of the program.
    ——Code installed for defensive programming is not immune to defects, and you’re just as likely to find a defect in defensive-programming code as in any other code—more likely, if you write the code casually.
    問題:什麼是前條件和後條件?
    ——Defensive-programming techniques make errors easier to find, easier to fix, andless damaging to production code.



   2014-02-03   223   The Pseudocode Programming Process 

   問題:什麼是僞代碼開發過程,測試先行開發和按合約設計?
               創造一個類和一個子程序的一般步驟是什麼?
    ——The Pseudocode ProgrammingProcess defines a specific approach to using pseudocode to streamline thecreationof code within routines.
    僞代碼原則:僞代碼不應該限制實現語言。
    ——Avoid syntactic elements from the target programming language. Pseudocodeallows you to design at aslightly higher level than the code itself. When you useprogramming-language constructs, you sink to a lower level,eliminating themain benefit of design at a higher level, and you saddle yourself with unnecessarysyntacticrestrictions.
    問題:什麼樣的僞代碼可以直接變成註釋?
    ——One of the keys to the success of a project is to catch errorsat the “least-value stage,” the stage at which the least effort has been invested.
    ——The single biggest way toimprove both the quality of your code and your productivity is to reuse good code.



    2014-02-04   231   The Pseudocode Programming Process

   ——Don’t waste time scraping for incremental improvements until youknow they’re needed.
    ——Trouble in writing the generalcomment is a warning that you need to understand the routine’s role in theprogrambetter.
    問題:設計好數據結構,算法往往就不言自明。如何充分設計?如何防止過度設計以至於“癱瘓”?
    ——Pseudocodecan make your assumptions and high-level mistakes more obvious than programming-language code does.
    問題:如何將僞代碼編程用於實際工作中?它會降低編程效率嗎?
    ——Once you start coding, you get emotionallyinvolved with your code and it becomes harder to throw away a bad design and start over.
    問題:從僞代碼到真實代碼的完整過程是什麼?僞代碼帶來什麼思維上的益處?在這裏可以使用寫作的隱喻嗎?
    ——Mentally executing a routine is difficult,and that difficulty is one reason to keep your routines small.
    ——If you often find yourself suspecting that the compiler orthe hardware made an error, you’re still in the realm of superstition.
    問題:爲什麼要在子程序完成後再編譯?有什麼思維陷阱?



    2014-02-11   234   The Pseudocode Programming Process

    問題:那些情況說明在堆砌代碼,需要停下來使用PPP?

    ——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).



    2014-02-12   243   General Issues in Using Variables

    ——The first step in creating effective data is knowing which kind of data to create. A good repertoire of data types is a key part of a programmer’s toolbox.
    問題:在Data Literacy Test測試中能得多少分?下一本書是否選擇數據結構與算法方面的?
    ——Initializing variables as they’re declared is an inexpensive form of defensive programming.
    問題:"keep related actions together"在那些情況下適用?有什麼好處?


    2014-02-13   254   General Issues in Using Variables

    ——Thus the main advantage of keeping references to variables together is that it improves program readability.
    問題:變量的span和live time各指的是什麼?
    ——Initialize variables used in a loop immediately before the loop rather than back at the beginning of the routine containing the loop. Begin with most restricted visibility, and expand the variable’s scope only if necessary.
    問題:變量值寫代碼時幫綁定,編譯時綁定,運行時綁定各代表什麼情況?遲綁定有什麼好處?有什麼壞處?
    ——Because successful programming depends on minimizing complexity, a skilled programmer will build in as much flexibility as needed to meet the software’s requirements but will not add flexibility—and related complexity—beyond what’s required.

 

    2014-02-25   258   General Issues in Using Variables

    ——Patterns in the code match patterns in the data.
    ——Your real data can be combinations of the sequential, selective, and iterative types of data. You can combine the simple building blocks to describe more complicated data types.
    問題:複用“臨時變量”是好的編程實踐嗎?
    ——The variable is stretched over two jobs, meaning that the variable is the wrong type for one of the jobs.
    問題:爲什麼要最小化變量的作用域,並全力避免全局變量?

 

    2014-02-26 267  The Power of Variable Names

    ——Programmers sometimes overlook using the ordinary words,which is often the easiest solution.

    A good name tends to express the what more than the how.
    問題:對於變量名長度需要有一個固定的規範嗎?如何掌握合適的度?
    ——When you give a variable a short name like i, the length itself says something about the variable-namely, that the variable is a scratch value with a limited scope of operation.
    ——If you modify a name with a qualifier like Total, Sum, Average, Max, Min, Record, String, or Pointer, put the modifier at the end of the name.
    問題:將計算值修飾詞後置有哪些好處?
    ——If you have several nested loops, assign longer names to the loop variables to improve readability.
    ——A flag should never have flag in its name because that doesn't give you any clue about what the flag does.
    ——It's OK to figure out murder mysteries, but you shouldn't need to figure out code.You should be able to read it.
    In general, temporary variables are a sign that the programmer does not yet fully understand the problem.
 
 

    2014-02-27276  The Power of Variable Names

    ——Keep typical boolean names in mind: done, error, found, success or ok.
    問題:以上這些經典的布爾變量名稱各代表什麼含義,適用在什麼場合?
    問題:命名布爾變量名稱時加前綴is,有什麼好處,有什麼缺點?
    ——Effective standards are some of the most powerful tools at your disposal.
    問題:命名慣例有哪些具體的好處?
    ——Programming conventions can make up for the weakness of the language you're using.
    ——Different conventions have different degrees of formality.An informal convention might be as simple as "Use meaningful names".
    ——Identify a class's member data. Make it clear that the variable isn't a local variable and that it isn't a global variable either.
    ——Augmenting a language with a naming convention to make up for limitations in the language itself is an example of programming into a language instead of just programming in it.
    問題:Java的一般性命名慣例有哪些?

 

    2014-02-28   285   The Power of Variable Names

    問題:變量名應包含的三種信息:內容、種類和作用域各代表什麼?
    ——Although the Hungarian naming convention is no longer in widespread use, the basic idea of standardizing on terse, precise abbreviations continues to have value.
    ——Standardized prefixes are composed of two parts: the user-define type(UDT) abbreviation and the semantic prefix.
    問題:UDT和Semantic Prefixes的含義各是什麼?有哪些常用的Semantic Prefixes?
    ——Standardized prefixes add precision to several areas of naming that tend to be imprecise. The precise distinctions between min, first, last and max are particularly helpful.
    ——In modern languages like C++, Java and Visual Basic, you can create names of virtually any length; you have almost no reason to shorten meaningful names.
    問題:創造略縮詞時有哪些注意點?
    ——Apply the telephone test-if you can't read your code to someone over the phone, rename your variables to be more distinctive.
    ——The fact that a programmer can't create a new abbreviation without the overhead of checking the Standard Abbreviations document out of version control, entering the abbreviation, and checking it back in is a good thing. It means that an abbreviation won't be created unless it's so common that it's worth the hassle of documenting it.


    2014-03-02   290  The Power of Variable Names

    ——Read code of your own that you haven't seen for at least six month and notice where you have to work to understand what the names mean.
    ——If you can switch the names of two variables without hurting the program, you need to rename both variables.
    問題:什麼是變量名的心理距離?
    問題:爲什麼用Count或者Index代替Num是一個良好的習慣?
    ——Good variable names are a key element of program readability. Specific kinds of variables such as loop indexes and status variables require specific considerations.
    ——Code is read far more times than it is written.Be sure that the names you choose favor read-time convenience over write-time convenience.


    2015-03-03   295   Fundamental Data Types
    ——This chapter contains tips for using numbers(in general),integers,floating-point numbers,characters and strings,boolean variables,enumerated types,named constants,and arrays.
    ——A good rule of thumb is that the only literals that thould occur in the body of a program are 0 and 1.Any other literals should be replaced with something more descriptive.
    ——Avoid mixed-typed comparisons. Do the conversion manually so that the compiler can compare two numbers of the same type and you know exactly what's being compared.
    ——Top programmers fix their code to eliminate all compiler warnings.It's easier to let compiler do the work than to do it yourself.
    ——When using floating-point numbers,avoid additions and subtractions on numbers that have greatly different magnitudes.
    問題:用計算機做無窮級數相加,爲什麼要從最小的開始?


    2015-03-04   307   Fundamental Data Types

    ——There is an exception to every rule.
    ——In some languages such as Java,all strings are Unicode.In others such as C and C++,handling Unicode strings requires its own set of functions.
    ——Having a convention minimizes mental overload and programming errors.
    問題:如何避免C中出現無限長的字符串?
    ——Instead of merely testing a boolean expression,you can assign the expression to a variable that makes the implication of the test unmistakable.
    ——Anytime you see a numeric literal,ask whether it makes sense to replace it with an enumerated type.
    ——If your language doesn't have enumerated types,you can simulate them with global variables or classes.


    2015-03-05   318   Fundamental Data Types

    ——If the way the code is written leaves any shadow of a doubt about its purpose,clarify it with a well-named constant.
    ——Be a fanatic about rooting out literals in your code.Use a text editor to search for 2,3,4,5,6,7,8,and 9 to make sure you haven't used them accidentally.
    ——Consider using container classes that you can access sequentially-sets,stacks,queues,and so on-as alternatives before you automatically choose an arry.
    問題:儘量用容器類代替數組有什麼好處?
    ——Are type names oriented toward the real-world entities the types represent rather than toward programming-language types?


 


 




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章