Slashdot Mirror


Ask Slashdot: What Makes Some Code Particularly Good?

itwbennett writes: When developers talk about what makes some source code particularly 'good,' a handful of qualities tend to get mentioned frequently (functional, readable, testable). What would you add to this list?

2 of 298 comments (clear)

  1. Re:must fail by DickBreath · · Score: -1, Flamebait

    > Code needs to fail in an easy to understand and predictable way.

    Being written in C++ is a sure way to guarantee both!

    Did it fail in an easy to understand way? Yes. It is easy to understand that it failed because it was written in C++.

    Was it predictable that it would fail? Again, yes.

    :-)



    Those who fail to learn from history are doomed to be promoted to a decision making role.

    --

    I'll see your senator, and I'll raise you two judges.
  2. Compactness and Readability by UnknownSoldier · · Score: -1, Flamebait

    Let's let at the clusterfuck of Boost's CRC code
    1109 lines of over-engineered C++ crap for a simple CRC32 function!?!?

    Now compare that to these simple 27 lines of C/C++ code.

    #include <stdint.h>
     
    const uint32_t CRC32_REVERSE = 0xEDB88320; // reverse = shift right
    const uint32_t CRC32_VERIFY = 0xCBF43926; // "123456789" -> 0xCBF43926
    /* */ uint32_t CRC32_Table[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // i.e. 0x00000000, 0x77073096,
     
    void crc32_init()
    {
      for( short byte = 0; byte < 256; byte++ )
      {
          uint32_t crc = (uint32_t) byte;
          for( char bit = 0; bit < 8; bit++ )
              if( crc & 1 ) crc = (crc >> 1) ^ CRC32_REVERSE; // reverse/reflected Form
              else /* = 0*/ crc = (crc >> 1);
          CRC32_Table[ byte ] = crc;
      }
      if( CRC32_Table[8] != (CRC32_REVERSE >> 4))
          printf("ERROR: CRC32 Table not initialized properly!\n");
    }
     
    uint32_t crc32_buffer( const char *pData, uint32_t nLength )
    {
      uint32_t crc = (uint32_t) -1 ; // Optimization: crc = CRC32_INIT;
      while( nLength-- > 0 )
          crc = CRC32_Table[ (crc ^ *pData++) & 0xFF ] ^ (crc >> 8);
      return ~crc; // Optimization: crc ^= CRC32_DONE
    }

    Typical bloated code solves some theoretical "general purpose" solution. Good code does one thing well:

    It communicates clearly what it is trying to do.

    _When_ was the last time you actually needed a different CRC function from the standard 32-bit one?