-3

Excerpt from book Clean Code authored by Robert Martin:

In the early days of programming we composed our systems of routines and subroutines. Then, in the era of Fortran and PL/1 we composed our systems of programs, subprograms, and functions. Nowadays only the function survives from those early days.

In my opinion, this excerpt is telling that routine is a bad thing, and is getting out of programming era.

I did some search and found that the difference between subroutine and function in Fortran is:

  • Subroutine performs some operation on the input variables, and as a result of calling the subroutine, the input variables are modified.
  • Function takes one or many parameters as inputs and returns a single output value.

Although I didn't write Fortran and only write C, there is a same construct supported by C as in int foo(int x) and void foo(int* x).

What drew my attention is that I never heard of void foo(int* x) is a bad convention in C, so I want to figure out what does Robert Martin mean by this excerpt.

One more question. From OOP's perspective, what if I want to simulate OOP in C, is routine a good choice over function?

Andy Lin
  • 397
  • 1
  • 2
  • 21
  • Just consider what is easier to understand in the long run when the code gets larger, a `check(x)` function that is guaranteed to not be able to change `x` or a `check(x)` routine that may very well increase x by one and the only way to know is to hit the documentation? – Joachim Isaksson Oct 13 '19 at 11:35
  • @JoachimIsaksson Well that does not really explain those many functions in C and C++ that either only return status codes or are `void` functions. – Vladimir F Героям слава Oct 13 '19 at 12:18
  • 2
    I would really recommend to ask one question per post and avoid opinion based ones as much as possible. I would seriously consider some sister site instead as a better venue. Secondly, you should probably ask people in the other language tags. People doing Fortran are just fine with subroutines. Think about the tags you use to get the right people. – Vladimir F Героям слава Oct 13 '19 at 12:21
  • @VladimirF Thank you for recommendation. May I ask which sister site can I go? – Andy Lin Oct 14 '19 at 07:14
  • 4
    Check what is on-topic at https://softwareengineering.stackexchange.com/. It is better to delete this one if you asked there. There is also computer science stackexchange but it may be off-topic there, Check their descriptions. – Vladimir F Героям слава Oct 14 '19 at 07:55
  • I edited the post. How do I know whether this edit is acceptable for cancelling put on hold mark? – Andy Lin Oct 16 '19 at 14:38

1 Answers1

1

It is basically redundant to have a separate language construct once you allow the function to return nothing or also discarding the function result. A C or C++ void function is really like a subroutine, it just looks like a function and uses the same keyword. So you save the need of one keyword and the language is more unified. And C is the origin of syntax of most languages used in practice today.

  • Although I agree that removing the `call` keyword is an effect here, to some extent, outside a purely functional language, the simplification of "subroutine is a function that has no result" leads to corresponding complexity elsewhere. For example, in Fortran terms, "function reference is a primary" would instead be "reference to a function which returns a result is a primary" or "a function not returning a result can be used only as a standalone statement". – francescalus Oct 13 '19 at 12:45
  • I consider the `call` keyword only an artifact made necessary by Fortran source form syntac rules for disambiguation. Also, I really wasn't addresing Fortran but the C-family languages. – Vladimir F Героям слава Oct 13 '19 at 12:49