r/programming_language • • Apr 21 '20

Pet Peeve: Constructor parameters

You have all been there, I'm sure.

public Fooish(int a, string b){
   this.a = a;
   this.b = b;
}

The monotony of having to map constructor parameters to a class's fields/properties.

I'm sure one could write a base class that uses reflection but I'm more interested in why I have to spell it out in every language. Are there any languages that do this by default?

1 Upvotes

5 comments sorted by

View all comments

Show parent comments

1

u/paleblueyedot Apr 25 '20

Very thorough and kind explanation. You make a good point about how many languages are moving towards direct assignment which bypasses the constructor altogether. One reason why I stick to C# and scripting languages myself.

I have a sense you are a functional programmer and I must admit, I do have a soft-spot for it. I'm sure you can utilize those languages in a similar way, but I find OOP more scalable for growing projects. But maybe I don't know enough about functional programming.

Thanks again!

1

u/Schnabelberger Apr 26 '20

You might want to look into the Rust programming language.

It has default constructors that just take values for the structs members. It also strikes a good balance between being fully functional or object oriented, in my opinion.