Friday, April 12, 2013

Five quick Java programming tips for junior software developers

Rochester Oliveira shared the following professional tips. Patience is a needed virtue when a senior software developer mentors more junior members of the application development team. Many of the concepts a seasoned veteran takes for granted are not nearly as obvious to new programmers who are just cutting their teeth on Java syntax and structure. Here are five tips that might be obvious to experienced programmers, but are common mistakes made by those who are a little bit more green when it comes to application development:






  1. Don’t waste time testing if conditions repeatedly inside an else block. New developers often make mistakes similar to the one made in the following code:

    if (x<0 br="">else (x>=0 && x<10 br="">
    Once the else block is run we know for sure that x is indeed greater than or equal than zero, so the second test is redundant
  2. The switch  is not a replacement for a well thought out if…else statement. The switch statement doesn’t do any kind of black magic behind the scenes. A switch statement simply runs all of the various cases until it finds the right one, and all of them will be tested the same way they would be were an if…else statement was used. Switch statements are not inherently bad in themselves, but simple if…elsestatements tend to be easier to read, and a heavy reliance on switch statements can sometimes be an indication of poor object orientation.
  3. When a variable needs to be initialized inside an iterative loop, do not use the for loop, but instead use ado…while loop.  The do…while loop is always entered at least once, regardless of the condition being tested. A for loop does not provide the same guarantee. Even better advice is to initialize variables outside of loops entirely, which leads to the next tip.
  4. Initialize variables as they are declared, and provide sensible default values. While lazy loading of complex objects is always a more efficient way to program, there is value in assigning individual variables a initial value, even if that initial value is zero. It makes your programs more predictable in the future. And if there is a non-zero or non-blank value that makes sense for a given variable, then make that assignment up front. Many mature frameworks, including Java EE, have advertised that key improvements have been including smart and sensible default values. If the big frameworks are doing it, it makes sense to do it in the code you are developing for your own projects.
  5. Don’t repeat yourself. Methods are there to be used, so take the time to factor out repeating code snippets into a common and reusable method. It’s obvious advice, but as obvious as it is, it bears repeating. Breaking code down into well formed methods will save time and trouble in terms of maintenance and debugging.
None of these pieces of advice are particularly earth shattering, but they are all important points that are often overlooked by junior software developers. If you’re a senior programmer mentoring some junior members of the team, it might be wise to have them quickly glean over these sensible pieces of advice.

No comments:

Post a Comment