Skip to main content

Posts

Copying objects – Java Clonable interface

We can assign reference of an object to another, provided each of them satisfies an “ is-a” relationship or of same type. For example, a Dog object can hold the reference of another Dog object regardless both have different states. Similarly an Animal object can hold the reference of a Dog or Cat object, if they satisfies an “is-a” relationship. But hold on, these assignments do not help you to create a duplicate copy of the object. It’s nothing but different pointers pointing to the same memory location. Java provides API to clone an object. Using this API one can get a copy of an object to another. To achieve this, proposed class should implement Clonable interface (else JVM will throw CloneNotSupportedException during runtime). The rationale behind cloning is to get an independent copy of an object. The Object class has a clone() method which takes care of this operation. This method is protected by nature and returns an Object , when called.   You can call this ...

UML 2.0 - Class diagram and different relations

Lately I had a hard time to understand different notations used in UML 2.0 to represent relationship between classes in a class model. Though I have experiences in programming and designing, it did not help much. Therefore to enhance this knowledge I did some research on internet and also read a beautiful book on UML 2.0 by Martin Fowler . Indeed it helped me a lot and here goes some of my interpretation on Class modeling based on UML notations. UML 2.0 defines standard notations to indicate relationship between the classes. The class diagram is necessary while designing behavior of a component. Also helps a lot in TDD to scribble some design before start coding. The relationship between classes can be defined as below, Relationship that is defined at instance level , are of three type. Relation that is defined at instance level can be considered as a run-time relation. These relations are realized and contributed at run-time. Association: It defines the abstract form of a relat...

Code maintainability index

How do you measure that your code is maintainable? There is no such standard measure exists in IT industry that indicates that your code is maintainable. However there can be several disciplines which you can use to interpret maintainability of your code. One way of arriving to this is to identify different high priority code metrics and summarization of weightage of each of those metrics. For example look at the below calculation, Code Metrics Weightge in % Code coverage 50 Cyclomatic Complexity 20 Duplicates 10 Coupling 10 Large Class 5 Large Methods 5 Total 100      The above table illustrates the weightage derived from each metrics considered to calculate the maintainability index of software. The weightage can be calculated based on the target metrics you have defined for each item on the list. This calculation is quite interesting and c...

A thought on code metrices

We always face interesting discussion on code matrices. I got a funny but very interesting understanding of the types of metrices and its prioritization that we always talk about.   I would spread code metrics into two categories,        Code matrices that can be identified with bare eyes by a developer.       Code matrices that cannot be identified with bare eyes by a developer.  Code matrices that can be identified with bare eyes are Unused types  Depth of inheritance Code comments etc. The above matrices can be identified by a developer while coding or refactoring and he can react on them immediately. For example, if he / she see an unused import he / she can delete it. He / she does not have to wait for a tool to detect it or to Google for a new tool to recognize this measure. Same applies for code comments, do not write comments if you find any irrelevant comment delete them right a...