This article subsumes -
What are current ways to design tree structure ?
Comparision of all of them ?
Flat table approach and advatages ?

dumb approach

Clever approach

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
Coding Guide Lines
For some Team leads code review is like just one afternoon free from work .What these guys look in code is indentation , naming convention and all crap which is not going to reduce bugs in QA or in acceptance testing .Actually , such activities are overhead from companies . Under the name of code review it is a real time pass with vacuous comments.
For static code review we must rely on automated checking using like find bug , check style PMD etc .Many people don't know to calculate dependency , complexity of code , how to calculate cyclomatic complexities of code .Just review of indentation , formatting is useless and overhead.
In design review, which must happen at after requirement gathering stage . These people must be well versed with design patterns .At least they must know gang of four patterns .Some people I have seen do excellent procedural coding in java which is very upsetting and frustrating .
Buzz words of OOP coding like loose coupling and adhesive coding should be found in such reviews .Most important thing is neglected is thread safe coding .Eventhough These bugs are very hard to catch in code review , we should at least try to catch them early stages of project because in next staged cost of fixing is very high and futher at customer end is the highest cost and loss of trust .
The most important thing is that reviewer does not consider amount of data handled by method .If large amount of data to be processed then inefficiency of loop is going to magnify and it will cause more response time.
Proficient reviewer must catch such errors before causing serious damage to customer.
While coding consider the situation that next person who will be coding , is a desperado , reckless outlaw with three counts of felony and further more he knows your address. ;) .Or you may imagine that next coder is demon and his doppelganger is going to haunt you if you make you code inflexiable and not readbale , inefficient .
What is cost to the company?
It is really time consuming and extractives with no real time out put .So do code review for hot spots like critical module, new comer who don't know code. Instead of making long list of does and don't , real implementation of recommendations is important .Code review should be done in minimum time with maximum awareness of nasty design related bug and not just grammar check of code .shouldn't it ?
In general
1. One function should be maximum of 25 lines. Use proper indentation.
2. Follow naming conventions for classes, methods and variables.
3. Refractor the complex code in simple pieces whenever possible.
4. Put informative comment to important methods and update the same when logic is changed.
5. Minimize code complexity by reducing nested of conditional statements
(if else , while , for) statements inside one another.
6. Reduce dependency of one class on another .Too many import statements suggest more dependency and less flexibility.
7. Remove old commented code which clutters uncommented code.
8. Do not waste time to optimize, generalize the code which will be used only once.
9. Concentrate on optimization, efficiency of module on which many modules are dependent.
10. Avoid copy paste of code .Write proper reusable function for it.
11. Consider reuse of repeated and critical code.
In specific to Java
1. Coding style related
Pease refer to
http://www.oracle.com/technetwork/java/codeconv-138413.html
2. Standard practice related
1. Use private variables and give set method only if necessary.
2. Avoid try {} catch (Exception ex) .Be specific about thrown exception.
3. Try to avoid null. Return empty object in case of collection.
4. Always override hash code when you override equals
5. Select proper collection for time efficient methods.
6. Do not make public class level variables
7. Do not synchronize methods unnecessarily.
8. Always override toString() methods for data models.
9. Use comparable interface than comparator.
10. Consider thread safety when multiple threads are accessing the object.
For more please refer to
http://www.javapractices.com/home/HomeAction.do
http://www.havelund.com/Publications/JavaCodingStandard.pdf
http://www-01.ibm.com/software/webservers/appserv/ws_bestpractices.pdf
Code review
A. Static/Automated Code review
Rely on automated code review for coding style and naming convention and not for logical or design related bugs .
1. Use of PMD
Link - http://pmd.sourceforge.net/
2. Use of check style
Link - http://checkstyle.sourceforge.net/
3. Use of find bug
Link - http://findbugs.sourceforge.net/
4. Use of tools like Jdepend, TC tool to determine complexity, dependency Index of class.
- I Will be adding information on Jdepend , TCtoolkit ,SourceMonitor ,GraphViz ,DependencyFinder
Depend-o-meter etc.
B. Logical/Design related Code review
• Correct Design patterns
Link -
-- NILESH SALPE
(NOTE : This blog is draft - no grammar check )
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JavaServerFaces</display-name>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>faces/hello.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee" version="1.2" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" xi="http://www.w3.org/2001/XInclude">
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
---
managed beans
--
navigation rules
---
</faces-config>
function moveUpRow(tableId) {
var fromindex = null;
var trs = document.getElementById(tableId)
.getElementsByTagName('tbody')[0].getElementsByTagName('tr');
for ( var i = 0; i < classname ="=" fromindex =" i;"> 0){
var table = document.getElementById(tableId);
moveRow(table,(parseInt(fromindex) - 1),parseInt(fromindex));
moveUp(fromindex);
}
}
function moveRow(table, from, to)
{
var tbody = table.tBodies[0]; // Use tbody
var trFrom = tbody.rows[from]; // Make sure row stays referenced
tbody.removeChild(trFrom); // Remove the row before inserting it (dupliate id's etc.)
var trTo = tbody.rows[to];
tbody.insertBefore(trFrom, trTo);
}
Jcaptcha problems
Warning - In UNIX environment JCAPTCHA need X11 server running .But In my case inspite of this server graphic image generation was not possible so I opted for reCAPTCHA.
<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=your_public_key">
</script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=your_public_key"
height="300" width="500" frameborder="0"></iframe><br>
</noscript>
Boolean checkData (){
// Other code
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) facesContext
.getExternalContext().getRequest();
String challenge = request.getParameter("recaptcha_challenge_field");
String response = request.getParameter("recaptcha_response_field");
String remoteAddr = request.getRemoteAddr();
RECAPTCHASingleton RECAPTCHASingleton = RECAPTCHASingleton
.getInstance();
boolean isResponseCorrect = RECAPTCHASingleton.validate(challenge,
response, remoteAddr);
if (!isResponseCorrect)
{
flag = false;
}
//Other code
}
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
public class RECAPTCHASingleton
{
// Logic for SINGLETON DESIGN PATTERN
public synchronized boolean validate(String challenge, String response,String remoteAddr)
{
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
reCaptcha.setPrivateKey();
ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr,
challenge, response);
return reCaptchaResponse.isValid();
}
}
---------- NILESH SALPE