General Programming Terms
About:
There are many general programming terms that we come across while using programming languages. Today I would explain such terms w.r.t to java programming language with examples.
Description:
1. Declarative Programming:
There are two approaches to programming called
a) Imperative programming and
b) Declarative programming
Imperative programming gives a list of instructions to execute in a particular order -- Java program that counts the number of words in a text file is an example of the imperative approach.
Declarative programming describes a set of conditions, and lets the system figure out how to fulfill them. The SQL statement
SELECT COUNT(*) FROM XYZ is an example for the declarative approach.
In other words, "specifying how" describes imperative programming and "specifying what is to be done, not how" describes declarative programming.
In JAVA context we have examples of Declarative programming in EJBs and Annotations.
What makes EJB components special is the declarative programming model through which we can specify the services such as security, persistence, transaction etc., that the container should provide. An EJB only implements the business logic; the services are associated through a deployment descriptor, which essentially acts as metadata for the EJB. At runtime, the container uses the metadata specified in the deployment descriptor to provide the services. The deployment descriptor is an XML file, not part of the Java classes that make up the EJBs.
To add even more simplicity and ease to this after JDK1.5 we now have Annotations which makes our life even more simple with the programming.
Annotations provide a standard way by which we can annotate the JAVA classes and make up the EJBs so that the developer so that a developer can look at the class definition, together with annotations, and know everything about that class.
With annotations we just "say what to do" and "how it is to be done is handled by the JVM at runtime.
Lets consider simple HTML and CSS, even these are declarative programming languages. For instance, the HTML example <table border="1">, indicates a thin border. A CSS example is color: blue. This specifies the text color. As can be seen in these examples, HTML and CSS specify what should appear on a web page but not how to do so.
So to conclude, the advantage of declarative programming languages is mainly two-fold. The programs are concise; this makes it easy even for non-programmers to obtain solutions. In the SQL example above, an analyst or business support person can get the desired information. Similarly, laypersons can write acceptable web pages with simple HTML and CSS commands.
The second advantage of the declarative programming model is that repetitive imperative code that indicates how to solve things is provided in the computer system behind the scenes. Such code can be made highly efficient and can incorporate the best ideas from computing. It can take advantage of parallelism.
2. CallBack Methods:
Callbacks methods are
the way of managing life cycle of an instance.
Callback methods are generally used by containers.
The methods are called at specific time during the
lifetime of an instance.
For example in JAVA servlet destroy() method is
called by the servlet container that indicates that the servlet is being taken
out of service. This type of methods are generally called by the container, the
developer does not need to call these methods explicitly.
Most of the languages
specifies the callback method by passing the address of the subroutine to the
system to the request is calling back from,
But JAVA performs the same thing(i.e. specifying callback methods) by using interfaces. JAVA does not allow passing the address of subroutine but allows passing an
instance of a class that implements the standard interface. For this purpose
anonymous classes are mainly used as they support a better compact definition
of the class that is required as a new class. One more example of callback
methods is ejbCreate method that is used by an
ejb container to create a ejb bean - the developer does not call it explicitly
in code- the container calls it- although the developer can override it- or put
stuff in it.
3. Cohesion and Coupling:
One of the most important goals of object oriented
design is to have high cohesion classes and loose coupling between these classes.
Coupling refers to links between separate units of a program. In object oriented programming, if two classes depend closely on many details of each other, we say they are tightly coupled.
Coupling is a measure of the interdependence between classes. If every object has a reference to every other object, then there is tight coupling, and this is undesirable. Because there's potentially too much information flow between objects. Loose coupling is desirable. It means that objects work more independently of each other. Loose coupling minimizes the "ripple effect" where changes in one class cause necessity for changes in other classes.
Cohesion, on the other hand, refers to the number and diversity of tasks that a class is designed for. If a class is responsible for a few related logical tasks, we say it has high cohesion.
Cohesion is a measure of strength of the association of variables and methods within a class. High cohesion is desirable because it means the class does one job well. Low cohesion is bad because it indicates that there are elements in the class which have little to do with each other. Modules whose elements are strongly and genuinely related to each other are desired. Each method should also be highly cohesive. Most methods have only one function to perform. Don't add extra instructions into methods that cause it to perform more than one function.
Loose
coupling makes it possible to:
·
Understand one class without reading
others
·
Change one class without affecting
others
·
Thus: improves maintainability
High
cohesion makes it easier to:
·
Understand what a class or method
does
·
Use descriptive names
·
Reuse classes or methods
<<More General Terms Coming Soon>>
Conclusion:
Feel free to post in your comments. Happy Programming!.
No comments:
Post a Comment