DEV Community

Cover image for Best Practices in Coding for Beginners
shreegowtham27
shreegowtham27

Posted on

Best Practices in Coding for Beginners

There are a few things that you need to take care of while you're coding.

These things will improve the readability of your code and also increase your confidence level of you in coding.

  1. Indentation.
  2. Case Convention.
  3. Commenting on the code.
  4. Using Reusable Code.
  5. Using env Variables instead of Hardcoding secret Keys.

1. Indentation

A proper Indentation will make your code look more beautiful.

Not only that, more of readability. when you can read your code easily, you can debug more faster.

Have a Tab Indentation, instead of space indentation. (4 space indentation is Tab indentation).

You can use the Rainbow Indent extension so that the code will be more pleasing for you in your code editor.

2. Case Convention

Typing a variable is also a major factor when it comes to programming.

using random variables like var d = dog; is completely ugly when comes to the professional level.

Use a Variable name based on what it is giving you or what is going to be stored in the variable name.

If you want to store a pet name, use a variable, var PetName and not as random as var d, var k. Stop those things. But using i,j,K is the most common starting from the very first code till the end in loops, especially for loops.

Case Types.

  • camelCase
  • PascalCase
  • kebab-case
  • snake_case

There are some more cases as well. But try to be fixed in these cases.

Based on the language that you use also plays a major role in choosing your typing case convention.

Eg: camelCase and PascalCase are the most commonly used patterns in JavaScript.

snake_case is most preferred in languages like Python and PHP.

GoLang again goes with Camel and PascalCase.

Even the Database you use, haves some conventions like this.

When you're in MongoDB, you can see camelCase and PascalCase mostly. similarly, when you're in MySQL, it is more common to see snake_case.

kebab case is something getting a high level in using URL path.

eg: website,com/this-is-a-path.

you can also see the website path with snake_case as well. like, website,com/this_is_a_path.

3. Commenting the code

You write a certain block of code, later on, you go to build another set of code, when you come back to the first set of code you have written, you don't even know whether you have written the code or someone else.

It is always a best practice to comment on your code / What is your code trying to do there. so that you will understand in a better way.

It is better to comment/ take notes while coding, then convert those notes to comments,

Eg: You need to get a user name from the user, but you should allow it only if it is more than 5 characters long.

// Name is empty

if(!Name)

// Name is less than 4 chars.

if(Name.length<4)

// if Name is present and more than 4 chars.

if(Name && Name.length>4)

In this way, you don't have to worry about commenting on your code.

This also helps to focus more on coding, since you have already written what to do next.

4. Using Reusable Code

There is something called a "DRY principle", Which is an acronym for - Do not Repeat Yourself. You will have to write a code once and have to use those classes/methods/functions wherever it is necessary.

There should not be the same block of code that is repeating over not only in the folder but also in the whole application.

5. Using env Variables instead of Hardcoding secret Keys

There is something called environment variables, this has become a kind of compulsion for every programmer to be known.

An env variable or environment variable is simply another variable, which will be set outside the program or functionality, still, it runs on the process of the program or functionality. (Tried my level best in simplifying)

These are also key-value pairs, just like the usual variables.

Eg :

DATABASE_NAME = "name"

DATABASE_PASSWORD = "password"

(Here again, you can see I have used a different case, this is the most common case in the env variable and is called MACRO_CASE or UPPER_CASE_SNAKE_CASE)

Most of the constants in any language can be seen using UPPER_CASE_SNAKE_CASE.

with this env variable, there won't be any need to hardcode secrets like your passwords, Just use these variables to make your code to the next level secure.

unless you add the .env file in your .gitignore file, it won't be a secret anyway

Happy Coding!

Happy Weekend

Top comments (2)

Collapse
 
nftcryptomix profile image
nftcryptomix

Thanks for the tips

Collapse
 
shreegowtham27 profile image
shreegowtham27

Thanks for your positive comment <3