Slashdot Mirror


Ask Slashdot: What Do You Consider Elegant Code?

lxrslh writes: "Since the dawn of computing, we have read about massive failed projects, bugs that are never fixed, security leaks, spaghetti code, and other flaws in the programs we use every day to operate the devices and systems upon which we depend. It would be interesting to read the code of a well-engineered, perfectly coded, intellectually challenging program. I would love to see the code running in handheld GPS units that first find a variable number of satellites and then calculate the latitude, longitude, and elevation of the unit. Do you have an example of a compact and elegant program for which the code is publicly available?"

5 of 373 comments (clear)

  1. Duff's Device by smittyoneeach · · Score: 4, Informative
    --
    Get thee glass eyes, and, like a scurvy politician, seem to see things thou dost not.--King Lear
    1. Re:Duff's Device by Anonymous Coward · · Score: 3, Informative

      Uh... that Wikipedia article shows 3 lines of code, which it describes as "straightforward":

      Straightforward code to copy items from an array to a memory-mapped output register might look like this:

      do { /* count > 0 assumed */
              *to = *from++; /* Note that the 'to' pointer is NOT incremented */
      } while(--count > 0);

      Then this reference implementation of Duff's device:


      send(to, from, count)
      register short *to, *from;
      register count;
      {
              register n = (count + 7) / 8;
              switch(count % 8) {
              case 0: do { *to = *from++;
              case 7: *to = *from++;
              case 6: *to = *from++;
              case 5: *to = *from++;
              case 4: *to = *from++;
              case 3: *to = *from++;
              case 2: *to = *from++;
              case 1: *to = *from++;
                      } while(--n > 0);
              }
      }

      Then this penny drops:

      When numerous instances of Duff's device were removed from the XFree86 Server in version 4.0, there was an improvement in performance

  2. Re:Complete Spectrum Rom Disassembly by Anonymous Coward · · Score: 4, Informative

    ftp://www.worldofspectrum.org/pub/spectrum/books/CompleteSpectrumROMDisassemblyThe.pdf

  3. GPS code by tlambert · · Score: 3, Informative
  4. Re:GPS? Are you kidding? by jonwil · · Score: 4, Informative

    As someone who has a copyright assignment on file with the FSF for GCC and actually tried to write an implementation of the Visual C++ __declspec(thread) keyword for GCC-on-windows (i.e. proper OS-provided thread-local-storage support) and got lost somewhere deep in the code that actually converts the intermediate representation into assembler (I needed to do stuff to it so it would correctly access the thread-local-storage data when an access to a thread local variable was made) I question your statement that GCC is well-written, elegant or easy to understand...