Agile approaches are not usually standards-driven, so issues of standards compliance are not usually considered. Taking the initiative to refactor code relies on (and, should be) individual responsibility. The following pointers apply to both JavaScript usage in frameworks and plain native JavaScript.

1. Substitute directly evaluated expressions with descriptive variables

While considering performance, the ideal approach would be to use fewer variables and thus reduce overall memory requirements. While working with jQuery, I find myself using the following code more than often:

$(‘.some-el’).click((event) => {
    $(‘.’+$(this).attr(‘id’)).addClass(‘some-css-class’);
});

At the time I’m writing this code, I know that the HTML element with the class **'some-el'** has an **id** that represents a target element, and a click event on this element should modify _that _target. It gets the job done, but it’s messy. Not so easy for future maintenance. This once, we can forego performance in favor of legibility. Besides, this is 2020, computers can handle a few variables! I, therefore (almost always) resolve to use the following:

$(‘.some-el’).click(() => {
    let target = $(this).attr(‘id’);
    $(‘.’+target).addClass(‘some-css-class’);
});

#development #web-development #code-optimization #programming #javascript

Writing Cleaner Code in JavaScript
1.20 GEEK