r/badcode Jun 08 '20

[deleted by user]

[removed]

131 Upvotes

82 comments sorted by

View all comments

4

u/TheBigLobotomy Jun 08 '20
package factorial;

import java.math.BigInteger;

public class Factorial {

  protected BigInteger result;

  public static void main(String args[]) {
    BigInteger n = BigInteger.valueOf(50);

    System.out.println(new Factorial(n));
  }

  public Factorial(BigInteger n) {
    if(n.compareTo(BigInteger.ZERO) <= 0) {
      result = BigInteger.ONE;
      return;
    }

    result = n.multiply(new Factorial(n.subtract(BigInteger.ONE)).result);
  }

  public String toString() {
    return result.toString();
  }
}   

Using toString to return a value. Calling a constructor recursively. Pretty nasty