git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC] Convert builin-mailinfo.c to use The Better String Library.

From
WBWalter Bright <boost@digitalmars.com>
Date
Sep 7, 2007, 20:22 UTC
Message-ID
<fbsbul$dg0$1@sea.gmane.org>
In-Reply-To
<85wsv26cv8.fsf@lola.goethe.zz>
David Kastrup wrote:
> Again, C won't keep you from shooting yourself in the foot.

Right, it won't. A good systems language should do what it can to prevent the programmer from *inadvertently* shooting himself in the foot, while allowing him to *deliberately* shoot himself in the foot.

In the example loop, the cases I pointed out were ones where, if one changes part of the code (such as the array dimension, or the array type, etc.), then there are multiple places in the source that must be updated to reflect it. The reality of human programmers is we update one or two places, and overlook the third place, and we now have a bug. Saying that one should just be a better programmer and not make such mistakes is a pipe dream.

Ideally, each facet of the design of the code should have a single point where it can be changed, and then all dependencies on that design should be automatically updated. That way, nothing gets overlooked. Doing this in C, such as using a #define for the array dimension, involves extra work.

It's a truism that if it involves extra work, then it often gets omitted.

Doesn't "A design is perfect not when there is no longer anything you can add to it, but if there is no longer anything you can take away." apply here? Going from:

  void foo(int array[10])
  {
     for (int i = 0; i < 10; i++)
     {   int value = array[i];
         ... do something ...
     }
  }
to:
  void foo(int[] array)
  {
    foreach (value; array)
    {
      ... do something ...
    }
  }

takes a lot of frankly unnecessary things away, each of which is a potential source of error when maintaining the code.

> No, because programmers get things wrong.

Exactly. That goes back to my point that a good language should help prevent inadvertent errors, while still allowing deliberate choices. D approaches this by making the correct approach essentially be the one with minimal typing effort. To deliberately shoot yourself in the foot usually requires extra typing. For our loop example, we can still write the C style loop (with all its potential problems) in D, but it requires extra effort to put in those potential problems. The easier, simpler way doesn't have the problems.

(The issue I have with C++'s fixes to various problems is they require extra typing (like the loop example), so guess what, people being people tend to not use them. This results in endless attempts to try and push C++ programmers into using the more verbose forms, a strategy I suspect will be ultimately futile.)

> You can tell C compilers to
> check all array accesses, but that is a performance issue.

Runtime checking of arrays in D is a performance issue too, so it is selectable via a command line switch. But more importantly,

1) Static type checking of fixed size arrays works, so errors can be 
caught at compile time.
2) For dynamically sized arrays, the dimension of the array is carried 
with the array, so loops automatically loop the correct number of times. 
No runtime check is necessary, and it's easier for the code reviewer to 
visually check the code for correctness.

------ Walter Bright http://www.digitalmars.com C, C++, D programming language compilers http://www.astoriaseminar.com Extraordinary C++

