Tuesday, May 7, 2013

Java Code Metrics


Google CodePro AnalytiX - Metrics


http://sourceforge.net/projects/metrics/


I liked Google Analytix. It is simple, easy to use, fast and accurate


Update site @ 
http://dl.google.com/eclipse/inst/codepro/latest/3.6   for eclipse 3.6


Tested Results for my plug-in:



Lines of Code: 5,18,178
This is a count of the number of lines in the target elements that contain characters other than white space and comments

Number of Lines:  This represents lines of code with comments.

Global Results

Metric Name
Value
Abstractness
4.8%
Average Block Depth
1.18
Average Cyclomatic Complexity
2.94
Average Lines Of Code Per Method
12.96
Average Number of Constructors Per Type
0.45
Average Number of Fields Per Type
2.84
Average Number of Methods Per Type
5.79
Average Number of Parameters
0.90
Comments Ratio
5%
Efferent Couplings
3,302
Lines of Code
518,178
Number of Characters
26,077,514
Number of Comments
26,156
Number of Constructors
2,636
Number of Fields
27,006
Number of Lines
695,811
Number of Methods
33,343
Number of Packages
220
Number of Semicolons
308,386
Number of Types
5,751
Weighted Methods
107,977

Why would one mark local variables and method parameters as “final” in Java?

String literal can be replaced by a character literal

The single character string literal can be replaced by a character literal to improve performance.

Example:


Given two functions:
public static String concatString(String cs) {
    return "hello" + cs + "world";
}

public static String concatChar(char cc) {
    return "hello" + cc + "world";
}
after examination of the bytecode it boils down to two AbstractStringBuilder.append(String) vs.AbstractStringBuilder.append(char).
Both methods invoke AbstractStringBuilder.expandCapacity(int)) which will allocate a new char[]eventually and System.arraycopy the old content first.
Afterwards AbstractStringBuilder.append(char) just has to put the given char in the array whereas AbstractStringBuilder.append(String) has to check a few constraints and calls String.getChars(int, int, char[], int) which does another System.arraycopy of the appended string.


Trial:



package kondal.performance;

public class StringTest {

public static void main(String[] args) {

long start = System.currentTimeMillis();

StringBuffer message1 = new StringBuffer("Hello world");
for (int i = 0; i < 1000000; i++) {
message1.append("Program");
message1.append("!");
}
long diff = (System.currentTimeMillis() - start);
System.out.println("Totat time(string) in ms:" + diff);


long start2 = System.currentTimeMillis();

StringBuffer message2 = new StringBuffer("Hello world");
for (int i = 0; i < 1000000; i++) {
message2.append("Program");
message2.append('!');
}
long diff2 = (System.currentTimeMillis() - start2);
System.out.println("Totat time(Char) in ms:" + diff2);

}
}


Results:
Totat time(string) in ms:154
Totat time(Char) in ms:116






Friday, May 3, 2013

Java script - Prototype and new


Basically I was looking for how 'new' and 'prototype' things works out in Java script, and I could find very good site, which has lot of stuff on these areas.

http://ejohn.org/apps/learn/#65

Example #1:


RecordManager= function(){};

RecordManager.prototype.getRecords = function(){
  return true;
};

var manager = new RecordManager();

assert(manager.getRecords(), "status");

OutputPASS status


Example #2:


function RecordManager(){}

RecordManager.prototype.addRecords= function(){
  return true;
};

var manager1 = RecordManager();
assert( manager1 , "Is undefined, not an instance of RecordManager." );

var manager2= new RecordManager();
assert( manager2.addRecords(), "Method exists and is callable." );

Output:

  1. FAIL Is undefined, not an instance of RecordManager.
  2. PASS Method exists and is callable.





Tuesday, April 30, 2013

What skills do tech companies look forr in new hires ??

Interesting article on what tech companies look for in new hires:
http://mashable.com/2012/09/05/tech-skills-hiring/



Everyone knowing their one thing - What's the one thing that you do better than anyone else??


Product minded and want to see their codes launched.
Who take pride in crafting a fantastic product experience, we look risk takers who are willing to fail but really eager to launch things.

How knows how to setup a process  and ability to execute tasks
How has passion in doing things

Wednesday, April 24, 2013

How to setup RFC Destination to SAP System



RFC Destination is one of the way to connect from an ABAP system to an external system. This weblog talks about the basic HTTP connection to external server (type G).
Business usecase from my area of expertise is connecting any R/3 system to a CE ESR (mainly for Proxy generation of Services Modeled in ESR).

Step 1:

Go to transaction SM59.
UI looks like as follows:

Step 2:

As shown in the picture above, select the HTPP connection to external server and click on the Create icon (marked in red).
RFC Connection UI will come up. Fill in the necessary details as shown below:


Note that RFC Name should be unique, connection type should be Type-G.
Other information you need to enter is the host (or IP), Port and the Context root. The following URL should match the service deployed on AS.
A good example to use the RFC destination will be to write a small ABAP code to establish connection to a servlet deployed on AS. User can use HTTP protocol to invoke it from ABAP.

Step 3:

Click on the save icon  and you will recieve an info message stating "HTTP connection may not be secure" which is true.


Press  to continue.

Step 4:

Now most of the application which the RFC destination points to will require some basic authentication. And if it is not provided part of the RFC destination, quite possible when the program uses the RFC Destination, it prompts for username-password! So lets enter the logon & security details as shown below: 

Step 5: 

Click on the save icon  and now press on the connection test button to see whether the server is available or not. Incase the entries point to the correct system, output should be somewhat similar as shown below: 
  
More info:
- Incase wrong entries are provided error message comes at the status bar:
- Connection test DO NOT check the credentials. Even if wrong credentials are passed this information is not validated.
Next steps... write small abap code to invoke a servlet...!!! Sounds simple & interesting.





Friday, April 19, 2013

Controlling number of parallel jobs based on number of available processors for JVM



//No. of processors available for JVM
private static final int NO_OF_PROCESSORS = Runtime.getRuntime().availableProcessors();
private static final String JS_FAMILY = "js_typechecking_family"; //$NON-NLS-1$


Override belongsTo method in Job.
@Override

public boolean belongsTo(Object family) {
return family == JS_FAMILY;
}


Execution call:


try {
monitor.beginTask("Project: " + project.getName(), jsModules.size());

//Create number of parallel jobs based on the number of processors available to the Java virtual machine.
IJobManager jobManager = Job.getJobManager();
for (IFile iFile : jsModules) {
Job[] find = jobManager.find(JS_FAMILY);
if (find.length == NO_OF_PROCESSORS) {
while (true) {
try {
Thread.sleep(100);//sleep for some time buddy!!
} catch (InterruptedException e) {
//nothing
}
Job[] queueJobs = jobManager.find(JS_FAMILY);
if (queueJobs.length < NO_OF_PROCESSORS) {
break;
}
}
}

if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
monitor.setTaskName("Identifying xxx: " + iFile.getName());
try {
startExecution(...);//args here
} catch (Exception e) {
EditorPlugin.logError(e);
}
monitor.worked(1);
}

} finally {
monitor.done();
}