Nov 22, 2014

Are you a Java beginner? Why good Java developers are self-taught?

Q. Are you a beginner wanting to learn Java?
A. If the answer is yes, read on. You can't learn much with a paid 5 to 10 days or even a 3 month course. It will only set you back on your wallet.  Formal education like computer science degree is very valuable and will be well recognized. But, there are a group of people like me who want to make a career transition from other degrees like mechanical engineering, science degree in nuclear physics, etc.

Self taught usually means "interested in the art and science of creating quality code to solve business problems", and generally speaking you will be proactively learning and mastering the language fundamentals (e.g. data types, flow control, data structures, multi-threading, file I/O, etc), key concepts (e.g. Object Oriented Programming, Functional Programming, Aspect Oriented Programming, etc), tools (IDE, Dos/Unix commands, build tools, code repositories like SVN/Github, etc), frameworks (e.g. Spring, Hibernate, etc) and libraries (Apache libraries, Google Gauva libraries, etc) of the programming trade.

As a programmer

Invest in good books that will teach you the Core Java and Enterprise Java fundamentals, and increase your researching and hands-on development skills via myriad of free blog posts, online tutorials, and online articles. When investing on books, make sure you review the sample chapters to see if you like the style and check reader ratings to see  what others say. Join a good Java forum like to seek help when you are stuck from the fellow Java professionals. It is even better if you have mentors who are your friends or former colleagues to guide you.

Q. Are there any limitations to becoming a self-taught programmer?
A. Yes, there are 2 key limitations

1. Lack of interest. Learning to program on your own is hard and then to be able to be knowledgeable enough to be good in technical interviews is even harder. Hope this blog will alleviate this problem to some extent. If you don't have the interest, you will not be disciplined to make the effort. You need to motivate yourself to get into a learning cycle of

learn a fundamental -->  apply that learning via hands-on coding --> blog your experience via blogging (i.e. your journal and sharing your experience)

2. Getting too overwhelmed by the number of technologies and frameworks. Not knowing where to start and what path to take. If you look at Java, there are so many frameworks. So, you need to have good mentors to guide you through this jungle.

  • Java forums where fellow developers can give you advise and guidance, but you need to show interest and take initiative. You can't expect them to do the home work for you, but can post some code, and ask them why it was not compiling, etc. You can also seek career guidance.

