How to be good in writing in OOP?

Hi, I am finding that I am in bottleneck. I am unable to write the code clean in OOP way. I always amazed by Mosh for the way he code. He can code in such simple and understandable way.

I am wondering which should I focus or is there any sequence to learn for the portion of Design Pattern, OOP, UML, or anything I missed out.

In simpler word, is there any clear path to achieve the goal of writing clean code and understandable program? I know practice is best, but I believe practice in a correct direction can make whole thing become more efficient.

Btw, sorry for broken english.

Mosh, by all means, is very experienced and getting to his level will take a lot of work, so do not get discouraged.

Mosh does have a course for clean coding, and studying design patterns or OOP would definitely help with writing clean code but there is no one perfect solution for clean coding. When you write code, analyze it after.

Ask yourself, am I re-using certain methods or variables in multiple areas? If I want to perform similar tasks in the future will I have to copy and paste all this code? Is this code too specific/would I be required to re-write or change this code if I wanted to use it elsewhere? Am I passing a bunch of variables between classes or passing multiple arguments into methods? If so, perhaps consider capturing this in a class/object.

One problem I ran into when starting out was I tried to make my code clean but in doing so, fell into a pattern of procedural programming; I would pass tons of variables in methods that cascaded into other methods and became a mountain of methods that depended on each other. It was a perfectly usable program but when I needed a new variable or new method, oh boy was that a nightmare to add.

So my suggestion, when you write a new program first try to look for areas where you can re-use code. i.e. That hardcoded variable you’re using in a method, can you refactor it to make it dynamic or easier to set vs. having to set it in multiple places? Or that filter you just used with a specific object or variable, can you refactor it to make it reusable if you need it again? In addition, try to look for areas that you could encapsulate into a class/object. One of the biggest flags for me to consider using an object is if I have to move around multiple variables or if I notice I am re-using code.

Honestly, even if you write your next program and you can only create one new object/class. That’s great. The more you do it, the easier it will be for you to notice when you should be using an object/class.

Hi, thank you so much for giving so much of your suggestions and willing to share all these to me!
I think consistently review the code that written as you suggested might be a good practice for me in current stage!
Again, thanks lot!
Have a nice day