Programming by Intention, Depth-First or Breadth-First?

Say I have the following graph of dependencies between procedures/functions/methods:

      o
    /   
  v       e
 /      / 
r   f   l   w

That is, function o first calls function v, and then function e. Function v itself first calls function r, and then function f. Function e itself first calls function l, and then function w.

With Programming by Intention, in which order should these functions be defined?

Depth-First: o, v, r, f, e, l, w

Breadth-First: o, v, e, r, f, l, w

6

Everything old is new again. The folks who brought us the eXtreme Programming books were, among other things, promoting a set of best practices in software development. One of them, which they termed “Programming by Intention”, was not actually new, but was something that had been a very common coding technique in languages like COBOL and Smalltalk (usually called “top down” programming) years before.

(from the introduction of Programming by Intention)

Programming by Intention appears to be a rediscovery of top down programming where one starts with the top layer and goes down from there.

This was the way that code was written in the days of Pascal and its contemporaries. You started writing the main procedure (it happened to be located at the end of the file, by necessities of the language definition and compiler) and then write a procedure or function, which was then located before the code that called it.

This contrasts with the ‘bottom up design’ model that OO emphasizes – building the building blocks first, and then building them up from there.

It would be a misconception and misapplication of Programming by Intention to use this to lay a structural framework to the code. Unless the language defines it (a la pascal), it doesn’t matter what order the methods are declared in. Readability (and consistency between programmers) is key.

With modern IDEs, one can see the outline of the code quickly and easily. Hitting F3 in eclipse will open up the definition of a method call no matter where it is located in the code. To this extent, the organization of the methods is unimportant. If there is an order use the following guideline:

  1. Program within the language constraints.
  2. Program within the language conventions.
  3. Program within the team’s style constraints.
  4. Program consistently.

If you order top down or bottom up or depth first or breath first or alphabetical, do it. And stick with it.

I don’t know if this is actually specified in programming by intention, but maybe we can analyse pros and cons.

In prolog, a 5th generation programming language, you can use both forms of execution, thus from a pseudocode point of view both could be valid.

Breadth-First has the advantage that it can be read in a top-down fashion, if your functions vary widely in abstraction with each level then it may be possible to skip one level. Breadth-first would be the way to go.

However, if someone reading this has to read everything, or to avoid making assumptions about that, then depth-first would be better. It allows reading the code in the same way it is executed and makes reading it more easy and natural, think about the example of alligator eggs 😉

The best option is to have some tool that allows to sort everything in a depth-first fashion and collapse inner elements. I’ve seen that in many code editors and in the html generated by FreeMind. That allows the reader to choose the direction to continue, either breadth-first by collapsing and scrolling or depth-first by expanding and scrolling.

In general, depth-first is a much safer option.

I follow Robert C. Martin’s Step-down Rule. In his book Clean Code, he writes:

We want the code to read like a top-down narrative. We want every function to be followed by those at the next level of abstraction so that we can read the program, descending one level of abstraction at a time as we read down the list of functions. I call this The Step-down Rule.

Reading the code means you descend one level of abstraction at a time. Per your example, the narrative when reading the code would be

  1. To do O, we do V and E
  2. To do V, we do R and F
  3. To do E, we do L and W
  4. Here are the low-level details of how R is done
  5. Here are the low-level details of how F is done
  6. Here are the low-level details of how L is done
  7. Here are the low-level details of how W is done

If the reader wants to understand O, they may be able to stop reading after item 1, 2 or 3 above, depending on how many implementation details they want to know.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật