Programming - Software Engineering The Practice of Programming phần 2

Lập trình viên cũng phải đánh giá cân bằng, chọn trong số các lựa chọn thay thế thiết kế, gỡ lỗi và thử nghiệm, cải thiện hiệu suất, và duy trì phần mềm được viết bởi chính mình và những người khác. Đồng thời, họ phải được quan tâm với các vấn đề như tính tương thích, mạnh mẽ, và độ tin cậy, trong khi các chi tiết kỹ thuật đáp ứng. | 18 STYLE CHAPTER 1 One of the most serious problems with function macros is that a parameter that appears more than once in the definition might be evaluated more than once if the argument in the call includes an expression with side effects the result is a subtle bug. This code attempts to implement one of the character tests from ctype. h define isupper c c A c Z Note that the parameter c occurs twice in the body of the macro. If i supper is called in a context like this while isupperfc getcharO . then each time an input character is greater than or equal to A it will be discarded and another character read to be tested against The C standard is carefully written to permit and analogous functions to be macros but only if they guarantee to evaluate the argument only once so this implementation is broken. It s always better to use the ctype functions than to implement them yourself and it s safer not to nest routines like that have side effects. Rewriting the test to use two expressions rather than one makes it clearer and also gives an opportunity to catch end-of-fileexplicitly while c getcharO EOF isupper c . Sometimes multiple evaluation causes a performance problem rather than an outright error. this example define ROUND_TO_INT x int x x 0 size ROUND_TO_INT sqrt dx dx dy dy This will perform the square root computation twice as often as necessary. Even given simple arguments a complex expression like the body of ROUND-TO-INT translates into many instructions which should be housed in a single function to be called when needed. Instantiating a macro at every occurrence makes the compiled program larger. functions have this drawback too. Parenthesize the macro body and arguments. If you insist on using function macros be careful. Macros work by textual substitution the parameters in the definitionare replaced by the arguments of the call and the result replaces the original call as text. This is a troublesome difference from functions. The expression .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
76    841    3    04-07-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.