DEV Community

Discussion on: How did you break the habit of writing procedural code?

Collapse
 
sunflower profile image
sunflowerseed • Edited

Guess what, procedural is like actions... the physics, of particles flying around, or how they interact, due to some electrostatic force, and so forth.

When it is object oriented programming, it is like material, together with the actions. So you can say

particle.move()
Enter fullscreen mode Exit fullscreen mode

or

animal.giveSound()
Enter fullscreen mode Exit fullscreen mode

You are packaging the properties (states) of the material (the x, y coord, etc) together with all the actions it know to perform, into a specification, called a "class". The class is a blueprint, and when you instantiate the object, the object is born. It has properties, and it has all the "methods", or actions it know how to perform.

So the actions are still procedural.

In some situations, your "world" is just a number or a few numbers, and your just write everything procedural, because there is no need to "package" the states and actions into an object. Your program is the "world" and is the "object" already.

When you write programs, a lot of time you are concerned about the actions... and about packaging these actions together with the states into an object... that's not as important and it may depend on the situation. I saw people writing a function by write a class and all it does is one method, and so he either use it as a class method or instantiate the object and invoke obj.provideService() and it is kind of silly.

Note that the "action" is the method. In Smalltalk, you send the object a message, such as you tell the animal to giveSound, and the animal perform the giveSound action... so in some language, it is "sending the object a message". In some language, they call it "invoking a method on an object", and these are very similar ideas.

You mentioned when solving code challenges, you write it as a procedure. That's actually quite natural. For example, if given an array, and they tell you to write Quicksort, then you probably are not going to create a "Quicksort" object that can sort an array. You are writing the action, so while you can add a method to the array class such as array.quicksort(), you wouldn't usually write it as quicksort.sort(array) with quicksort being the object or even the class. You might write it as a library or module and use Sort.quicksort(array) with Sort being the class and quicksort and mergesort being the class methods. But it really depends on the situation. For many coding challenges, they want to see your algorithm skills, so it is natural to write it as procedural or functional.

Collapse
 
jasterix profile image
Jasterix

Thanks for the detailed response! You're right that may be the case with code challenges. But I noticed when working with my team (in an older codebase), it was challenging for me to write code within that context, especially when it to using fields and properties, extending other classes and working with arguments

I wish I could share the code as an example :(