• Post Reply Bookmark Topic Watch Topic
  • New Topic

Robustness  RSS feed

 
sparsh khandelwal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
what is the meaning of robustness in java?
thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robustness means something doesn't break easily.

Robust methods are methods that don't allow you to pass invalid arguments, and they don't let the outside world affect them once they are running. Useful tools to achieve this are parameter checking, defensive copying, file locking and synchronizing, among others.

Example:
This method demonstrates defensive copying, and parameter checking. Before it does anything, it copies the collection of things to a new list. This makes sure that while the method is running, no other thread can come in and change the collection of things we're working on. Another thread can still edit the individual objects in the list, but you can mitigate this problem by making sure that the objects immutable.

After the argument has been copied, it is checked to see if it is valid to work on, and won't break the program at some later stage, or even worse, do harmful stuff like passing wrong bank account numbers or passwords. In this example, a loop checks that the things in the list aren't null, and that they are valid in some way.

Finally, when the method is certain that everything is okay, it can start working on the input. Depending on what the method does, you can first check the entire input before doing anything at all, or you can check input as you need it. It's important that you check all input before you do anything, if the input in related to each other. The example method assumes that the things in the list are not related to each other, so it processes each thing immediately after it's checked it.

Robust classes are classes that don't allow other classes to use them in a way they weren't intended for. They are built from other classes that are also robust, or if they use classes or methods that aren't, they don't expose them to the outside world directly. They make sure that their internal state is never corrupted, and if it is (because of some exceptional situation they can't recover from) they become unusable. State checking is an important aspect. Making a class immutable makes them as robust as possible.

Sometimes robustness comes at a price. It may be too expensive to check everything before you use it. Here's where assumptions come into play. You don't have to check data as long as you may assume that they will be in a valid state. When can you make assumptions? Classes may assume that private members are always valid, unless the outside world is allowed to have a reference to their values. This is why you should often perform defensive copying in constructors and setters before you assign a value to private members, and defensive copying in getters before you return the private member.

Private methods don't have to check parameters. They can only be called by methods that belong to the same class, so they may assume that arguments passed to them are always valid.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robust - sturdy in construction; strong, health, vigorous.

When referring to code, robustness usually means it is relatively free of bugs, has good design, good structure, good organization, is able to handle exceptional conditions gracefully, is able to handle different kinds of inputs gracefully.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!