In Java I use getters/setters when I have simple models/pojos. I find that the code becomes self-documenting when you do it this way. Calling getName() will return a name, I don’t need to care how it’s mapped to some database and so on.
Problems rise when using languages where getters and setters start feeling clunky, like in Python, and I often hear people saying that they are bad. For example some time a go I had a PHP project in which some of the data was just queried from the database and column values mapped to the objects/dictionaries. What I found out was that code like this was annoyingly hard to read, you can’t really just read the code, you read the code, then you notice that the values are fetched from the database and now you have to look through the database schema all the time to understand it. When all you could do is just look at the class definition and knowing that there won’t be any undocumented magic keys there.
So my question is how do you guys document code without getters and setters?
In my opinion, there shouldn’t be any undocumented attributes in a class. PHP and other languages allow you to just stick attributes on a class from anywhere, whether they’ve been defined in the class or not. I think that’s bad practice for the reasons you describe and more:
It’s hard to read.
It makes it harder for other programmers (including your future self) to understand what’s going on.
It prevents auto-complete functionality in IDEs from working.
It often makes the domain layer too dependent on the persistence layer.
Whether you use getters and setters to access the defined attributes of a class is a little more fungible to me. I like things to be consistent, so if I have a class that has a getChildren() method to lazy load some array of objects, then I don’t make the $children attribute public, and I tend to make other attributes private as well. I think that’s a little more a matter of taste, but I find it annoying to access some attributes in a class directly ($object->name;) and others by getters/setters.