How to justify anything as a developer

One way of calculating pi is from the area of a circle, you could even embedded that image in the code. You /could/ then suggest someone call this as an external program, to calculate pi from a script. This is clearly a stupid thing to do.

Here’s a badly written program to do it:

#include <stdio.h>
#include <math.h>

char mask [20][20] = {

// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0},
  {0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},
  {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
  {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},
  {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},
  {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
  {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
  {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
  {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
  {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
  {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
  {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
  {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},
  {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},
  {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
  {0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},
  {0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

};


int main() {

  int area=0;

  for(int x=0;x<20;x++) {
    for(int y=0;y<20;y++) {
      if(mask[x][y] == 1) area++;
    }
  }

  printf("pi: %f\n",(float)area/((17/2)*(17/2)));
}

And here are some ways of justifying it:

Hmm perhaps we should refactor this, maybe there’s a better way?
It works so it’s fine, there are more important things for us to be working on.

Why don’t you just use a hardcoded value, or value from a library?
That’s crazy, you should /always/ avoid magic numbers in your code. How do you know a hardcoded value is correct? You’re developers get no intuitive feeling for the value of pi. They don’t know how to increase the precision of the value. I’m sorry that’s just nuts.

The answer is WRONG it calculates pi as around 3.7
There’s no /right/ answer, all values of pi are approximations. 3.7 is good enough for many applications. It’s also trivial to modify this program to use larger circles and get a better approximation.

OK, but there are FASTER ways of calculating pi
Have you benchmarked it? (people are generally too lazy to benchmark). If it’s really a performance bottleneck you can calculate the value once and store it. In any case, don’t be sucked into premature optimisation. Developer time is more expensive than compute time.

But it’s completely UNCLEAR
I disagree, this method gives the developer a visual representaion of the value of pi. They can intuatively see where the value is coming from. What’s more it’s easy from them to change to code and add larger circle images as required. There are even peer reviewed images of circles all over the Internet which can easily be incorporated.

You’re nuts
You just don’t understand the business case for doing it this way, you’re too wrapped up in the technical issues to see the big picture. You’ll understand better when you’ve been programming for (as long|at the same scale|with large teams) as me.

You can attempt to justify pretty much any programming position by appealing to: utility, premature optimisation, readability and intuition, business cases, and the old standby: experience.

3 thoughts on “How to justify anything as a developer

  1. brilliant. please accept my submission, written in Java, a program that, given N – the radius of the required circle, generates c code to compute pi in your excellent manner. Perhaps someone could write a program to convert my Java program to C. :)

    Use: java Circle N, where n is radius of circle.
    Eg: java Circle 1000


    public class Circle {

    public static void main(String[] args) {
    int N = Integer.parseInt(args[0]);

    System.out.println("#include ");
    System.out.println("#include ");
    System.out.println("\nchar mask [" + N + "][" + N + "] = {\n");
    System.out.print("\t//");

    for (int i = 0; i < N; i++)
    System.out.print(" " + i);
    for (int i = 0; i < N; i++)
    System.out.print(" " + i);
    System.out.println();

    for (int i = -N; i <= N; i++) {
    System.out.print("\t{");
    for (int j = -N; j <= N; j++) {
    if (i*i + j*j <= N*N) System.out.print("1");
    else System.out.print("0");
    if (j <= N-1) System.out.print(",");
    }
    if (i <= N-1) System.out.println("},");
    else System.out.println("}");
    }
    System.out.println("};\n");

    System.out.println("int main() {\n\n\tint area=0;\n\n\tfor(int x=0;x<" + N + ";x++) {\n\t\tfor(int y=0;y<" + N + ";y++) {\n\t\t\tif(mask[x][y] == 1) area++;\n\t\t}\n\t}\n\n\tprintf(\"pi: %f\\n\",(float)area/((17/2)*(17/2)));\n}");
    }
    }

  2. Thanks! You’re code requires two small modifications, the “r squared” values need to change with the radius of the circle, and the circle is twice the diameter of the input parameter, with these changes it works fine. I was able to compile and run it online here:

    http://www.compileonline.com/compile_java_online.php

    I will now incorporate the following into our new workflow:

    A call to this php script to compile your Java, output C, compile that on the local machine and then run it to calculate the value of pi.

    Thank you for your more flexible solution. :)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>