And, what name would make the most sense to me, right now, as I imagine this method already exists?”And since the method does not actually exist, you are not constrained by anything other than your intentions.
Consider the code snippet below.
parameters : p_rows type count default ‘100’.
* — — — — — — — — — — — — — — — — — — — — — — — -** CLASS lcl_main DEFINITION* — — — — — — — — — — — — — — — — — — — — — — — -*CLASS lcl_main DEFINITION.
CLASS-METHODS : start.
METHODS : get_data ,display.
CLASS-DATA : lr_main TYPE REF TO lcl_main.
DATA it_mara TYPE mara_tt.
“lcl_main DEFINITION* — — — — — — — — — — — — — — — — — — — — — — — -** CLASS lcl_main IMPLEMENTATION* — — — — — — — — — — — — — — — — — — — — — — — -*CLASS lcl_main IMPLEMENTATION.
CREATE OBJECT lr_main.
— — WRITE SOME CODE HERE……ENDMETHOD.
— — WRITE SOME CODE HERE……ENDMETHOD.
The start() is the public method of the class which is invoked while executing the report.
The other methods get_data() and display() are private methods of the class which are invoked locally by instantiating an object of the class.
We can simply call these methods as functional steps.
The point is that their existence is part of the internal implementation of this class, not part of the way it is used from the outside.
And they do not exist yet.
So when you try to compile this code, the compiler will throw an error stating they do not exist.
They have to be created for the code to be compiled.
That is precisely what we must do next.
So by programming by intention, we allow ourselves to focus purely on how we are breaking down the problem into the overall context of the requirement.
We are not writing one big black chunk of code upfront and executing.
All we are doing here is breaking down the code into functional steps and then creating the steps one by one.
Consider this code snippet below in which I wrote the code for the functional steps get_data() and display().
SELECT * FROM mara INTO TABLE me->it_mara UP TO P_rows ROWS .
DATA : lr_table TYPE REF TO cl_salv_table.
cl_salv_table=>factory( IMPORTING r_salv_table = lr_tableCHANGING t_table = me->it_mara ) .
“displayThere is no change here.
We are simply writing the code in a different way and in a different order.
The concept is simple but the advantages we get out of it are tremendous.
Following are some of the advantages of writing code in this way.
Every Entity is CohesiveOne of the golden rules of programming is that every entity in the code should do only one thing.
And this technique precisely forces us to implement just that.
We say a method is strongly cohesive if the method accomplishes one single functional aspect of overall responsibility.
Single-tasking pretty much builds on the capacity of the human brain.
When we multitask we are less productive and on the other hand, when we focus all our energies on a single task we complete it faster and in a more efficient way.
Programming by intention does precisely that.
Every class, function, and subroutine does only one function making the code robust, clean and modular.
The code is ReadableConsider the code example I had written earlier.
The concept is simple.
· A public method of a class is invoked while starting the program.
· The public method, in turn, calls two private methods.
· Each private method does only one task· The flow is self-explanatory.
Notice that I have not included any comments in the code explaining the purpose.
The flow itself is readable and easy to get to.
Another thing to be noticed here is the names of the methods.
The methods do exactly the way they are named.
Also, since we name the methods before they actually exist, we tend to pick names that express our thinking.
We call names of this type “intention-revealing names” because they disclose the intention of the name.
And the biggest advantage of readability comes while debugging.
The code is written in such a way that bugs are easier to find, track and vanquish.
Since every method does only one thing, you get to the root cause of the problem much faster.
The central, organizing method in Programming by Intention(start() method in this case) contains all the steps but very little or no actual implementation.
That is the key to the readable code.
Refactoring and Enhancing code becomes EasierOne of the biggest ironies of any code is that it can never be frozen.
New requirements come in.
Changes happen in code.
Scope gets modified.
The process is continuous and unavoidable.
So often we end up either refactoring the code (cleaning up a poorly written code) or enhancing the code (adding new functionality).
One of the common techniques for refactoring is the extract method.
The developer takes the piece of troublesome code from the main code and creates it as a separate method having its own variables.
This method after refactored is then called from the same original place.
Programming by intention makes it very easy to extract as the bad code is clearly visible.
Similarly, Programming by Intention can make it easier to enhance your system.
I just need to add the new functionality as a new method in the central organizing method of the code.
Nothing breaks and the functionality is modular by default and can be achieved with a high degree of confidence.
Testing Becomes EffectiveTesting not only becomes effective because of the separation of different aspects of the program into individual methods.
Rather programming by intention helps us in testing by allowing us to do both unit and integration testing at the same time.
There are two possibilities here.
· We unit test every individual private method for the functionality intended.
We know there are vulnerabilities that make them somewhat likely to fail at some point.
For efficiency and security, we want to be able to test them separately from the way they are used.
· We do integrated testing through the central organizing method of the code.
We execute every functional step in sequence and find out if the overall functionality is working as expected.
Note that the desire to test both aspects is forcing us to look at this “Are they really just steps?” issue, which is an important aspect of design.
If this is the case, then we need to revisit our design.
For example, let’s say we determine that the step that displays the data in our earlier code(display()) is not really required as part of the design, we could change the design just enough and so on…And the biggest advantage of programming by intention is the clear visibility of failure patterns in your code.
You can exactly pinpoint the failure patterns which are cropping up again and again during testing and modify the code accordingly.
And Lastly, It Brands Your CodeA lazy developer just keeps on writing lines of code as part of his daily routine.
Once the day ends, he gets “lost” among the “pool” of developers who are supposed to be working on “something” every day and getting billed for the same.
On the other hand, an “active” developer makes his code unique; he brands his code with his unique signature.
This “uniqueness” can be as simple as adding a “bit of code” that makes the user’s life a little easier to as complex as outstanding code optimization which boosts up the performance to significant levels.
Good Programming is a few brief moments of sublime elegance embedded in months of niggling, exacting, precise trivialities.
Make that sublime elegance stand out.
Programming by intention gives you the platform to make your code stand out in all its brilliance.
Your modular way of coding not only earns you accolades but also gives others a readymade platform to add further, learn more and work as a team.
Together we progress towards success.
As Edsger W.
Dijkstra has rightly said.
“Progress is possible only if we train ourselves to think about programs without thinking of them as pieces of executable code.
”About the author-:Ravi Rajan is a global IT program manager based out of Mumbai, India.
He is also an avid blogger, Haiku poetry writer, archaeology enthusiast, and history maniac.
Connect with Ravi on LinkedIn, Medium and Twitter.
This story is published in The Startup, Medium’s largest entrepreneurship publication followed by +430,678 people.
Subscribe to receive our top stories here.