Header Ads

Code Smarter, Not Harder: 6 Efficient Coding Practices You Should Adopt




Again, Hi guys :) Hoping you are doing good progress in your coding career. So guys, today I will not cover any coding concept here. All coding topics will help you in cracking an interview, giving you a high paid job.
but today, I will talk about something that will help you even after getting that job. As you guessed from title, we ill be talking about some tips and practices that will help you writing clean and short programs. In this article, I will not tell you any tricks to do so, but the efficient practices you should adopt.
because, sometimes you write the correct code for a problem, but get rejected due to longer code lines.

So let's start...

1. Simplify the symphony

Suppose you are making tea/coffee. you go step by step by firstly boiling the water, etc. Similarly, when solving a problem, you go step by step to solve it. When writing the code, break the code into segments and write statements step by step and in order, so that anyone seeing the code find it easier to understand. for example, suppose you are writing a html code for a website, so you should go step by step by adding (i) background size, (ii) color, (iii) header and footer, etc. Instead, you should not do like, first you add all the elements of the website, and then at the end you give background size and color to it.

2. Comments are compasses

this is quite understandable by heading. comments are very important to add in the code, so that other people can understand it. Also, adding comments in your code make it more meaningful.

3. Debug Ritual

you heard it right, debugging the code, even if you are sure about it's correctness, is a good practice to adopt. Even your code is correct, debugging it helps you to see it moving, gives you to pass some more testcases. And obviously, if you are unsure about your code's efficiency, it helps you making it more efficient.

4. Go Shorthanded

Using shorthand properties not also make your code shorter, but also looks more managed. Some most used shorthand properties includes : 
            (i) Enhanced For loop : 
                                                   for (int num : arr) {
                                                        // do something with num
                                                    
                 :- for every element with index "num" in the array "arr"

            (ii) Shorthand if else :
                                                    int result = (condition) ? value1 : value2;
                 :- if result = condition, return "value1" , else "value2" 

            (iii) list initialization : 
                                                    List<String> list = new ArrayList<>() {{
                                                                                    add("item1");
                                                                                    add("item2");
                                                                            }};
            (iv) Starting a Thread :
                                                     new Thread(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                    // Your code here
                                                           }
                                                    }).start();

            (v) Lambda expressions : 
                                                    (parameters) -> expression
                                                    example : (int a, int b) -> a + b;  

5. Variable Verse

give your variables meaningful names. like when adding two numbers, give names like "firstNum" and "secondNum". When writing long codes, give some variables names as characters of a story. this makes easier to understand the logic and implementation behind longer codes.

6. Automate your tasks

Writing slightly more technical code doesn’t mean it has to be less readable. Multiple lines of duplicate code are not only harder to read, but they also increase the chance for error. The great thing about programming is that you can automate certain tasks using loops and switch case statements. for example :- 
Suppose you have an array "boxArray" with each element having x and y dimensions.
                boxArray[4] = {box1, box2, box3, box4};
one way to assign dimensions to every box is : 


other (more efficient way) to do this is :


CONCLUSION

Coding in Java is an art of combining efficiency, readability, and elegance. Utilizing a diverse array of techniques and shortcuts—ranging from enhanced loops and ternary operators to string formatting and the builder pattern—developers can sculpt their code with finesse. These practices, tried and tested, have become the cornerstone of Java development, empowering programmers to craft solutions that are not only concise but also maintainable. Embracing these best practices is akin to conducting a symphony, where each line of code harmonizes to create a masterpiece, resonating the essence of efficient and elegant programming.





HAVE ANY DOUBTS ? Feel free to ask in Comment section
HAPPY CODING :)

1 comment:

  1. Thank you for these tips!
    Could you write an article about debugging tips, techniques and methods? It would be a great help.
    Thanks again!

    ReplyDelete

Feel free to ask your doubts :)

Powered by Blogger.