Create a role or use the code directly?

Asked

Viewed 67 times

0

What is most recommended?

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

What is recommended to do? Create a role 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();

That was just an example, but summarize what I want to know.

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

If this question is based on opinions, I’m sorry!

  • 3

    I think it’s kind of personal opinion, but I would say that if you’re going to use the same code in more than one place: create a function to stay more DRY

  • 1

    You can give an example of these "things so small that in everyday life we need to do" to better understand what kind of things are ?

  • 1

    Each case has its peculiarities and, as it is not a real example, it is difficult to answer.

  • Isac, I work for example in a company that is an E-commerce, and another company provides a Magento Saas for us, that is, I do not have access to Backend and in some pages also of the same, and sometimes I need to change for example, text of some buttons (example this). I simply take jQuery $('.myClass'). text('New name') or should I create a function? Even though I don’t need to reuse that code again.

4 answers

3


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

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

  • 1

    It really does not have a correct form in my view and yes it depends a lot of the case, for code that can be reused it is always good to create a unique function!

1

I’d rather create function even if it can be used without them because I imagine on sites with several Scripts where a var can interfere with other so using function avoid conflict because it exists only inside that function n interfering with codes from outside

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 above 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!');
    }

he would enter the yellow inserir a descrição da imagem aqui

1

Each case may be a specific case, but it is necessary that you observe these items for the good practices of the needs to be created, or not, a function.

You will create a function to:

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

Keeping these rules in mind you will realize when it will be necessary to create a function or not.

Always think of your code in a scalable way. Sometimes you don’t need a routine in a method or function at the moment, but by the structure of the project you can predict that it will be necessary in future modifications and improvements

1

It is optional for you 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 grades from school students and that returns which have been approved and within it you verify various competencies for student approval. If you use functions, when you need to change the form that will be verified the student’s approval you will change only in the function and if you use direct code, you will have change a part of the system structure. When we are learning with small and simple applications it makes no difference, but now imagine it in a great application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.