Skip to content

Instantly share code, notes, and snippets.

@weakish
Last active August 23, 2024 15:32

Revisions

  1. weakish revised this gist Aug 23, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion code.md
    Original file line number Diff line number Diff line change
    @@ -56,7 +56,7 @@ public int getFibonacciNumber(int n) {
    // TODO This should probably throw an exception. Or maybe just print
    // a log message?
    return - 1;
    } else if (n == 1) {
    } else if (n == 0) {
    // TODO Generalize the initial conditions?
    return 0;
    } else if (n == 1) {
  2. weakish revised this gist Nov 12, 2013. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions code.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ public int fibonacci(int, x) {
    } else if (x == 2) {
    return 1;
    ) else {
    return fibonacci(x 1) + fibonacci(x -2);
    return fibonacci(x - 1) + fibonacci(x - 2);
    }
    }
    ```
    @@ -35,7 +35,7 @@ public int getFibonacciNumber(int n) {
    case 7: return 13;
    default:
    // good enough for the demo, 1o1
    return 1;
    return - 1;
    }
    }
    ```
    @@ -55,7 +55,7 @@ public int getFibonacciNumber(int n) {
    if (n < 0) {
    // TODO This should probably throw an exception. Or maybe just print
    // a log message?
    return -1;
    return - 1;
    } else if (n == 1) {
    // TODO Generalize the initial conditions?
    return 0;
    @@ -64,10 +64,10 @@ public int getFibonacciNumber(int n) {
    } else {
    // TODO Spend some thime with my family and kids, I've been at work for
    // over 48 hours straight.
    return getFibonacciNumber(n -1) + getFibonacciNumber(n -2);
    return getFibonacciNumber(n - 1) + getFibonacciNumber(n - 2);
    }
    }

    ```

    Code Written At A Large Company
    -------------------------------
  3. weakish created this gist Nov 12, 2013.
    144 changes: 144 additions & 0 deletions code.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,144 @@
    The Six Most Common Species Of Code
    ===================================

    http://www.willa.me/2013/11/the-six-most-common-species-of-code.html


    code Written By A CS 101 Student
    --------------------------------

    ```java
    public int fibonacci(int, x) {
    if (x == 1) {
    return 1;
    } else if (x == 2) {
    return 1;
    ) else {
    return fibonacci(x — 1) + fibonacci(x -2);
    }
    }
    ```


    Code Written At A Hackathon
    ---------------------------

    ```java
    public int getFibonacciNumber(int n) {
    switch(n) {
    case 1: return 1;
    case 2: return 1;
    case 3: return 2;
    case 4: return 3;
    case 5: return 5;
    case 6: return 8;
    case 7: return 13;
    default:
    // good enough for the demo, 1o1
    return1;
    }
    }
    ```

    Code Written At A Startup
    -------------------------

    ```java
    // TODO add Javadoc comments
    /**
    * getFibonacciNumber
    */
    // TODO Should we move this to a different file?
    public int getFibonacciNumber(int n) {
    // TODO Stack may overflow with recursive implementation, switch over to
    // iteration approach at some point?
    if (n < 0) {
    // TODO This should probably throw an exception. Or maybe just print
    // a log message?
    return -1;
    } else if (n == 1) {
    // TODO Generalize the initial conditions?
    return 0;
    } else if (n == 1) {
    return 1;
    } else {
    // TODO Spend some thime with my family and kids, I've been at work for
    // over 48 hours straight.
    return getFibonacciNumber(n -1) + getFibonacciNumber(n -2);
    }
    }


    Code Written At A Large Company
    -------------------------------

    ```java
    /**
    * getFibonacciNumber is a method that, given some index n, returns the nth
    * Fibonacci number.
    * @param n The index of the Fibonacci number you wish to retrieve.
    * @return The nth Fibonacci number.
    */
    public CustomInteger64 getFibonacciNumber(CustomInteger64 n) {
    FibonacciDataViewBuilder builder =
    FibonacciDataViewBuilderFactory.createFibonacciDataViewBuilder(
    new FibonacciDataViewBuilderParams(n, null, null, 0, null));
    if (builder == FibonacciDataViewBuilderConstants.ERROR_STATE) {
    throw new FibonacciDataViewBuilderFactoryException();
    }
    FibonacciDataView dataView = builder.GenerateFibonacciDataView(this);
    if (dataView == FibonacciDataViewConstants.ERROR_STATE) {
    throw new FibonacciDataViewGenerationException();
    }
    return dataView.accessNextFibonacciNumber(null, null, null);
    }
    ```


    Code Written By A Math Ph.D.
    ----------------------------

    ```java
    public int getFibonacciNumber(int n) {
    return (int divide(subtract(exponentiate(phi(), n), exponentiate(psi(), n)),
    subtract(phi(), psi()));
    }

    public double exponentiate(double a, double b) {
    if (equal(b, zero())) {
    return one();
    } else {
    return multiply(a, exponentiate(a, subtract(b, one())));
    }
    }

    public double phi() {
    return divide(add(one(), sqrt(add(one(), one(), one(), one(), one()))),
    add(one(), one()));
    }

    public double psi() {
    return subtract(one(), phi());
    }
    ```

    Code Written By Your Cat
    ------------------------

    ```java
    public static final int UNITE = 1;
    public static final int UNITED = 2;

    // meowwwww meow
    public int meow(int KITTENS_OF_THE_WORLD) {
    // MEOW
    if (KITTENS_OF_THE_WORLD < UNITED) {
    return KITTENS_OF_THE_WORLD;
    } else {
    // meeoowwwwwwwww
    // meooowwwwwwwwwwwwwwwwwwwwww
    return meow(KITTENS_OF_THE_WORLD - UNITE)
    + meow(KITTENS_OF_THE_WORLD - UNITED);
    }
    }
    ```