Private fields and accessor placing in classes
Where’s the best place to put your private fields and accessors (getters and setters) in classes? Well, the traditional approach is to group all private fields together, all accessors together and all methods together. That is quite intuitive yet it presents some problems.
A private field is almost always associated with accessors. In almost every OOP language (even more with C# with its Properties feature), you have a tight coupling between a private fields and accessors. It becomes an inconvenience to be modifying a field and an accessor at the same since you have to switch between different parts of the source. You gotta split the screen or go up and down and up and down again. So darn stressful.
It’s a good programming practice to place declaration of variables near if not just before its use. Why declare a variable way in the start of a method and just use it at the end? You gonna make future programmers go “Where the fuck did he use that variable?!”
That’s why I recommend placing private fields declarations just before their accessors. Everything is in on one screen! ONE SCREEN! Don’t make me look for its declaration on other parts of the file. Damn it! I’ve got a lot of articles (funny ones usually) to read. I can’t waste time doing this! I want everything there when I need to make my changes.
People would say “blah blah it’s harder to look for the private fields blah blah blah its not organized enough blah blah blah,” and so on. Yeah but a well structured class will rarely retrieve and set private fields directly because you have accessors to do that for you. Get that? ACCESS-ors! Getters and setters. If you’re not gonna use the accessors then just get rid of them and just expose the field as public. Better! The only time you’ll need a separate grouping for private fields is when you have private fields with no accessors.
Not organized enough? It’s still organized since you can expect to have the field-accessor pairing all together in one place. Organization doesn’t only mean putting all the same things together. The organization has to be functional as well.
Now, I just wanna say this: People are just so darn afraid to change their poor programming habits because it’s been what they are “used to” or it’s what been “taught in school” or “the standard thingie sez so” or it’s been “like that since time began” or “shut up, I’m more senior than you.” I’m not saying it’s bad to conform to standards, but I can’t help but think that following a bad standard is as bad or is worse than having no standards at all.
