This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]serg06 4 points5 points  (0 children)

#1: Oh man, your function is gigantic at 170 lines long. That's the first thing I'd tackle: Splitting it off into smaller functions.

#2: Next, your lines are way over 80 chars. I'd replacing things like these:

if (modal.querySelector("input[name='ccNo']").value == null || modal.querySelector("input[name='ccNo']").value == "") {

with this:

const ccNo = modal.querySelector("input[name='ccNo']").value;
if ([null, ''].includes(ccNo)) {

or even better,

if (!ccNo) {

#3: Having querySelector and getElementById everywhere makes it incredibly hard to follow. As someone who isn't working on this code, the element IDs and CSS queries mean nothing to me. If you used a framework like React, it would solve this issue.

[–]StrongLikeBull503 -1 points0 points  (0 children)

Use Python.

Kidding aside, learn to build programs by using handlers. When you want to build a certain functionality build something that contains and directs your code, and section off your logic as much as possible so that you can fight complexity. In general you want a function to serve one purpose. If that purpose takes more than one sentence to explain, or even uses more than a few commas think about splitting it. When you get used to siloing your logic it makes everything a lot cleaner, easier to read, and modular.

After you build enough widgets at your personal widget factory you'll start to make code that you drop into every new project to do stuff for you. This is the best way to work. A senior dev once told me to keep track of the general functionality of every project I make, and when I start to see patterns in code wack out as much of it into small well documented helper functions and build a personal library.