Q. 5 tips to become a self-taught programmer?

  1. Learning by investing in good books
  2. Learn online via good blogs, tutorial sites, and open-source contribution (i.e. by looking at others' code in Git hub). When there are myriad of free online materials to learn from, there are no excuses other than your lack of motivation and getting lost in the information jungle. If you don't have a computer science degree, you may need Git hub work (i.e. self-taught projects) to be examined and validated by the potential employers.
  3. Learn on the job by proactively looking at others' code. Learn to use new tools, frameworks, and libraries.
  4. Learn by asking the right questions relating to 16 key areas and fundamentals.
  5. Learn to share your experience by blogging and helping others via forums and discussion groups. Join and discussion groups relevant to your programming language. Don't under estimate the power of learning through helping others.  

In all the 5 steps listed above, you must be writing hands-on code. You can't become a programmer without writing code. The more you code, the more motivated you become. The more mistakes you make, the more questions you need to ask to gain experience. Once you start asking the right questions, you will shape up as a programmer by putting things together like a jigsaw  puzzle, and often researching online and drawing on your own experience for solutions.

Q. As a self-taught programmer, how do you know that you are becoming more experienced?

  1.  You will start to hate code you wrote 6 months ago, and find ways to improve them.
  2.  Project after project, day after day you will feel more and more "professional" and start to ask the right questions and have opinions about things without blindly accepting what others say or write. You will enjoy the feeling of accomplishing real life tasks.   A "professional" means you are capable of performing work that people would pay you to do.  A "professional" means you are capable of performing work that people would pay you to do. This means building an application that can satisfy business requirements in a timely fashion.
  3. You will stat to train yourself on the 16 key areas like best practices, memory considerations, performance considerations, transaction management, security, etc.
  4. You will get motivated to work on your own pet or hobby coding projects like a online app, mobile app, development tool/framework, games, etc. Share these projects with other programmers via Git hub. Flaunt your skills and passion by encouraging your prospective employers to review your work on Git hub. 
  5. You will stat to expand your horizons as a developer like becoming a freelancer, starting your own small business, making yourself more visible via your online presence, etc. 

Q. Can you get a job as a self-taught programmer?
A. If you asked me this question in 2000, the answer is yes. That is when I transitioned my career from a mechanical engineer to a Java developer with just 12 month self-study. It was a .com boom time and there were more jobs than qualified Java developers. But, now a days things are different as 100+ job applications are filtered through and you need to do more to standout from your competition.

#1: Degree. Computer Science is preferable, but at least some recognized degree. Degree means you have proven that you can be trained and learn things.

#2: Hands-on experience in your chosen mainstream programming language (e.g. Java, C#, PHP, etc) gained via self-taught projects, volunteer work and opensource contributions. You can provide links to your Git hub project repository.

#3: Good job hunting skills to try different avenues (e.g. online advertisements, networking, cold calling, etc) and effectively promoting yourself via eye-catching resume and good interviewing skills. Both your technical and soft skills will be under scrutiny. Use the 16 key areas to sell yourself as someone who can solve business problems with the "can do" attitude.

#4: Trying smaller software companies, community and charity organizations before trying larger organizations as experience matters a lot. The culture in smaller companies are more informal, and degrees and experience are highly appreciated but not necessarily mandatory. At smaller companies, the fact you are motivated enough to learn may get you a foot in the door. Be prepared to be flexible. 


Nov 18, 2014

Java exception handling beginner mistakes and best practices

a) Sweeping exceptions under the carpet by doing nothing.

}catch(SQLException sqe){
    // do nothing

In few rare scenarios, it is desired to do nothing with an exception, e.g. in a finally block, you try to close database connection, and some exception occurs. In this case, exception can be ignored.

}catch(SQLException sqe){
    }catch(Exception e){
        //leave it.

But in general, this is a very bad practice and can hide issues. Another typical example is  the InterruptedException. Most Java developers leave the catch block empty. This is a very bad practice. You should either re throw e or restore the interrupted status as shown below

  public void run() { 
    try {
        while (true) {
    catch (InterruptedException e) { 
       // Restore the interrupted status

If a thread is executing a low-level interruptible blocking method like Thread.sleep( ), Thread.join( ), or Object.wait( ), it unblocks and throws InterruptedException. The interrupted status can be read with Thread.isInterrupted( ).

b) Inconsistent use of checked and unchecked (i.e. Run time) exceptions.

Document a consistent exception handling strategy. In general favor unchecked (i.e. Run time) exceptions, which you don't have to handle with catch and throw clauses. Use checked exceptions in rare scenarios where you can recover from the exception like deadlock or service retries. In this scenario, you will catch a checked exception like and wait a few seconds as configured with the retry interval, and retry the service configured by the retry counts. After a few retries, the exception is thrown to the caller.

c) Exceptions are polymorphic in nature and more specific exceptions need to be caught before the generic exceptions.

So, it is wrong to catch Exception before IOException.

} catch(Exception ex){
    log.error("Error:" + ex)
} catch(IOException ex){
    log.error("Connectivity issue:" + ex); //never reached as Exception catch block                                           //catches everything

Fix this by catching the more specific IOException first

} catch(IOException ex){
    log.error("Connectivity issue:" + ex);
} catch(Exception ex){
    log.error("Error:" + ex)

In Java 7 onwards, you can catch multiple exceptions like

catch (ParseException | IOException exception) {
    // handle I/O problems.
} catch (Exception ex) {
    //handle all other exceptions

d) Wiping out the stack trace

}catch(IOException ioe){
    throw new MyException("Problem in data reading."); //ioe stack is lost

e) Unnecessary Exception Transformation.

In a layered application, many times each layer catches exception and throws new type of exception. Sometimes it is absolutely unnecessary to transform an exception. An unchecked excption can be automatically bubbled all the way upto the GUI Layer, and then handled at the GUI layer with a log.error(ex) for the log file and a generic info like "An expected error has ocurred, and please contact support on xxxxx" to the user. Internal details like stack trace with hostnames, database table names, etc should not be shown to the user as it can be exploited to cause security threats.

Data Access Layer --> Business Service Layer --> GUI Layer

f)  why throw exceptions early, and catch exceptions late?

Best practice is to throw exceptions at the point when the errors occur so that you have the most detail about the cause of the exception. For example, you want to know the line that throws the NullPointerException so that you can figure out which variable is null. NullPointerException means you have some broken code, and you need to fix your code. A good example for throwing early would be to throw IllegalArgumentException.

  public void someMethod(String input){
      throw new IllegalArgumentException("input cannot be empty");
      // something

Catching an exception too early before it can properly be handled, often leads to further errors and exceptions.

Bad: Catching it too early

public void readFile(String filename) 
    InputStream in = null;
    // Don't do this !!! 
        in = new FileInputStream(filename);
    catch (FileNotFoundException e)

Good: Catching exceptions late

public void readFile(String filename) throws IOException
    if (filename == null)
        throw new IllegalArgumentException ("filename is null");
    InputStream in = new FileInputStream(filename);

In many situations, the higher layer like the GUI layer knows how to handle the exceptions than the lower layers like data access objects. In many cases, same data access exception will be handled differently by the different calling code. If an exception is thrown, you catch it and decide on the following 4 possible actions to take.
  1. Catch --> Rethrow (but include the original exception with some additional information). 
  2. Catch --> Handle (mainly in the higher layer (e.g. GUI layer) where you make the final decision like logging the exception and asking the user to contact support).
  3. Let the exception bubble up to the higher layers
  4. Catch --&gt Return an error code (for example in batch jobs, Unix scripts, etc). Usually in a higher layer.

Labels: ,

Nov 13, 2014

Unix Interview Questions and Answers for Java developers

Most commercial Java applications run in a Unix environment. For example, batch jobs. Here are a few Unix questions and answers to test your Unix knowledge.

Q. Can you write a Unix function that parses the command-line arguments?

For example:

 process_txns.ksh -env $ENV -backdate 20130125

A. Usually only nine command line arguments can be accessed using positional parameters. The shift command gives access to command line arguments greater than nine by shifting each of the arguments. Successive shift commands make additional arguments available. The second argument ($2) becomes the first ($1), the third ($3) becomes the second ($2) and so on.

function f_parse_command_line_args
 # Parse the command line arguments
 while [[ $# -gt 0 ]]
  case $1 in

Q.  How do you check for successful completion or failed processing of a batch file?
A. You need to use the exit status command. For example,

if [ $? -ne 0 ]
     echo "Processing has failed"
     echo "Processed successfully."

Q. How will you capture the run time stamp?
A. vRunTimestamp=`date +%Y-%m-%d-%H:%M:%S`

Q. How will you execute a Java command? say for a spring batch job?
A. For example, basically a number of -D arguments, -classpath, spring CommandLineJobRunner, spring context file (my-app-job.xml), bean id for the job myAppJob), job paramters (e.g. RUN_DATE) and log file name (${LOGFILE})

$JAVA_HOME/bin/java  -Dds.password.file=${datasourcePasswordFile} \
 ${MY_APP_FOLDER}/java/prop/keystore.jks \ \
                     -classpath ${vClaspath} \
                     my-app-job.xml \
                     myAppJob RUN_DATE=${vRunTimestamp}  \
                     >> ${LOGFILE} 2>&1

The password files are used for database connectivity and RESTful web service calls. The trusstore is used for SSL connectivity to a web service.

Q. If you want to run your job periodically say every 15 minutes, 7 days of the week, how will you achieve it
A. Put your shell script that kick off the job as a cron job.

To view all the cron jobs, type

crontab -l

To edit the cron job, type

crontab -e

You can add the cron job as

5,20,35,50 * * * *   . $HOME/.profile;  ${MYAPP_BASE_DIR}/scripts/my_app_batch.ksh

Runs 5, 20, 35, and 50 past every hour for 7 days. If you want it to run only from monday to friday, then

5,20,35,50 * * * 1,2,3,4,5   . $HOME/.profile;  ${MYAPP_BASE_DIR}/scripts/my_app_batch.ksh

0 or 7 -- Sunday, 1 - Monday, 2 - Tuesday, 3 - Wednesday, 4 - Thursday, 5- Friday, 6 - Saturday