TAILIEUCHUNG - Visual Basic .NET I Didn't Know You Could Do That phần 2

nó. Kể từ khi bảng điều khiển nằm ngoài phạm vi của vòng lặp, biến ra khỏi phạm vi. Hầu hết các lập trình viên có thể chống lại tiềm năng cho các lỗi này phạm vi địa phương bằng cách đặt tất cả các tuyên bố Dim ở phía trên cùng của thủ tục hoặc chức năng. | 10 FROM VB6 TO it. Since the is outside the scope of the loop the variable is also out of scope. Most programmers might combat the potential for these local scope errors by putting every Dim statement at the top of the procedure or function. This can lead to an inefficient use of resources however. Consider the following code fragment If not UserHasAlreadyRegistered then Dim f as New RegistrationForm end if In this code some magic function goes off and checks if the program has already been registered. If it has not then an instance of the registration form is declared and shown. If the user has already registered the software why bother creating an instance of a form that will never be displayed All this does is clog up the garbage collector later. As you can see clever use of local scope variable can save your program memory making it run more efficiently. 3 Avoiding Redundant Function Calls The redundant function calls code can be found in the folder prjRedundant-FunctionCalls. This little coding shortcut seems so obvious that I almost didn t consider it worth inclusion in the book but I see this rule broken so frequently that I felt it worth repeating. The rule in its most basic form is as follows Why execute code more than once when running it once gives the same result To illustrate the rule with an absurd example consider the following block of code For X 1 to 1000 Y 2 Next AVOIDING REDUNDANT FUNCTION CALLS 11 This loop assigns the value 2 to variable Y one thousand times in a row. Nobody would ever do this would they What s the point Since no other code executes in the loop except for the assignment statement you know that nothing could possibly be affecting the value of Y except the assignment statement itself. When the previous loop is complete Y has the value of 2. It doesn t matter if this loop runs one thousand times one hundred times or simply once the end result is the same. While I ve never seen code quite as .

TỪ KHÓA LIÊN QUAN
Đã 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.