Create a function or use the code directly?

0

What's most recommended?

In a situation that I can use both ways (I can choose, it will work the same way).

What is recommended to do? Create a function or insert the script directly into the JS file?

Basic example:

if(2*5 == 10){
   console.log('Verdade!');
}

Or

function conta() {
   if(2*5 == 10){
      console.log('Verdade!');
   }
}
conta();

This was just an example, but it sums up what I want to know.

Once again, reinforcement I know that in certain places I will not be able to create the code directly because it will not make sense and errors will occur, but there are some things so small that in the day to day we have to do , which would like to know which way is recommended, if there is something in the processing speed, etc ...

If this question is based on opinions, sorry!

    
asked by anonymous 05.07.2018 / 20:56

4 answers

3

The answer is "it depends." Knowing which is the best alternative depends a lot on what you want to do. In some cases one function is better, in others, just having the code is better.

However, when the code starts to get big and complicated, it's a sign that it should be better modularized, and that's where functions and even objects come in. So, except for the simplest and simplest cases, having everything inside functions and objects produces a code in which maintenance is easier. On the other hand, simple and silly cases can be quite common.

    
05.07.2018 / 21:09
1

I prefer to create function even if it can be used without them because I imagine in sites with several Scripts where one var can interfere in the other so using function would avoid conflict because it only exists within that function n interfering with outside codes

example with function:

var i =1;
    function ex(){
     var i=0;

    }
    ex();


if(i==1){
         console.log('Verdade!');
        }else{
console.log('amarelo!');
}

In this example it will go green.

example without function: enter the code here

    var i = 1;
    var i=0;
if(i==1){
             console.log('Verdade!');
            }else{
    console.log('amarelo!');
    }

It would go into yellow

    
05.07.2018 / 21:12
1

Each case may be a specific case, but it is necessary that you observe these items for good practice on whether or not to create a function.

You will create a function for:

  • To allow reuse of code already built (by you or other programmers). In your example a sum function would be useful as a function to be reused whenever you needed to calculate the sums.
  • To prevent a snippet of code that is repeated several times within the same program; Imagine having that routine mode for some reason, you would have to overwrite all the places that the code snippet appears. If you modularize, you can only change one location and everything will be resolved.
  • So that the blocks of the program are not too big and consequently more difficult to understand;
  • To separate the program into parts (blocks) that can be logically understood in isolation.

Having these rules in mind you will realize when you need to create a function or not.

Always think of your code in a scalable way. Sometimes you do not need a routine in a method or function at the moment, but by project structure you can predict what will be needed in future modifications and improvements

    
05.07.2018 / 21:33
1

It's optional to leave the code directly or in a function, but I advise you whenever you can use a function, I'll give you an example.

Think about the situation where you have a function that receives notes from school students and which returns you which ones were approved and within it you check various skills for student approval. If you use functions, when you need to change the form that will be checked for student approval you will only change in the function and if you use direct code, you will have to change a part of the system structure. When we are learning with small and simple applications this does not make a difference, but now imagine this in a great application.

    
05.07.2018 / 21:39