top | item 27809232

(no title)

throwaway77770 | 4 years ago

I don't think Bob's standards are preferable. Or, in the likeness of Yegge's Execution in the Kingdom of Nouns, I present:

void get_coffee() { /* ... */ }

void move_away_from_obstacles(auto what_to_move_away) { /* .... */ }

void place(auto object_to_place, auto to_be_placed_on) { move_away_from_obstacles(where_to_place); put_object_on(to_place, to_be_placed_on); }

void sit() { place(butt, seat); }

void prepare_mind() { get_coffee(); sit(); }

void get_opinion(int on_whom) { /* .... */ if (on_whom == UNCLE_BOB) { return create_opinion("too many short functions"); } }

void get_opinion_of_uncle_bob() { prepare_mind(); opinion my_opinion = get_opinion(UNCLE_BOB); return my_opinion; }

discuss

order

frant-hartm|4 years ago

Haha, this is really good, never seen it. But one piece of the uncle bob's advice says you should order your functions from higher level abstraction to lower level, then it reads like a book and you can stop at any time you get the understanding of the code. So your example reordered:

void get_opinion_of_uncle_bob() { prepare_mind(); opinion my_opinion = get_opinion(UNCLE_BOB); return my_opinion; }

void prepare_mind() { get_coffee(); sit(); }

void get_opinion(int on_whom) { /* .... / if (on_whom == UNCLE_BOB) { return create_opinion("too many short functions"); } }

void get_coffee() { / ... / }

void sit() { place(butt, seat); }

void place(auto object_to_place, auto to_be_placed_on) { move_away_from_obstacles(where_to_place); put_object_on(to_place, to_be_placed_on); }

void move_away_from_obstacles(auto what_to_move_away) { / .... */ }

Zababa|4 years ago

I've always found this advice confusing and contradicting. Higher level to lower level doesn't read like a book. To make it read like a book (or an essay), you have to "flatten the tree", do something like:

void get_opinion_of_uncle_bob() { prepare_mind(); opinion my_opinion = get_opinion(UNCLE_BOB); return my_opinion; }

void prepare_mind() { get_coffee(); sit(); }

void get_coffee() { / ... / }

void sit() { place(butt, seat); }

void place(auto object_to_place, auto to_be_placed_on) { move_away_from_obstacles(where_to_place); put_object_on(to_place, to_be_placed_on); }

void move_away_from_obstacles(auto what_to_move_away) { / .... / }

void get_opinion(int on_whom) { / .... / if (on_whom == UNCLE_BOB) { return create_opinion("too many short functions"); } }