Saturday, June 29, 2019

What Features of Java is Used very often By Experienced Programmers?

In this post you will know what are the features of java is used by experienced Programmers but not junior developers. A very simple test to differentiate between an experienced programmer and a newbie is to ask the two to concatenate about a 1000 unidentical strings.

A newbie programmer would do it as simply as

public String computeResult(String[] arr) {
    String result = "";
    for(int i = 0; i < arr.length; i++){
        result = result + arr[i];
    }
    return result;
}

The problem with the above code snippet is that the plus operator when used to concatenate strings has a major overhead that compromises to the performance.

Although the use of the "+" operator is substituted with StringBuilder which is further converted to a String during compile time, this sequence of events would be taking place repeatedly within the loop and the code would be creating and then discarding an intermediate String object repeatedly.

Now, if an experienced programmer was asked, he/she would realize that we are dealing with a lot of strings and would do the same task as -

public String computeResult(String[] arr) {
    StringBuilder sb = new StringBuilder();
    for(String temp : arr)
        sb.append(temp);
    return sb.toString();
}

Of course its possible that the programmer might divide the array in two parts or more and then have multiple threads each computing its own concatenated string; further there might be a final merge which would connect all such strings but that's not the point.

Coming back to the question, using StringBuilder to concatenate Strings can be a feature usually used by experienced programmers.

2nd Example:

Beginner:

ArrayList<String> stringList = new ArrayList<String>();

Experienced developer:

List<String> strings = new ArrayList<>(); //Java7+

There is also the case of writing interfaces. This is a bit more complicated than to write a concrete class, since there is an extra level of abstraction. Some new developers might find this cumbersome and avoid it it not necessary. However, interfaces have great advantages in maintaining contracts.

public interface Car {
    void deaccelerate(BreakType type); // HARD, PANIC, SOFT, etc, ...
    void accelerate(float acceleration); // Newton Meter?
    void changeGear(Gear gear) //ONE, TWO, ...
}

public class CarFactory {
    public CarFactory(){}

    public Car getCar(CarType type) {
        //Implement
    }
}

class Ferrari implements Car {
    // Implementation
}

class Fiat implements Car {
    //Implementation
}

So basically you can only access the internal implementations of the car unless you use reflection. There is no way that someone will be able to use anything else than you allow them to. The only way for the users of the Car class will be able to create a Car is by using the CarFactory. Then the user will get a Car defined in the CarType enum.

A possible issue is that all new Cars need to be added to the CarType struct, so it is not possible to do own extensions. However, Car is just an example that I use and is easy to come up with. This kind of implementations is normally done for things that should not be meddled with outside of the API that is used.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...