Smart Code

Smart Code

Smart Programmers often use smart techniques while coding.
Let's recall the ways they use and implement in our own code.

Let's move🚶🏼

Short Code

Make your code short, crispy and easy to understand by other programmers.
There are some syntaxes you can use to shorten the length of your code.

First among them is ?ternary operator, take a look:

if( num%2 == 0)? even:odd

We have another such i.e. switch statement

But what if some programmers came to ask about these shortcodes, and then make them understand shortcodes?

Another way to shorten your code is to write short variables, a, b or c.

I will suggest not to write such short variables in the sake of writing small code;

Like, a short variable vanishes from the code. Nobody will be able to find it using the editor's "search" function. Nobody will be able to "decipher" what the names an or b mean, even if they try.

By using abbreviations

Using a large variable is ambiguous at many times and also using a single variable too,
then how to name a variable🤔,

Let's learn
If not a single variable or large name, we could use an abbreviation for an ig one i.e.

Some Examples:

  • headerhdr.

  • userSalaryus.

  • browserbrsr.

etc...

A good programmer would be able to catch your meanings,

Taking forward..

Try to be more realistic

To make your variable more related to what you want to achieve through it..

There are some ways through which you can try to name your variable -- a more better and human-readable name.

Let's get it forward..

Try to use the word that is the most spiritual when choosing a name. like an object, data, value, item, element, etc.

Data is the best name for a variable. Anywhere you can use it. Every variable does indeed contain data, right?

Datatype is also the best method to name a variable, the datatype tyo which value belongs str, num

The variable name still has meaning, yes. It specifies whether a string, a number, or something else is stored inside the variable.

Debugging makes it simple to identify the value type. But what does the variable actually mean? What string or number is it storing?

Without a solid meditation, there is simply no way to know!

Using similar names

Using similar names for the same things makes it easy to name a variable.

Likely, if a function shows something on the screen, we could name it with a variable starting with display i.e. displayMessage.

But what if we have a function that is also used to display something on screen?

For two functions with important differences – use the same prefix!

displayResult, displayAnything

Reusing name

Add a new variable only if it is necessary.
Reuse your variable, and add new values into it.

Try to use only the variables that are passed as parameters in a function.

The approach can be advanced further by covertly (!) replacing the value with a similar one in the middle of a loop or function.

function ninjaFunction(elem) {
  // 20 lines of code working with elem

  elem = clone(elem);

  // 20 more lines, now working with the clone of the elem!
}

It will surprise a coworker programmer who wants to use elem in the second half of the function. They won't realize they're working with a copy until they start debugging and looking over the code!

often seen in code. even against an expert ninja, deadly effective.

Use Underscores(_)

By putting underscores _ or __ we define the hierarchy of a variable or some variable associated with its prefix.

Example:

Imagine we have a card of cricket players and we have a name in the card.
Then, if we have to change the color of the name of the player, what we could name the variable is card__name

By naming variable name as card__name we are doing error-free naming of variables this way.

<div class = "card">
    <div class = "card__name">
        Rohit Sharma
    </div>
    <div class = "card__age">
        xy
    </div>
</div>

Create Unique names

You can create unique and catchy named variables.

Like creating beautifulCard , superText instead of beautiful or super , having no meaning in themselves.

Overlapping outer variables

Variables inside and outside of a function should have the same names. as basic. No attempts to come up with new names.

let name = oldUser();

function render() {
  let name = newUser();
...
    // block of statements  
...
}

A programmer who enters the render without looking around is likely to miss the fact that a local user is shadowing over the external one.

After that, they'll try to work with user under the assumption that it's an external variable that newUser() returned.

Skillful Functions

The function should not be constrained by the name. Be more comprehensive.

For instance, the function validateEmail(email) could display an error message and request that the user enter the email again in addition to validating the email.

The function name shouldn't give away any additional actions. A true ninja programmer will hide them in the code as well.

Creating a single action from multiple actions prevents code reuse.

Imagine that a different developer only wants to check email and not produce any output. They won't be satisfied by your function validateEmail(email), which performs both. As a result, they won't interrupt your meditation by asking any questions.

Take care of some of the coding practices after this blog!

I Hope you loved the blog❣️

See u in the next blog...Stay tuned🎶

Don't forget to follow me on:
Twitter LinkedIn