PrG_Attribute_Creation.xml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
  4. <sect1 id="Attribute_Creation" role="nobrk">
  5. <title>Attribute Creation</title>
  6. <sect2 id="Similarities_and_Differences">
  7. <title>Similarities and Differences</title>
  8. <para>The similarities come from the fundamental requirement of solving
  9. any data processing problem: First, understand the problem.</para>
  10. <para>After that, in many programming environments, you use a “top-down”
  11. approach to map out the most direct line of logic to get your input
  12. transformed into the desired output. This is where the process diverges in
  13. ECL, because the tool itself requires a different way of thinking about
  14. forming the solution, and the direct route is not always the fastest. ECL
  15. requires a “bottom-up” approach to problem solving.</para>
  16. </sect2>
  17. <sect2 id="Atomic_Programming">
  18. <title>“Atomic” Programming</title>
  19. <para>In ECL, once you understand what the end result needs to be, you
  20. ignore the direct line from problem input to end result and instead start
  21. by breaking the issue into as many pieces as possible—the smaller the
  22. better. By creating “atomic” bits of ECL code, you've done all the known
  23. and easy bits of the problem. This usually gets you 80% of the way to the
  24. solution without having to do anything terribly difficult.</para>
  25. <para>Once you've taken all the bits and made them as atomic as possible,
  26. you can then start combining them to go the other 20% of the way to
  27. produce your final solution. In other words, you start by doing the little
  28. bits that you know you can easily do, then use those bits in combination
  29. to produce increasingly complex logic that builds the solution
  30. organically.</para>
  31. </sect2>
  32. <sect2 id="Growing_Solutions">
  33. <title>Growing Solutions</title>
  34. <para>The basic Attribute building blocks in ECL are the Set, Boolean,
  35. Recordset, and Value Attribute types. Each of these can be made as
  36. “atomic” as needed so that they may be used in combination to produce
  37. “molecules” of more complex logic that may then be further combined to
  38. produce a complete “organism” that produces the final result.</para>
  39. <para>For example, assume you have a problem that requires you to produce
  40. a set of records wherein a particular field in your output must contain
  41. one of several specified values (say, 1, 3, 4, or 7). In many programming
  42. languages, the pseudo-code to produce that output would look something
  43. like this:</para>
  44. <programlisting>Start at top of MyFile
  45. Loop through MyFile records
  46. If MyField = 1 or MyField = 3 or MyField = 4 or MyField = 7
  47. Include record in output set
  48. Else
  49. Throw out record and go back to top of loop
  50. end if and loop
  51. </programlisting>
  52. <para>While in ECL, the actual code would be:</para>
  53. <programlisting>SetValidValues := [1,3,4,7]; //Set Definition
  54. IsValidRec := MyFile.MyField IN SetValidValues; //Boolean
  55. ValRecsMyFile := MyFile(IsValidRec); //filtered Recordset
  56. OUTPUT(ValRecsMyFile);
  57. </programlisting>
  58. <para>The thought process behind writing this code is:</para>
  59. <para>“I know I have a set of constant values in the spec, so I can start
  60. by creating a Set attribute of the valid values...</para>
  61. <para>“And now that I have a Set defined, I can use that Set to create a
  62. Boolean Attribute to test whether the field I'm interested in contains one
  63. of the valid values...</para>
  64. <para>“And now that I have a Boolean defined, I can use that Boolean as
  65. the filter condition to produce the Recordset I need to output.”</para>
  66. <para>The process starts with creating the Set Attribute “atom,” then
  67. using it to build the Boolean “molecule,” then using the Boolean to grow
  68. the “organism”—the final solution.</para>
  69. </sect2>
  70. <sect2 id="Ugly_ECL_is_Possible_Too">
  71. <title>“Ugly” ECL is Possible, Too</title>
  72. <para>Of course, that particular set of ECL could have been written like
  73. this (following a more top down thinking process):</para>
  74. <programlisting>OUTPUT(MyFile(MyField IN [1,3,4,7]));</programlisting>
  75. <para>The end result, in this case, would be the same.</para>
  76. <para>However, the overall usefulness of this code is drastically reduced
  77. because, in the first form, all the “atomic” bits are available for re-use
  78. elsewhere when similar problems come along. In this second form, they are
  79. not. And in all programming styles, code re-use is considered to be a good
  80. thing.</para>
  81. </sect2>
  82. <sect2 id="Easy_Optimization">
  83. <title>Easy Optimization</title>
  84. <para>Most importantly, by breaking your ECL code into its smallest
  85. possible components, you allow ECL’s optimizing compiler to do the best
  86. job possible of determining how to accomplish your desired result. This
  87. leads to another dichotomy between ECL and other programming languages:
  88. usually, the less code you write, the more “elegant” the solution; but in
  89. ECL, the <emphasis role="underline">more</emphasis> code you write, the
  90. better and more elegant the solution is generated for you. Remember, your
  91. Attributes are just <emphasis role="bold">definitions</emphasis> telling
  92. the compiler <emphasis role="underline">what</emphasis> to do, not
  93. <emphasis role="underline">how</emphasis> to do it. The more you break
  94. down the problem into its component pieces, the more leeway you give the
  95. optimizer to produce the fastest, most efficient executable code.</para>
  96. </sect2>
  97. </sect1>