Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Code can be underabstracted, but it can also be overabstracted - and abstracted with the wrong abstractions. And fixing the latter sometimes involves a temporary stay at "untestable mess of big God classes" when you remove the bad abstractions to clear the way for creating better ones. Not because it's the best code - far from it - but because it's slightly less terrible code.

> And using public fields in a non trivial project is a sure receipt for disaster.

Ergo, all non trivial C projects are disasters? Well, maybe, but I disagree on the reasons.

Language enforced encapsulation is a useful tool, but some people take it to the deep end and assume that if their math library's 3D vector doesn't hide "float z;" in favor of "void set_z(float new_z);" and "float get_z() const;" (one of which will probably have a copy+paste bug because hey, more stupid boilerplate code), they'll have a sure receipt for disaster. Which I suspect you'd agree is nonsense - but would also follow from reading your words a little too literally.



In my experience, in quite big projects, people had a tendency to mutate objects in the wrong place and in the wrong reasons. A field encapsulated in a getter without setter certainly helps.


But if it has to be able to be mutated in some cases, you're stuck. This is a place where Scheme's parameterize may be useful: Define a closure with the setter in scope, and write it so that when it's called with a lambda as an arg, it will raise an error unless those certain conditions are met, in which case, it will use parameterize to make the setter available in the lambda's scope. Or I suppose it could pass it in, which would be slightly simpler...


That would be a final field. But generally I agree with you and its by far the pattern I use most.


The issue is, for example, if you have for example a class encapsulating a bunch of flags, and allowing the user to either set each flag seperately, or a bitfield representing all of it.

In C, you might be able to do some magic, but in Java, you’ll need setters and getters there – you can’t even do final fields there.

Luckily, in C#, you can just use accessors, and maybe in Java with Lombok, too.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: