Defensive programming Assignment Help

Assignment Help: >> Error-handling techniques in Java >> Defensive programming

Defensive programming:

There are techniques to deal with potential errors, which are based on anticipating the conditions under which errors will occur. The first technique is to have a method return a value indicating whether the error condition was met when performing the code within the method.

For example, assume that we are writing a method called add, which adds an item to some data structure such as a queue of a fixed maximum size. You can imagine such a queue as being implemented by an array or some similar structure. Being a queue, it will have specific rules as to where elements can be added (only at the 'back' of the queue) and how elements can be taken out of the queue (only from the 'front' of the queue, not from the middle or the back). Normally the add method would return no value and would just add the element to the end of the queue. If the queue is already full, an error will occur. In order to cater for this error condition a boolean return value could be used. For example, the definition of such a method for a queue of strings might be:

public boolean add (String newItem)
{
if (isFull()) // private helper method isFull
{
return false; // queue full, item not added
}
else
{
// code to add item to end of queue
// ...
return true; // item added successfully
}
}

If the queue is full, then the method immediately returns the boolean value false and the new item is not added.

This approach has limitations - some methods may already return a result and it may not be possible to return an error indication as well.

The second technique - really a variation on the first approach - is to use a special method to check for a potential error condition before executing code that may encounter the condition. As above, assume that we are writing a public method called add that adds an item to a queue of fixed size. Before executing this method it would be wise to invoke another public method called, say, isFull, which would check that the queue had space for the item to be added. If it did not, then the method add wouldnot be invoked and the program would have to take some alternative action. This might be used as follows, where q is a suitably defined object of a queue class:

if (q.isFull())
{
// carry out alternative action
}
else
{

// queue has space - so add new item
q.add(newItem);

}

This second approach is related to an idea called design by contract. Here we are using the word 'contract' in the business sense to mean a well-defined agreement between two or more participants. The add method guarantees to add the item if there is space in the queue, as indicated by the isFull method. If there is no space in the queue then the 'contract' condition does not hold and the result of trying to add an item is not guaranteed (and will usually cause a problem, often throwing some sort of RunTimeException).

Both of the above approaches to error conditions are part of what is sometimes called defensive programming. This involves anticipating possible errors and including code to prevent them or to take appropriate action if errors do occur.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Defensive programming homework and assignments? Live Defensive programming experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Defensive programming homework help, java assignment help and Defensive programming projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd