Tips and Tricks for Java or any other Programming Language you use.




"Experience makes man perfect". This quote makes a perfect sense if you are good programmer then you can even share some tips & tricks. Here I'am sharing my personal experience through out my Programming Life. I've been programming since 2007 and passed through tons of lines of code. Now lets cut the bullshit and straight to tips and tricks.




1. Cut the bullshit
Divide your code in more than one file. Write smart code not hard code. Write less do more.

2. One for all.
In General file. Say if you have functions-methods used in multiple files then create object of file one for all(importClass.java). If you don't get it copy/paste code and compile-run files. Say if you need multiplication in other file so you don't have to to create object of file just use : importClass.createMath().mul(10,20);
//math.java
public final class math {
    public final int add(int x,int y){return x+y;}
    public final int sub(int x,int y){return x-y;}
    public final int mul(int x,int y){return x*y;}
    public final int dvd(int x,int y){return x/y;}  
}
//importClass.java
public final class importClass {
    public final static math createMath(){
        return new math();
    }
}
//Demo.java
public class Demo {
    public static void main(String[] args) {
        System.out.println(importClass.createMath().add(10, 20));
    }
}
3. Variables file(variable.java). Create general variable using final(if you're sure), static.
Changing variable name in one file will affect in all files. Say if you want to change the database host,username,password its kick-ass way. Note: Import required libraries.
//variables.java
public final class variables {
    public static final String dbhost = "jdbc:mysql://localhost:3307/demo";
    public static final String dbusername = "root";
    public static final String dbpassword = "root";
    public static final String dbdriver = "com.mysql.jdbc.Driver";
    private static String alphabet = "G8abHcId1J23Kefg9L0MhOPijkQlRmnSopT45UVqrWsFXtuY6EZ7vwCxByzA";
    public static final String fileNotAllowed = "*.jsp,*.java,*.class,*.html,*.php,*.exe,*.sh,*.lnk,*.msi,*.js | *.JSP,*.JAVA,*.CLASS,*.HTML,*.PHP,*.EXE,*.SH,*.LNK,*.MSI,*.JS";
    public static final String fileAllowed = "*.zip,*.doc,*.docx,*.ppt,*.pptx,*.pdf | *.ZIP,*.DOC,*.DOCX,*.PPT,*.PPTX,*.PDF";
    public static final String ImageFile = "*.png,*.jpeg,*.jpg,*.bmp,*.gif,*.tif,*.tiff,*.tga | *.PNG,*.JPEG,*.JPG,*.BMP,*.GIF,*.TIF,*.TIFF,*.TGA";
    public static final String mediaFile = "*.mp4,*.mp3,*.flv,*.mov,*.f4v,*.3gp,*.3g2,*.m4a,*.aac | *.MP4,*.MP3,*.FLV,*.MOV,*.F4V,*.3GP,*.3G2,*.M4A,*.AAC";

    public static final String _currentDate() {
        Date d = new Date();
        return d.toLocaleString();
    }
    public static final String getRandomNo() {
        Random rand = new Random();
        String token = "";
        char tokenChar = '\0';
        for (int i = 0; i < 20; i++) {
            tokenChar = alphabet.charAt(rand.nextInt(alphabet.length()));
            token += tokenChar;
        }
        return token;
    }
    public static final Connection getDatabaseConnection() throws Exception {
        Class.forName(dbdriver);
        return DriverManager.getConnection(dbhost, dbusername, dbpassword);
    }
}
//Demo.java
public class Demo {
    public static void main(String[] args) {
        System.out.println(variables._currentDate());
    }
}
If you have any other Tips & Tricks then share with us. Keep codding...

0 comments :