Most Common Mistakes Made By An Inexperienced Programmer Or Developer

Hi! in this article, we will look at the most common mistakes that are made by Programmers and Developers over years. There are a lot of lists on the internet but we will look here to list the most common ones and also resolve the solution how to avoid that mistake in the future.

Both beginners and experienced programmers make mistakes. Since we have touched on this topic in the article, we are sure that you'll benefit from reading about this "rake" that most beginners step on.

1. Compare Strings & Objects in Java

Most Programmers use the  == operator to compare Strings. The == operator compares references stored in the objects rather than the value. References are the points where the actual value is stored in the memory. If the value is the same and stored in a different address(Reference) this will return false whereas we using the Object class method equals that check by value will be always right.

String name = "ravi";
String newName = "ravi";
System.out.println(name==newName); --> true

String mynewName = new String("ravi");
System.out.println(name==mynewName); --> false
System.out.println(name.equals(mynewName)); -->true 

So, why do programmers make these mistakes, It's simply because of the storing system of any programming language. In Java, strings are stored in the String pool, and objects are stored in the Java Heap Memory. So here is the catch java string are immutable (unchangeable) let's string a and b contain the same value then it points to the same address but this is not the case in the object, if we want to create a different reference of string we can do that by new keyword. Below is the case of the object 

public class Cat {
    private String name;
    public Cat(String name) {
        this.name = name;
    }
    public static void main(String[] args) throws Throwable {
        Cat cat1 = new Cat("lilly");
        Cat cat2 = new Cat("lilly");
        System.out.println(cat1 == cat2);
        -- > false
        System.out.println(cat1.name.equals(cat2.name));
        -- > true
    }
}

Compare Strings & Objects in Java

 2. Reinventing the wheel

  1. Not knowing regular expressions is a big one. I've seen newbies write 3 pages of code that could be replaced with a single regex.
  2. Writing Java/C++/whatever code for something they could have done with a simple shell script
  3. Not checking to see if there's already a library/module/framework to do the thing they're trying to do

 

3.  Overlook the Names, Writing Messy Code, and Ignoring Code Quality

If you dig into someone else's code how often, Instead of two hours, you may spend two days to simply understand the logic behind the code. The funny thing is that for the person who wrote the code, everything is clear and entirely transparent. This is not surprising: after all, perfect code is a very vague concept, because each developer has their vision of the world and the code, too. More than once I have been in a situation when a coworker and I looked at the same code and had different opinions about its correctness and cleanness.

  • Use correct names (do not use any name like abcd, xyz that is off the road) - 
  • proper indentation in code
  • refractor big function into small that is more understandable
  • Instead of the use of stashing all the code in a line differentiate that into new lines and white space.
  • Not commenting or over-commenting on the code
  • use comments only if necessary do not overburden the code by leaving a senseless comment.

Correct names

Naming interfaces - start with a capital letter and are written in CamelCase.

Class names - Just like interfaces, class names are capitalized and use CamelCase.

Method names - Usually, method names begin with a lowercase letter, but they also use camel case style (camelCase).

Variable names - In most cases, variable names begin with a lowercase letter and also use camelCase, except when the variable is a global constant. In such cases, all letters of the name are written in uppercase and the words are separated by an underscore ("_").

Naming convention

4. Use of Static and non-static variable 

The beginner always muddles in static and non-static contexts. So here is a simple difference, when a class is loaded into memory a static object is created immediately. This object stores static class variables (static class fields). The static object exists even if no ordinary (non-static) objects of the class have been created.

Ordinary variables of a class are bound to objects of the class (instances of the class), while static variables are bound to the class's static object.

The static method can't use the non-static variable. This makes sense: after all, a static method can be called without creating an object of its class, and all fields belong to specific objects. but you can use static variables in non-static methods:

class Solution3 {
    public int a = 1;
    public static int staticVar = 2;
    public static void main(String[] args) {
        System.out.println(a); // Non-static field 'a' cannot be referenced from a static context 
    }
    public void useStatic() {
        System.out.println(staticVar); // This is acceptable!
    }
}

Use of Static and non-static variable

5. Removing elements from an ArrayList

After removing an element from the ArrayList its size is affected dynamically and the positions of the remaining elements immediately change.

Let's assume you have 10 elements in the list and you have to remove 3 last from the list. 

Here's how to do that incorrectly:

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++)
        list.add(i);
int n = list.size();
list.remove(n - 3); n=10 , ->7 removed size 9
list.remove(n - 2); n= 9 , ->8 removed size 8
list.remove(n - 1); n= 8, ->9 removed size 7 (ERROR size is 7 but removing 9th element)

Assume this is like a plate over plates that will be shifted just after the removal of anyone.

There is two correct way to do that, first remove from the end of the list or use the size-number of the element want to remove from the list 

list.remove(n - 1);
list.remove(n - 2);
list.remove(n - 3);

or 

list.remove(n - 3);
list.remove(n - 3);
list.remove(n - 3);

Removing elements from an ArrayList

6. Call by reference and call by value

There are two types of variables in java that is primitive and non-primitive. If you pass primitive data then is called call by value and if you pass non-primitive data to the method then it is called call by reference.

Newbies often find it difficult to understand this concept. As a result, their code behaves unexpectedly. Let's understand both by example 

Call by value

public class TimeMachine {
    public void goToPast(int currentYear) {
        System.out.println("The goToPast method has started running!");
        System.out.println("currentYear inside the goToPast method (at the beginning) = " + currentYear);
        currentYear = currentYear - 10;
        System.out.println("currentYear inside the goToPast method (at the end) = " + currentYear);
    }
    public static void main(String[] args) {
        TimeMachine timeMachine = new TimeMachine();
        int currentYear = 2018;
        System.out.println("What was the year when the program started?");
        System.out.println(currentYear);
        timeMachine.goToPast(currentYear);
        System.out.println("And what year is it now?");
        System.out.println(currentYear);
    }
}

Output

What was the year when the program started?
2018

The goToPast method has started running!

currentYear inside the goToPast method (at the beginning) = 2018
currentYear inside the goToPast method (at the end) = 2008

And what year is it now?
2018

Call by reference

public class TimeMachine {
    public static class Cat {
        int age;
        public Cat(int age) {
            this.age = age;
        }
    }
    public void goToFuture(Cat cat) {
        cat.age += 10;
    }
    public static void main(String[] args) {
        TimeMachine timeMachine = new TimeMachine();
        Cat smudge = new Cat(5);
        System.out.println("How old was Smudge when the program started?");
        System.out.println(smudge.age);
        timeMachine.goToFuture(smudge);
        System.out.println("How about now?");
        System.out.println(smudge.age);
    }
}

Output

How old was Smudge when the program started?
5

How about now?
15

Call by reference and call by value

Summary

In this article, we have covered the most common mistakes made by programmers and how we can overcome them. You can comment on any suggestion in this article below.

Thank you, Happy learning.......