yFaizuS / Delivery-International

0 stars 0 forks source link

Data Clumps #2

Open yFaizuS opened 1 year ago

yFaizuS commented 1 year ago
cart.js

`addCart()` function ```javascript const addCart = (id) => { const data = ""; const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }; ``` `getCartAjax()` function ```javascript const getCartAjax = () => { const data = ""; const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }; ``` The headers object is repeated in both functions to avoid duplication we could refactor it into a function or an object

item.js

```javascript const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }; ajaxRequest( `api/dish/${idParams}`, 'GET', JSON.stringify(data), function (response) { // AJAX success callback }, function (jqXHR, textStatus, errorThrown) { // AJAX error callback }, headers ); // ... ajaxRequest( `api/dish/${idParams}/rating?ratingScore=${i}`, 'POST', JSON.stringify(data), function (response) { // AJAX success callback }, function (jqXHR, textStatus, errorThrown) { // AJAX error callback }, headers ); ``` The Data clumps exist on the Headers object, it appears multiple times on an AJAX requests

registration.js

```javascript const data = { fullName: $('#fullName').val(), password: $('#password').val(), email: $('#email').val(), address: $('#address').val(), birthDate: $('#date').val(), gender: $('#gender').val(), phoneNumber: $('#phoneNumber').val() } ``` from the code snippet above, the data of the user is clumped together as "data" and also passed around as "data". it can be counted as a data clump as the data simple clumps together in the "data"

LidiaIvanova commented 1 year ago

Data clumps means separated variables. First example is correct, but second and third are not. Why is this code bad? What principles does it violate? What can the existence of such code lead to? 0,8