msgbartop
Code that explodes conventions
msgbarbottom

03 Apr 11 Single Responsibility Pattern

The single responsibility pattern states that a class should have one responsibility and one responsibility only.  The resulting code, if this pattern is followed, is cleaner or more maintainable.  What every class does is abundantly clear and the location of bugs is less of a daunting task.

How do you define a responsibility? Where does one end and another begin.  These are nebulous questions to which there are no definitive answers, but that does not mean that this whole discussion is moot.

A violation of the single responsibility pattern is like pornography.  You can't define it, but you know it when you see it.  Experienced architects know what a class that has one responsibility looks like, so they have an instinctual sense of how to design their classes. Not everyone's an experienced software architect.  What's the point of going over all of this for that type of person?

The point is to make those who are less experience aware of a way of thinking about design.  If you're actively making each class have one responsibility, then you'll eventually learn to spot classes that egregiously violate that principle.  As you design software over your career, that skill will lead to better quality.

I've won you over, but you may be thinking: can you give me an example?  Let's consider something fairly straightforward: an email class.  It fits the single responsibility principle, but that responsibility is quite sizable.  Separating the content from the email makes the email class more focused.  Still better the senders and receivers could be separated out too.  The redesigned email class now extends two interfaces: one for the transportation points and the other for the content.  Those are the sorts of decisions and thought patterns that the single responsibility principle imparts to you.

Source Code: click this link

 

01 Apr 11 Java coding convention

There are numbers of reason behind coding convention and its now well-known. You know in a software development, 80% of cost is maintenance and hardly any software is maintained by its original authors. So code should be readable to others. Code convention improves the readability, and therefore, maintainability of code and allows engineers to understand code more quickly. It enables code review easier. It saves development time and once it is the guidelines of learners.

Java coding convention can be put into different groups. Today I’ll discuss about naming convention:

Naming convention:

General convention:

Meaningful names: all name used in code should be meaningful, methods name should be like what they do, for say calculate() which perform some calculation. And naming should be in English spelling. Naming should be avoid abbreviations. Should use meaningful name while naming class name, variable name, constant name.

Like -

class name → AddressDetails

method name → addAddressDetails

variable name → username

constant name → DEAFULT_VALUE

Familiar Names : existing terminology should be used like customer, clients etc

Case : Java is case-sensitive. So username and Username is different. But it should no use same name that differs only in case.

Package name: package name must be lower case.

Like → iit.bit.rokon.com

Class name: class name must be start with upper case. Class name should be noun. Class name can contain multiple words. This case they are linked like → AddressDetails. Every word should start with upper case letter.

Interface name: interface name should be like class name. And should use none or adjective while naming interface.

Method name: first letter must be lower case and capitalize first letter of each subsequent word that appears in a method name. And method name should be a verb. In the case of accessor method, we should use javaBean convention. Like getter and setters.

Example -

class User{
private String username;

public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
}

Variable name: Variable name should be noun and should be start with lowercase and capitalize first letter of each subsequent word that appears.

Constant Name: Constant name should use uppercase letters for each word and separate each pair of words with and underscore.

Example -

public static final int DEAFULT_VALUE = 1;

References: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

Loading...
Join thousands and get updates for free...No-Spam Guarantee