Previous: David KastrupNext: David Kastrup
Message 43 of 102 in “[RFC] Convert builin-mailinfo.c to use The Better String Library.”
  1. Lukas SandströmSep 4, 2007
  2. Alex RiesenSep 4, 2007
  3. Pierre HabouzitSep 4, 2007
  4. Kristian HøgsbergSep 5, 2007
  5. Matthieu MoySep 5, 2007
  6. Miles BaderSep 6, 2007
  7. Dmitry KakurinSep 6, 2007
  8. Shawn O. PearceSep 6, 2007
  9. Andreas EricssonSep 6, 2007
  10. Junio C HamanoSep 6, 2007
  11. Andreas EricssonSep 6, 2007
  12. David KastrupSep 6, 2007
  13. Miles BaderSep 6, 2007
  14. Johannes SchindelinSep 6, 2007
  15. Linus TorvaldsSep 6, 2007
  16. Dmitry KakurinSep 7, 2007
  17. Linus TorvaldsSep 7, 2007
  18. Dmitry KakurinSep 7, 2007
  19. Linus TorvaldsSep 7, 2007
  20. Dmitry KakurinSep 7, 2007
  21. David SymondsSep 7, 2007
  22. Theodore TsoSep 7, 2007
  23. Steven BurnsSep 20, 2007
  24. Andreas EricssonSep 20, 2007
  25. Andreas EricssonSep 7, 2007
  26. Dmitry KakurinSep 7, 2007
  27. David KastrupSep 7, 2007
  28. Dmitry KakurinSep 8, 2007
  29. David KastrupSep 8, 2007
  30. Andreas EricssonSep 9, 2007
  31. David KastrupSep 7, 2007
  32. Johannes SchindelinSep 7, 2007
  33. Johannes SchindelinSep 7, 2007
  34. David KastrupSep 7, 2007
  35. Linus TorvaldsSep 7, 2007
  36. alanSep 7, 2007
  37. Walter BrightSep 7, 2007
  38. David KastrupSep 7, 2007
  39. Walter BrightSep 7, 2007
  40. David KastrupSep 7, 2007
  41. Walter BrightSep 7, 2007
  42. David KastrupSep 7, 2007
  43. Walter BrightSep 7, 2007
  44. David KastrupSep 7, 2007
  45. Walter BrightSep 7, 2007
  46. Andreas EricssonSep 8, 2007
  47. Pierre HabouzitSep 9, 2007
  48. Andreas EricssonSep 9, 2007
  49. Wincent ColaiutaSep 7, 2007
  50. Pierre HabouzitSep 7, 2007
  51. Walter BrightSep 7, 2007
  52. David KastrupSep 7, 2007
  53. Walter BrightSep 7, 2007
  54. Pierre HabouzitSep 7, 2007
  55. David KastrupSep 7, 2007
  56. Pierre HabouzitSep 7, 2007
  57. Walter BrightSep 7, 2007
  58. Pierre HabouzitSep 7, 2007
  59. Walter BrightSep 7, 2007
  60. John 'Z-Bo' ZabroskiSep 8, 2007
  61. David KastrupSep 8, 2007
  62. Steven BurnsSep 19, 2007
  63. Wincent ColaiutaSep 7, 2007
  64. Paul WankadiaSep 7, 2007
  65. Nicolas PitreSep 7, 2007
  66. Wincent ColaiutaSep 7, 2007
  67. Andreas EricssonSep 7, 2007
  68. Johannes SchindelinSep 7, 2007
  69. Andreas EricssonSep 7, 2007
  70. Wincent ColaiutaSep 7, 2007
  71. Karl HasselströmSep 7, 2007
  72. Andreas EricssonSep 7, 2007
  73. Wincent ColaiutaSep 7, 2007
  74. Andreas EricssonSep 9, 2007
  75. David KastrupSep 7, 2007
  76. Wincent ColaiutaSep 7, 2007
  77. Walter BrightSep 7, 2007
  78. Andreas EricssonSep 7, 2007
  79. Walter BrightSep 7, 2007
  80. David KastrupSep 7, 2007
  81. Andreas EricssonSep 9, 2007
  82. Bernd JendrissekSep 17, 2009
  83. Wincent ColaiutaSep 7, 2007
  84. Walter BrightSep 7, 2007
  85. Steven BurnsSep 22, 2007
  86. David KastrupSep 7, 2007
  87. Andy ParkinsSep 7, 2007
  88. David KastrupSep 7, 2007
  89. Johannes SchindelinSep 7, 2007
  90. Dmitry KakurinSep 8, 2007
  91. David KastrupSep 8, 2007
  92. Alex RiesenSep 8, 2007
  93. figoSep 24, 2007
  94. David KastrupSep 24, 2007
  95. Steven BurnsSep 25, 2007
  96. David KastrupSep 25, 2007
  97. Syed M RaihanMay 22, 2012
  98. Ian MoltonJun 10, 2010
  99. Jakub NarebskiJun 11, 2010
  100. Dario RodriguezJun 11, 2010
  101. Kristian HøgsbergSep 5, 2007
  102. Lukas SandströmSep 7, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.