"Goto Statement Considered Harmful"

Dijkstra có nói :"… tay nghề của một lập trình viên tỉ lệ nghịch với số toán tử goto mà anh ta viết trong chương trình."
Mọi người nghĩ sao về câu này :thinking:

Nếu không dùng goto mà tay nghề vẫn kém thì sao :thinking: 0 làm gì có nghịch đảo :thinking:

1 Like

Câu này là câu đầu của bài “Go To Statement Considered Harmful” (1968):slight_smile: Bài ấy Djikstra còn yêu cầu phải xóa sổ goto cơ, và cũng có giải thích rồi, ngoài việc nhảy linh tinh thì còn khó theo dõi các biến nữa.

Nguyên bản là “decreasing function” thì nên hiểu là “nghịch biến” :slight_smile:

3 Likes

Djikstra đề nghị bỏ go to cũng là hợp lý. Bởi vì goto có thể thay thế bằng cách dùng if-else, while, do-while. Có những ngôn ngữ còn không có goto. Từ ngày không code C mình chưa bao giờ dùng go to cả :smiley:

1 Like

Thực sự goto (hay jmp các loại) vẫn có thể được dùng theo các hình mẫu (pattern) nhất định (đó là vào năm 1900 hồi đó nha), lúc này hướng cấu trúc manh nha hình thành :slight_smile:

nhìn rộng hơn: http://www.intentsoft.com/feature_x_is_co/

Today we live in the reality that you have to pick one language and you are stuck with the abstractions of that language. The ability to tune or extend a language to get closer to the domain we solve problems in is yet possible. And of course mixing languages leads to all kinds of complications because our tools do not work well across languages.

But until we increase the choice of language features corresponding to the greater variation in applications domains we face, more and more of the features we now have, will at one time or another have to be “considered harmful” as the degrees of freedoms of the features exceed the degrees of freedoms in the domains.

1 Like

Mình thì thấy trong linux dùng goto khá nhiều. Trong tài liệu coding style còn nêu ra một số lí do sử dụng lệnh goto: https://www.kernel.org/doc/html/v4.15/process/coding-style.html

7) Centralized exiting of functions

Albeit deprecated by some people, the equivalent of the goto statement is used frequently by compilers in form of the unconditional jump instruction.
The goto statement comes in handy when a function exits from multiple locations and some common work such as cleanup has to be done. If there is no cleanup needed then just return directly.
Choose label names which say what the goto does or why the goto exists. An example of a good name could be out_free_buffer: if the goto frees buffer. Avoid using GW-BASIC names like err1: and err2:, as you would have to renumber them if you ever add or remove exit paths, and they make correctness difficult to verify anyway.
The rationale for using gotos is:
    unconditional statements are easier to understand and follow
    nesting is reduced
    errors by not updating individual exit points when making modifications are prevented
    saves the compiler work to optimize redundant code away ;)
2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?