Skip to content

Learn with Jquery how to use the vanilla javascript #293

Closed

Description

@Gurigraphics

Learn with Jquery how to use the vanilla javascript

Bad:

var a = document.getElementById("a")
var b = document.getElementById("b")
var c = document.getElementsByClassName("c")
var d = document.getElementsByTagName("body")[0]

Good:

const $ = (v) => document.querySelector(v)

let a = $("#a")
let b = $("#b")
let c = $(".c")
let d = $("body")

Activity

  1. ryanmcdermott commented on Jan 22, 2020

    @ryanmcdermott
    Owner

    Hey there! I only want to add subsections that have a corresponding one in the book Clean Code. I don't think this one does.

  2. Gurigraphics commented on Jan 22, 2020

    @Gurigraphics
    Author

    Is the goal to write cleaner code, or philosophize about the book?

  3. ryanmcdermott commented on Jan 22, 2020

    @ryanmcdermott
    Owner

    The goal isn't to philosophize, it's "Software engineering principles, from Robert C. Martin's book Clean Code, adapted for JavaScript." I'll make this more clear in a CONTRIBUTING.md document!

  4. Gurigraphics commented on Jan 22, 2020

    @Gurigraphics
    Author

    My point is that:

    The "document.getElementById" repetition, it's "old habits of those who read the book, and has not mastered any principle. Because not use where could use to make the code cleaner.

    But I agree that, my point is really another question, that does not make this principles more clear.

    Thinking well, I don't always agree with Robert C. Martin's
    Because "avoiding" and "preferring" is not "always avoiding" and "always preferring".


    Avoid Mental Mapping. Explicit is better than implicit. (By Robert C. Martin)

    bad
    locations.forEach(l => {
    good
    locations.forEach(location => {

    Avoid Sesquipedalianism when Less is More. (Gurigraphics)

    bad
    sesquipedalianisms.forEach(sesquipedalianism => {
    good
    items.forEach(i=> {

    The more you need use a name, the lower the weight of the name that you need to carry in your memory. It is because mathematicians prefer "+", "-", "/", "x", "y" that "quantumSpinLeft" dividedBy "quantumSpinRight".


    "You should not have to read the body of a function to know what it does. Functions in small scopes should have long and precise names".( By Robert C. Martin)

    bad
    parseColumnHeader()
    good
    gatherFunctionsAndVariablesFromColumnHeader()

    If "Familiarity admits brevity"( Golang principle )

    && "Convention over configuration"(Ruby on Rails principle)

    Then => Prefer Mnemonics Conventions that Mental Mapping and Sesquipedalianism

    bad
    document.getElementById
    document.getElementsByClassName
    good
    $("#")
    $(".")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions