Thursday, December 4, 2014

Comments and blocks

OK, so far we've gone through how and what to store in memory and we know that to use them in our program, we need methods.

Methods, means where all the algorithms executed ?
Perfect, its exactly the way how you define a method.

OK. So, what is there in method ?
Method can have many responsibilities to take care.
Like, you define your algorithm of a counter as following,

  • Store 0 to variable a
  • Each time the method is invoked, it should add 1 to a
So, that's a simple algorithm.
Now, in a method, you define a variable and increment it on each method invoke. Method knows how it should behave like.

OK. I got it. So, how to do this ?

Methods use a small set of control commands, which determines how it works.
Next, we are going into detail.

Blocks: block is the simplest of all the control statements. Blocks group a set of instructions in a single statement.
A real life example, you wake up in the morning and get your breakfast. Now, that's a big set of instructions.
  1. take brush
  2. put some paste on brush
  3. brush
  4. take bread
  5. boil eggs
  6. boil milk
  7. eat bread and egg
  8. drink milk

 Now, we define this bigger block in smaller blocks,

  • <brush your teeth>
    Now, this block takes care of your activity of brushing. Basically the set of first 3 instructions in the bigger set.
  • <prepare your breakfast>
    This block takes care of activities of preparing the breakfast. Instructions 4,5,6 of the bigger set.
  • <have breakfast>
    This block is where you take your breakfast. The last 2 instructions of the bigger set.

Block has the following format.
           <statement 1>  
           <statement 2>  
           <statement 3>  
           <statement 4>  
Block consists of a sequence of statements enclosed within '{' and '}'.
Blocks also can contain nothing at all. These types of blocks are called empty blocks. Empty blocks are at times useful.
Below is an empty block.

Java is free format language. So, you can put statements anywhere in blocks. We can put all instructions in a single line and can execute it or we can put some in a single line and then put some other instructions in another line or we can put single instruction in a single line. So, all the following three are legal;

All statements in a single line:
 {this.message = message; this.msg = message; this.getClass();}  

Statements in two lines:
 {this.message = message; this.msg = message;  

Each statement in a single line:
           this.message = message;  
           this.msg = message;  

All the three programs run exactly the same way. So, you can stick to either of them.
But as a good development practice, you should follow the third practice, which gives you better readability and maintainability of the code.
Proper indentation and documentation makes your code self describing.

OK, what is documentation ? Are you here to code or write word documents ?
No word documents or something like that. Don't worry.
In java code, you can put some human readable comments which Compiler will ignore but developers can get understanding of the use of the part of code.

Comments: Comments are not for compiler, its for developers. Hmm, Java gives you a way to describe your code within the code itself.
Let's take an example. Consider the following code,
 public class Counter {  
      public static int counter;  
      void increment() {  
Can you tell me, why is this being used ?
Yeah, sort of.

Now go through this one,
  * This class is implementation of counter functionality  
  * @author Palash Kanti Kundu  
 public class Counter {  
       * The counter to be incremented. Default is set to 0  
      public static int counter;  
       * On each invoke, this will increment the counter value;  
      void increment() {  
           // Increment the counter  

Now, you get the clear picture. This is how comments help improving readability of your code.
It is always a good practice to write some comments in your code.

Java language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.
// text
The compiler ignores everything from // to the end of the line.

To get some more idea on comments you can refer this link.

Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment