Showing posts with label improving code. Show all posts
Showing posts with label improving code. Show all posts

Sunday, June 22, 2008

Performance Tuning

Now a days many projects failed due to performance constraints and server load problems.

These tips and tricks will help many of us to solve performance related issues.

Programming Approach:

1. Avoid using static variables.
2. Avoid using Loops if possible.
3. User Additional variables to check states and validations for example:

In banking application, we want to fetch list of eligible candidates for loan then we break things into some parts
1. Fetching list of candidates for that loan.
2. Iterating them and finding their record for another loan application, checking in defaulter list.
3. Now for particular employee document verification check will be done.
4. If everything suffice then business rules will be applies to that list.

Here some general validations are
1. Employee's eligibility check
2. Checking in defaulter database.
3. Checking for document submission Yes/No.
4. Business Rules calculations.
By simply writing complex queries(applying joins) we can reduce all above 4 checks into flags like
isEligible, isDefaulter, isDocumentAvailable etc. when fetching results from query this can be used to operate accordingly.
In heavy list it is further useful to take advantage of mathematical calculations at query level. It helps in saving iteration and various flag values.
We can also check for multiple validations at one time by providing different validator objects including flags and relative checks.

4. Query Level Calculations: If records list is quite high, its better to calculate at query level rather then reiterating records to calculate values.

5. Using Language specific suggestion and tips and tricks:
Like we can use StringBuffer instead of String in java for strings subject to change. In some languages,using regular expression is quite heavy. avoiding equalsWithIgnoreCase to equals.
Minimize casting operations by taking variables according to their behavior i.e. float, int etc instead of String.

6. Properly utilizing tools like synchronization, serialization, bit arithmetic and replacing small conditions with conditional operations in place of switch case or condition statements hierarchy.

7. Using HashMaps for constant variables instead of declaring it as plain objects.

8. Separating error/exception handling throws from business rules, Its better to properly handing known exceptions to take actions rather then doing pre check.
e.g. In above mentioned banking example, We can make some checks for specific conditions that whether any employee false in that range or not. Whether employee information is valid or not. This exceptions can be thrown by your query and can be caught at exception handler unit. So its always better to reduce database load by properly utilizing exceptions.

9. Using Interfaces instead of Classes and using light weighted data types provided by language.

10. Minimize query load for properly utilizing connection pooling mechanism: Most critical reason of failing to performance constraint defined is query load, because it may affect with quantity of data. We can minimize queries load by writing complex queries, using function and procedures and performing query optimization and caching at different levels.

11. Light weighting exception related components: Sometimes its quite beneficial to use custom exceptions to work with different type of business rules, we can further optimize it and treat accordingly. Its good to use external mapping files(may be text,hashmap) for error messages and related information.

12. Programmer should not compromise at coding standards specially indenting, variable declaration and using simple functions instead of nested functions. Apart from it, it is always better to take dummy/temporary variables instead of recalling big objects again and again.

13. Optimizing member variable calculations: For any object, it is recommended that state variables should be modified according to data operations defined on that object.
For example: In maintaining account records in banking example mentioned above, that class should automatically vary different states like(total accounts, last indexes, account types etc) with incremental approach rather than calculating all things at one functional call.
Increment approach can be efficiently used at many other places also(like interface,DB to DB transfer etc).

14: Minimize load at synchronized junctions: It is better to do alternative checks(constraints, validations etc) before making any object entrance at synchronize state. It will help in filtering objects and reducing load at synchronized channel. Synchronized code should be optimal and efficient.

Remaining Tips will be updated soon..

Sunday, December 10, 2006

JAVA TIPS AND TRICKS

Tips to make your program better
1. Don't make any class file larger than 1000 lines
2. Avoid Using Global Static function, if not possible then put that all function in a separate class.
3. Before starting the project Check out the proper decomposition of classes, interface and function. Practice of making structure then start coding.
e.g All Nouns is the probable candidate for classes and All Verbs are probable candidate for functions. User Interface wherever possible.
4. Always try to features given by java, don't try to write your own code if java provide such thing like StringBuffer, HashMap, ArrayList etc.
5. Always use the System properties handled by java for example. Don't use "\n" u can use
System.getProperty("line.separator");
6. Always Use Java coding convention(if you don't have company specified) and one popular IDE(eclipse/NETBeans etc) for programming.
7. Properly handle exception and their preferred treatment and if possible create a separate Exception Handling class where After Error message that thrown exception will be caught.
8. Avoid use of public data types for any class.
9. Don't use static function for breaking up the code instead use default functions like
-----
------- public abc(StringBuffer[] arg)
----YOUR CODE

----------(AFTER 50 LINES)
Partition can be done by two ways.
arg=doRemainingOperation(arg);
or
doRemainingOperation();
(by using default scope).
10. Convert all reusable components into class/widgets etc. To make them useful for other projects.
12. Do proper documentation for more information check out COMMENTS ON JAVA
it will help in future updation of code, also use fullname for variable don't use i,j,k,a,b,c etc.
e.g. customerCareServiceNumber, customerCareAddress,customerId,
13. In case of database connectivity, use the same name of variables for storing the value of that field i.e FIELD_NAME should be the variable name which contain value of that FIELD_NAME. It will help you to cover out all the errors by mismatch naming.
14. Always break up complex calculation by declaring new variables, it will be really helpful in removing small problems.
e.g.
x=-b+Math.sqrt((b*b-4*a*c))/(2*a) ...(COMMON MISTAKE)
can be solved by using variable
descriminent= (b*b*-4*a*c)
(-b+Math.sqrt(descriminent))/(2*a)
One more thing Always use brackets extra bracket don't create any problem. e.g. the above one without bracket will be quite complex
x= -b/2.0/a + Math.sqrt(b*b-4*a*c)/2.0/a
which seems quite complex

15. Take proper benefit of the scope variables e.g. if the variable maintain different index in a loop declare it at loop .
16. to help Garbage Collector Always unassign unused variables at Finally. and by marking null
e.g String source;
---source work over----
source=null;
Garbage Collector automatically free the space of source!!
16. properly put comments on the programmer I believe that comments are more important then the program because it helps in future reference and quick Accessing and understanding codes and the complete flow. Always use the tags to check out what can be do to update it, it helps in searching e.g. @TODO or @UPDATE xyz by using Hashmap.. etc..
with information why this updation needed
17 Expert Programmers can found tips on JAVA TIPS AND TRICKS BLOG
18. always try to use functions instead of procedures..
More Tips and tricks on "Threading,Database Connectivity, Session Management, Swings, JFrame,STRUTS,EJB" will be posted soon.

****PAGE IS BEING UPDATED REFRESH AFTER 2 Min****
I request you to please leave the comment for further improvement of the blog.