Best Website-BuildersBest Website-Builders
    What's Hot

    Johnson on Partygate: We avoided physical contact but passed drinks

    March 22, 2023

    Lyon 0-1 Chelsea: Blues get crucial European win at holders Lyon

    March 22, 2023

    The Future of Black Queer Characters in Comics Is Joyful

    March 22, 2023
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    Best Website-BuildersBest Website-Builders
    • Home
    • CSS

      Google’s Bard vs OpenAI’s ChatGPT: Which AI rival chatbot is better?

      March 22, 2023

      Staff Sergeant Lixandy Estrella Cespedes > Gunsan Air Force Base > Gunsan Air Force Base

      March 22, 2023

      Social Security launches investigation into disappearance of zombie drug

      March 22, 2023

      The Markland area will be closed for about six months

      March 21, 2023

      The battle of ironclads marked the beginning of modern ships

      March 21, 2023
    • Joomla

      Reseller Hosting Business: Important Q&A

      March 21, 2023

      Web Hosting: 8 Elements Every Entrepreneur Should Look For

      March 20, 2023

      VS Code Extension for In-Browser Development, WapuuGotchi Gamification Plugin & More – WP Tavern

      March 20, 2023

      How Superior Web Hosting Support Can Drive Business Success

      March 17, 2023

      PANDACU Studio Website Development Cooperation First Page Sage SEO Dsign Chicago adstargets Cardinal Digital Agency

      March 16, 2023
    • PHP

      Denver high school student shoots two adults, police say

      March 22, 2023

      ‘The Flock’ Review: Dominic Fishback Is Awesome

      March 22, 2023

      Why I Refused to Submit TikTok Documents to Congress

      March 22, 2023

      Midjourney Reportedly Bans Journalists Over AI Trump Arrest Images

      March 22, 2023

      March 22, 2023 — Biggest news story of the day

      March 22, 2023
    • UX

      User Experience Is Key To Massive Adoption In The Blockchain World

      March 22, 2023

      The Technical Magic Of The XGIMI Horizon Pro 4K Projector

      March 22, 2023

      Cargobase improves logistics with next-level user experience

      March 22, 2023

      Thousands registered for jobs at the Air Force Life Cycle Manager Center at Wright-Patterson Air Force Base

      March 22, 2023

      Inclusion in Work: Meet Heather Crowder

      March 22, 2023
    • Web Builders
      1. Web Design
      2. View All

      What Comes First in Website Development — Design or Copy?

      February 2, 2023

      Modern Campus Honors Best Higher Education Websites of 2022

      February 2, 2023

      Premier SEO Consultant in Las Vegas, Nevada with Unparalleled Customer Service

      February 2, 2023

      Can Religious Freedom Be Saved? This group is racing the clock to teach America’s first freedom

      February 2, 2023

      How i Create New Google Account

      February 7, 2023

      CWT powers tools for meeting and event planners

      January 31, 2023

      Best Website Builder – Website Builders

      January 24, 2023

      Is There A Market For Rap-Themed Slot Games? – Rap Review

      January 19, 2023
    • WordPress

      Lionsgate Streaming Platform Exposed Data of 37 Million Users

      March 22, 2023

      Metroid Prime 4: Everything We Know So Far

      March 22, 2023

      OnePlus teases OnePlus 11 made with ‘unprecedented materials’

      March 22, 2023

      I Did Nothing Wrong: I Tried Ear (2)’s Personalized Hearing Test – It’s A Big Hit

      March 22, 2023

      Saks Fifth Avenue Victim of Latest Clop Ransomware

      March 22, 2023
    • Realtoz
      • Our Other Sites
    • More News
    Best Website-BuildersBest Website-Builders
    Home » Experiment: Using ChatGPT to provide detailed refactoring instructions – JavaScript – SitePoint Forums
    JavaScript

    Experiment: Using ChatGPT to provide detailed refactoring instructions – JavaScript – SitePoint Forums

    websitebuildersnowBy websitebuildersnowFebruary 14, 2023No Comments3 Mins Read
    Facebook Twitter LinkedIn Telegram Pinterest Tumblr Reddit WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    I took a code example from a recent thread and thought I’d try it on ChatGPT.
    I started by providing the code and asked them to improve it.

    All the rest of this post is completely generated by ChatGPT.


    Prompt: Write refactoring instructions in Uncle Bob’s humorous style

    Indeed, Uncle Bob’s humorous style refactoring steps are:

    1. harden that code in strict mode

    It’s time to take the code through bootcamp and shape it. First, enable strict mode by adding the following line of code at the top of your script:

    "use strict";
    
    1. Always improve your code

    You should start treating variables with more respect. select and panels Good and honorable citizens that don’t need to be reassigned, so keep them constant and respect them const.

    const select = document.querySelector(".mlduo-select");
    const panels = document.querySelectorAll(".panels");
    
    1. Reduce nested if statements

    Nested if statements are starting to look like matryoshka dolls, so I need to break them down a bit. To do this, return early and avoid nested ifs.

    select.addEventListener("change", function() {
      panels.forEach(function(panel) {
        panel.style.display = "none";
      });
    
      const selectedPanel = document.getElementById(select.value);
      if (!selectedPanel) return;
    
      selectedPanel.style.display = "block";
    });
    
    1. Ditch innerHTML and embrace DOM manipulation

    you’re in the big leagues now but we don’t use innerHTML Around here. Instead, use DOM manipulation to create and modify elements. For example:

    const option = document.createElement("option");
    option.value = "value";
    option.text = "text";
    select.appendChild(option);
    
    1. Create dynamic strings using template literals

    String concatenation can get tedious, so let’s spice things up with template literals. You can easily create a dynamic string like this:

    const message = `Hello, ${name}!`;
    
    1. Use functions to make your code more modular

    Now that the code is getting a little unwieldy, it’s time to break it down into smaller, more manageable pieces. Create a few functions to handle specific tasks, like this:

    function hidePanels() {
      panels.forEach(panel => {
        panel.style.display = "none";
      });
    }
    
    function showSelectedPanel() {
      const selectedPanel = document.getElementById(select.value);
      if (!selectedPanel) return;
      selectedPanel.style.display = "block";
    }
    
    select.addEventListener("change", function() {
      hidePanels();
      showSelectedPanel();
    });
    
    1. Adopt an arrow function

    Arrow functions are new children of blocks and help make your code more concise and readable. For example:

    const double = x => x * 2;
    
    1. use querySelector Excluding that getElementById

    Start using it when you feel like an adult querySelector Excluding that getElementByIdThis makes the code more flexible and reusable. For example:

    const selectedPanel = document.querySelector(`#${select.value}`);
    
    1. accept array methods

    You’ve been avoiding it, but face music, it’s time to start using array methods like map, filterand reduce. Helps simplify code and make it easier to read. For example:

    const numbers = [1, 2, 3];
    const doubledNumbers = numbers.map(number => number * 2);
    
    1. avoid global variables

    You’re starting to grow, and with that comes the responsibility to avoid global variables. Declare the variable in the scope where you want it, like this:

    (function() {
      const
    



    Source link

    Share this:

    • Tweet
    • Email
    • Pocket
    • Mastodon
    • WhatsApp
    • Telegram
    • Share on Tumblr
    • Print
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Telegram Email
    Previous ArticleSpeed ​​up SMSF audits with customized education
    Next Article BBC India offices searched by income tax officials
    websitebuildersnow
    • Website

    Related Posts

    Move away from using (most) WebR R functions in WebR-powered apps/sites and call a single JavaScript function

    March 21, 2023

    “Smaller, Simpler, Faster” — Visual Studio Magazine

    March 21, 2023

    Burp Suite Enterprise Edition Power Tools: Unleash the Power of Command Line, Python and More | Blog

    March 21, 2023
    Add A Comment

    Leave a Reply Cancel reply

    Top Posts

    Subscribe to Updates

    Get the latest sports news from SportsSite about soccer, football and tennis.

    Advertisement
    Demo

    This website provides information about CSS and other things. Keep Supporting Us With the Latest News and we Will Provide the Best Of Our To Makes You Updated All Around The World News. Keep Sporting US.

    Facebook Twitter Instagram Pinterest YouTube
    Top Insights

    Johnson on Partygate: We avoided physical contact but passed drinks

    March 22, 2023

    Lyon 0-1 Chelsea: Blues get crucial European win at holders Lyon

    March 22, 2023

    The Future of Black Queer Characters in Comics Is Joyful

    March 22, 2023
    Get Informed

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    © 2023 bestwebsite-builders. Designed by bestwebsite-builders.
    • Home
    • About us
    • Contact us
    • DMCA
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.