Monthly Archives: October 2011

Functions – Clean Code Video

While watching the episode 3 of the clean coders video I noted down some important points.

Functions are the first line of code organization in your application.

Small Functions

  • The first rule to having clean functions is that they should be small.
  • Typically classes hide in large functions.
  • If you find functions that can be divided into several functional areas and you have variables that are used by all those areas then what you really have is a class. After all, a class is a group of functions that use a common set of variables.
  • Having lot’s of small well named functions is good. They act as sign post which help the reader to navigate through your code easily.
  • We should not be worrying about the function call overhead since the current computers are so fast that this overhead might be less then a nanosecond. Instead we should take the advantage of such high speed hardware to improve our code for readability first.
  • Advertisement

    Unloading of static fields in java

    I came across this interesting article about loading and unloading of static fields which details a couple of facts I was not aware of
    1. Loading – You can get a reference to a class, before it has been initialized (i.e its static fields being initialized or its static initializers being called), only when it is used does it get initialized. The link contains an example code which illustrates this fact.
    2. Unloading – Static fields are unloaded when the Class’ classloader is unloaded. This is unloaded when a GC is performed and there are no strong references from the threads’ stacks.

    This post adds more detail to the second point. Below are few reasons why classes are unloaded only when the classloader unloads.

    Read the rest of this entry

    Test Driven Development – Clean Code Video

    While watching the episode 6 of the clean coders video I noted down some important points.
    • Design and Code rots lead to fragile, rigit and in-mobile code base. Debugging becomes difficult, estimates mount and implementing any new changes becomes difficult. Uncertainity in the code increases.
    • Maintaining a test suite with a high code coverage has below benefits

    1. Keeps defects under control 
    2. Gives us the courage to change the code in order to clean it 
    3. Unit tests act as code examples for your API. They are low level design documents. For e.g. The test cases helps us to know all the possible ways to call a particular API. 
    4. Writing tests first, makes the production code testable. To write unit tests you would have to make your functions decoupled from each other. This leads to an improved design.

    Read the rest of this entry

    How to check if glassfish is running

    Executing “list-domains” command gives a list of the domains along with their status.

    In the above snapshot, I had just the default domain “domain1”. More about the command can be found here and a good link to understand the concept of a glassfish domain can be here.

    Java 7 Feature Guide

    A good concise guide to all the jdk7 enhancements can be found here. Some interesting ones are –

    Scalable file I/O support
    Many of the methods did not scale. Requesting a large directory listing over a server could result in a hang. Large directories could also cause memory resource problems, resulting in a denial of service. The new file I/O package is implemented so that it works regardless of the file system’s size.

    Watching a directory for changes
    The Watch Service api allows you to register a directory for file change notifications. An application can register for creation, deletion, or modification events. This feature can be implemented using the FileVisitor API to recursively watch an entire file tree for changes. For more information, see Watching a Directory for Changes (Java Tutorial).

    Read the rest of this entry

    Labelling Connections In Oracle UCP

    Oracle’s Universal Connection Pool (UCP) provides a feature called connection labeling where an application could attach arbitrary name/value pairs to a connection.By associating particular labels with particular connection states, an application can retrieve an already initialized connection from the pool and avoid the time and cost of re-initialization. The initialization varies and could include simple state re-initialization that requires method calls within the application code or database operations that require round trips over the network.

    Connection labeling is application-driven and requires the application to implement an callback interface called oracle.ucp.ConnectionLabelingCallback. The callback determines whether or not a connection with a requested label already exists. If no connections exist, the interface allows current connections to be configured as required.

    Read the rest of this entry

    Inverse Relationship In Hibernate And JPA

    Inverse attribute in hibernate defines which side is responsible of the association maintenance. In a one-to-many (bidirectional) or many-to-many relationship the side having inverse=”false” (default value) has this responsibility (and will create the appropriate SQL query – insert, update or delete). Changes made to the association on the side of the inverse=”true” are not persisted in DB.
    In Hibernate, only the relationship owner should maintain the relationship, and the “inverse” keyword is created to define which side is the owner to maintain the relationship.

    Few good links explaining this concept can be found here, here and here.

    Inverse attribute with JPA Annotations

    inverse=”true” is an attribute to be specified in the hbm.xml but in JPA it is specified using the mappedBy attribute of an @OneToMany annotation.

    Execute before or after a spring transactional method

    Spring provides a mechanism through which you can plugin some code which gets executed after the transaction is complete. We need to provide an implementation of the interface which can be registered using TransactionSynchronizationManager. This could be useful in scenario where your code does not have a control over when the transaction starts and ends. For e.g. Your service code always joins an existing transaction started by the calling method (using REQUIRED PROPAGATION LEVEL).

    Read the rest of this entry