As practice, I'm planning to implement a multivariable calculator library. The idea is that you can define a function, such as
f(x, y) = sin(x)^2 + 3*y^3, and then you can evaluate it with specific variable values (e.g.
f(4, 3)) or take the derivative with respect to a certain variable and get another function. The idea is that it might be used by a physics program or a calculator app that would more directly expose the functionality.
I'm not sure how to build the best interface. Using the style I'm most familiar with, I might define the above function with
Function f = new Sum(new Exponent(new Sin(new Variable("x")), 2), new Product(new Constant(3), new Exponent(new Variable("y"), 3), but that's very long compared to the typical mathematical shorthand. Here are a few ways I could imagine the API working:
Function f = new Sum(new Exponent(new Sin(new Variable("x")), 2), new Product(new Constant(3), new Exponent(new Variable("y"), 3)(same as before)
Function f = Sum.create(Exponent.create(Sin.create(Variable.create("x")), Constant.create(2)), ...(same as before but with static builder methods)
Function f = sum(power(sin(getVariable("x")), getConstant(2)), product(getConstant(3), power(getVariable("y"), constant(3)))(shorter, but requires quite a few static imports)
Function f = FunctionGenerator.parse("sin(x)^2 + 3*y^3")
I like 4 because it's so much shorter, but I'm wary because I very rarely see it used; the two examples I've seen that jump to mind are Regex and MySQL, and those are both basically porting older text-based languages. Regex especially seems to make sense for a text-based API because it's used to text processing.
In short, I like #4 but I'm having a hard time justifying to myself this departure from convention.
(Note: I know that option #4 will require a parser. I've used ANTLR before, so this shouldn't be a problem.)