100 Days of Improving Coding Style
Day 4: Good Object-Oriented Programming Style: Declaring Classes
Today, let’s talk about good OOP (Object-Oriented Programming) Style in terms of declaring classes. I know, you’ve heard OOP, and you know everything about it. In fact, you could tell me all the minute details about inheritance, abstract classes, and interfaces.
Great! But, what about actually having good style when creating classes for objects. Let’s walk through some important tips for having good OOP style.
First off: declare all fields and constants at the top!!

Now, I know this standard and most people know this, but you’ll be surprised to see a good number of people declaring all their fields at the bottom, or in between when they need them.
That is really, really annoying for someone who has to read your code. Declaring all your fields at the top makes it obvious what variables your object needs to define itself. It’s clear, and I’ll go as far as to say it’s the right way to do OOP.
Second: Constructors (Inits) RIGHT AFTER FIELDS!!
For everyone’s sake, just put your constructors(inits) as the first methods you declare in your class. Why? Because writing a class is like writing a story about your object. After you list the characteristics of your objects (the fields), explain how your object is defined using those fields. And that makes put the constructors right after those fields.
Constructors are where you instantiate those fields declared at the beginning of your class, so in the sense of having a good “flow” in your code, it makes sense to put the constructors right after the fields.
Third: Methods, Methods, METHODS
The more methods the better. Just do it. Why? Because, I’d rather see really clear method names that describe what you’re doing, rather than combing through individual lines of your code trying to figure out what you’re doing.
Use methods within methods. DON’T try to do multiple things at once. In fact, don’t even try to do two things at one time. Make methods for every action you want to implement for your class.
Trust me, it makes your code more readable and it will be easier for other people to use your class.
Happy Coding! See you guys tomorrow with another tip!