r/badcode • • Dec 03 '22

rust Yes, that is supposed to be a constructor.

Post image
543 Upvotes

42 comments sorted by

View all comments

Show parent comment

6

u/thejamibu Dec 04 '22

Just realised that the title is "Yes, that is supposed to be a constructor". So yeah this code won't work as a constructor since it requires the object to exist.

1

u/N0Zzel Dec 04 '22

Yes I know how the self argument works. What I'm saying is the only thing wrong here is the name of the function. Maybe you noticed that op left off the return type? For all we know it's returning the unit type and whoever wrote it is breaking convention and OP misunderstood the function because of the convention breaking name

Breaking convention doesn't necessarily mean the code is bad, it's like saying writing ergonomic bindings in a sys crate is bad. It's not bad it just breaks convention.

OP has left important details out

3

u/thejamibu Dec 04 '22

Ah I see what you mean, I was just assuming that this is OP's own code and, given the title, they 100% intended it to be a constructor in the manner you would normally see with "new" i.e pub fn new(text: String) -> Self.

If it isn't intended to be a constructor then it's not such a big deal, although I would still argue that the name is a bit confusing given the prevalence of the pattern above.