Go to mobile version

You are viewing [info]jackyf's journal

Previous Entry | Next Entry

bureaucratic programming

feterny-bird
Suppose that you need to write an interface to a function which draws triangles. It could look like this: (the language is a C++-based pseudocode)

void rawDrawTriangle(size_t a, size_t b, size_t c) { ... }
bool isValidTriangle(size_t a, size_t b, size_t c)
{
  return (a + b > c) && (a + c > b) && (b + c > a);
}

void drawTriangle(size_t a, size_t b, size_t c)
{
  if (max(a, b, c) > MAX_LINE_LENGTH)
  {
    throw("one of sides is too big");
  }
  if (!isValidTriangle(a, b, c))
  {
    throw("invalid triangle");
  }
  rawDrawTriangle(a, b, c);
}

void userFunction()
{
  ...
  drawTriangle(3, 5, 7);
  ...
}


And the following is how a bureaucratic version of the code could look like:

class Certificate
{
  time_t getCreationTime() const { ... }
  Certificate() { ... }
};

class TriangleIsValidCertificate: public Certificate
{
 public:
  const size_t a;
  const size_t b;
  const size_t c;
 private:
  TriangleIsValidCertificate(size_t a, size_t b, size_t c) { ... }
 friend class TriangleCertificationAuthority;
};

class TriangleCertificationAuthority
{
  static TriangleIsValidCertificate getTriangleIsValidCertificate(size_t a, size_t b, size_t c)
  {
    msleep(random() * 2);
    if ((a + b > c) && (a + c > b) && (b + c > a))
    {
      if (a == b && a == c)
      {
        msleep(random() * 10); // hm, suspicious query
      }
      return TriangleIsValidCertificate(a, b, c);
    }
    else
    {
      throw("invalid triangle");
    }
  }
};

class LineIsDrawableCertificate: public Certificate
{
 public:
  const size_t length;
 private:
  LineIsDrawableCertificate(size_t l) { ... }
 friend class LineCertificationAuthority;
};

class LineCertificationAuthority
{
  static LineIsDrawableCertificate getLineIsDrawableCertificate(size_t length)
  {
    msleep(rand());
    if (length <= MAX_LINE_LENGTH)
    {
      return LineIsDrawableCertificate(length);
    }
    else
    {
      throw("the line is too long");
    }
  }
};

void drawTriangle(size_t a, size_t b, size_t c, LineIsDrawableCertificate lineCerts[3], TriangleIsValidCertificate tivCert)
{
  msleep(rand() * 5);
  if (lineCerts[0].length != a || lineCerts[1].length != b || lineCerts[2].length != c)
  {
    throw("your application is rejected");
  }
  if (tivCert.a != a || tivCert.b != b || tivCert.c != c)
  {
    throw("your application is rejected");
  }
  if (time() - tivCert.getCreationTime() > '60 milliseconds')
  {
    throw("your application is rejected");
  }
  msleep(rand());
  rawDrawTriangle(a, b, c);
}

void userFunction()
{
  ...
  size_t a = 3, b = 5, c = 7;
  auto aCert = LineCertificationAuthority::getLineIsDrawableCertificate(a);
  auto bCert = LineCertificationAuthority::getLineIsDrawableCertificate(b);
  auto cCert = LineCertificationAuthority::getLineIsDrawableCertificate(c);
  auto tviCert = TriangleCertificationAuthority::getTriangleIsValidCertificate(a, b, c);
  drawTriangle(a, b, c, array(aCert, bCert, cCert), tviCert);
  ...
}

Comments

( 3 comments — Leave a comment )
[info]morbow wrote:
Feb. 17th, 2012 02:03 am (UTC)
Бюрократов - к стенке! Вечно они требуют тонны никому не нужных бумажек и тянут время!
[info]turbonomad wrote:
Feb. 17th, 2012 05:39 pm (UTC)
Ты забыл описание в комментариях для каждого метода, логирование операций в трейс и хендлинг всех исключений с оповещением сервера хранения ошибок.
[info]jackyf wrote:
Feb. 17th, 2012 05:52 pm (UTC)
Да. Но я всё же хотел бы, чтобы большинство читателей смогло дочитать до конца :)

( 3 comments — Leave a comment )