What are some techniques to facilitate safe refactoring in Javascript? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm coming from statically typed languages like C++ where there are tools (the compiler for one) that will remind me if I forget something obvious while refactoring. For example, if I change the number of arguments to a function but I forget to change all the calls to it. But it seems really easy to make this type of mistake in javascript. How do you avoid problems like this when refactoring javascript?

just like you do in c++/java. By writing tests.
Unit tests are the best. If you code is MVC, then you can certainly have unit tests with little effort, at least for the model layer. The benefit is you get feedback immediately. Check out QUnit
Functional tests via Selenium or equivalent are good too. They will find problems, but not immediately.

Only thing like that I can think of is: JSLint (http://jslint.com/)

Related

Javascript: Renaming built in functions [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In Javascript, is it acceptable to rename built in functions. For example, in my code, I use document.querySelector() tons of times. This is extremely long and tedious to type every single time (even with autocomplete from the IDE's side). Therefore, I decided to create a new function with a shorter name like the following:
let qs = selector => document.querySelector(selector);
Is this an acceptable practice in the JS community? Is this considered bad code? If so, why? Additionally, if doing this is alright, what are some drawbacks?
Thanks.
No.
Someone is going to come behind you to edit your code.
They will then have to track down your renaming function to actually see what it does.
Create an snippet in your IDE if it’s that much of an issue for you.

What is the best way of testing that, removing unused tables(migration) do not affect the application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
We have some very old tables which we do not use. I am planning to remove those. My initial plan is to rename the tables and test if our application is affected anyway. But I am not sure how to test the application and make sure that it's not affected.
Ideally, you should have tests for your application, which are going to break if anything is using the deleted tables.
Assuming that you don't have tests, the next best way is to run a global search in your codebase and look for those models / table names being used in the code. If you still don't feel confident, you can manually go through every page and make sure that nothing is broken. Depending on the size of your app, that might be really slow and painful, but it's what you get for not writing tests from the start :P
Good luck!
Run all your automated tests. If you don't have any, right now is always the best time to start adding them.

Combining JS Frameworks [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
From an experienced developer's perspective, is bad practice to create a web application using multiple JS frameworks ?
For example, if you start using AngularJS and if some tasks of the project can be done easier with JQuery, should you go for it, or try to make that part in Angular too ?
In my opinion, a framework should only be used if it's absolutly necessary. E.g if you do lots of DOM work, jQuery is the right one.
But, if you need a mvn framework, go for angular/backbone or something like this.
Tio many people today think that for each and every single problem, a framework is the best solution.
Sometimes, it could be the best solution to use 2 frameworks. E.g. jQuery + lodash.
So the answer is... It depends on the type of application you want to develop. My approach would be to allways ask whether the framework is really needed, or if you maybe just need a single function that you better could write on your own.

Is there an IDE that highlights possible logical errors? If so, what is the best? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
For example, after spending an hour on the following javascript logic error, I finally realized the problem. Keep in mind that this if statement is embedded in a lot of other code.
for(i=0;i<alength;i++)
{
if(myvar = correct)
{
//ommitted irrelevent code
}
}
As you know, I was assigning the correct variable to myvar as well as comparing it, so it should have been:
if(myvar == correct)
After finally realizing the error, I was very annoyed that I didn't recognize it much faster. I have been programming for over 5 years, mostly in java, but this happens quite often and I was wondering if there is an IDE or something that can highlight possible logical errors.
You could have your code linted by JSHint.
There should be plugins for any major editors.
In this case, it would have told you "Expected a conditional expression and instead saw an assignment."

javascript guidance re arranging functions into files [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Have written a large system in ColdFusion/Javascript/MySQL. There is lots of Javascript. The few functions which are long are separated into cases. Currently I have all the js in 3 files, depending on the section of the application to which they refer.
Every now and again, some function which tested okay before turns out not to be working (I might have changed something elsewhere to cause that problem, without realizing I had to retest).
Okay, I deserve that, but when one js function in the file stops working, sometimes others do as well. So unexpectedly something I was counting on to validate things, or prevent a submit in the presence of errors does not work.
I've thought of breaking up the js so that any javascript function which is used by just one program will be resident there, and I would use the files with several js functions only for those that several programs have to access. That would insulate my functions somewhat from problems occurring elsewhere.
Is this approach recommended? Could someone offer good reasons for or against it? Or is there a way to "firewall" my functions so that problems with one do not spill over into another?
I'm feeling that the js is a weak link in my system -- that I can't trust it; yet I need it, so I have to find a way to make things more stable. Any and all suggestions to help would be much appreciated.
The suggestion "get better at Javascript" has already occurred to me. I am trying. Meanwhile, my needs are not very demanding. All my functions do simple things and are written in a pretty straightforward way. Yet I am having all this trouble.

Categories