Javascript clean code example, bad code vs good code, naming variables

preview_player
Показать описание
#javascript #cleancode #goodcode #badcode #namingvariables

Javascript Clean Code

JavaScript Shorts

JavaScript Arrow Functions

JavaScript | What does it mean

JavaScript | Select HTML elements

10 Seconds of JavaScript

JavaScript quick tips

JavaScript Arrays Shorts

JavaScript Arrays

JavaScript Classes Shorts

JavaScript Objects

JavaScript Fetch API TUTORIALS

An Example of JavaScript ...

JavaScript Classes

JSON

JavaScript vs CSS

100 SHORTS TO START LEARNING WEB DEVELOPMENT

🔗 JavaScript Scope

JAVASCRIPT LONGS

JavaScript functions

JAVASCRIPT FOR BEGINNERS

Javascript Challenge; first to know wins

Javascript ... Spread and ... Rest Operators

HTML SHORTS FOR BEGINNERS

Table (HTML, CSS, Bootstrap, Javascript, jQuery)

Visual Studio Code Shorts

Bootstrap Shorts

Long Tutorials HTML CSS JAVASCRIPT AND JQUERY

Jquery Shorts

HTML shorts

CSS Shorts
Рекомендации по теме
Комментарии
Автор

👉 As tinnick said, using Math.PI is better than using PI = 3.14
👉 As Ben Davies said, using yearOfBirth = 1982 is better than using dateOfBirth = 1982
👉 As Tony Lin said using comments like this // comment is better than than this //comment ( use space between word and // )
👉 As Erdi said there is an incosistent use of ' and " in this example

❗❗❗ Some resources❗❗❗


Clean-code-javascript: Variables
Use meaningful and pronounceable variable names
Bad:
const yyyymmdstr =
Good:
const currentDate =


12 Conventions for Writing Clean Code


6. Variable Naming
Camel case is the naming standard for both variables and functions, and also other identifiers. This means a name is supposed to begin with a small letter, and every first letter of the next word will be uppercase.
Both function and variable must follow the rule.

Benefits of Following Conventions
1 Clean code
2 Quality of code
3 Readability of code
4 Makes code maintenance easier




For variable names use lowerCamelCasing, and use concise, human-readable, semantic names where appropriate.



Writing Clean JavaScript

1. Variables
Use meaningful names

Names of variables should be descriptive. The rule of thumb is that most JavaScript variables are in Camel Case (camelCase).



How to Write Clean Code: Variable Names
Camel case is used for almost everything in JavaScript. Camel case is a naming standard we use for identifier names (variables and functions).

It states that a name needs to begin with a lower-case letter and then, every first letter of the next word is uppercased.



Naming Conventions

Always use the same naming convention for all your code. For example:

Variable and function names written as camelCase
Global variables written in UPPERCASE (We don't, but it's quite common)
Constants (like PI) written in UPPERCASE

camelCase:

camelCase is used by JavaScript itself, by jQuery, and other JavaScript libraries.



In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, CONSTANT_VALUES_LIKE_THIS and filenameslikethis.js.



JAVASCRIPT NAMING CONVENTIONS: VARIABLES
// bad
var firstname = 'Robin';

// bad
var first_name = 'Robin';

// bad
var FIRSTNAME = 'Robin';

// bad
var FIRST_NAME = 'Robin';

// good
var firstName = 'Robin';



👉 Source: Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship
You should name a variable using the same care with which you name a first-born child.

Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control.

oneminutecoder
Автор

That’s not good vs bad code, that’s naming convention
Part of good coding style is being consistent following a convention and being consistent with it

anthonymerle
Автор

You wouldn't need comments in your code if you named variables like that.

I wish coders wouldn't use shortcut names.

Good work.

aussieraver
Автор

const VaRiAbLeNaMe = "variable name";

rentokawaii
Автор

my own natural convention is: PascalCase for namespace/class definitions, Capitalized_Pascal_Case for methods/big functions, snake_case for regular variables, and UPPER_SNAKE_CASE for constants.

I find code the absolute easiest to read this way.

sonofspardauser
Автор

Sometimes you can't evade using underscore, today I was working with mongoose and express, it was much better for me to use destructuring then keeping with the patern than navigating through the data.

yurisoares
Автор

What's wrong with using an underscore for variables ? I find it a lot more readable than using camelcase. Javascript methods use camelcase but I don't think it means everyone should use it. Personally I don't see anything wrong with first_name as a variable name.

Mslepe_
Автор

We have a total of 3 juniors web developers including me. It doesn't matter what naming convention we use as long as it's very specific. I named a variable with 25 characters once with camelcasing and underscores and it's totally fine.

flux
Автор

If I see db the first thing I'll think is Database

yurisoares
Автор

I’d like to pull you you up on a couple of things dateOfBirth should be yearOfBirth and your basically saying that const should only be global level constants but if you remember arrays and objects are pointers so why would you make them let. So I always use the basis that everything is const unless I absolutely need mutability

Ben_Davies
Автор

Depends on the convention being used. The only thing that must be avoided are confusing variable name.

carlomoleka
Автор

const user = { firstName: “adam”, lastName: “bothon”, age: 18 }

gfmopgo
Автор

Clean code is not about camelCase or snake case, is about using good naming

yuzuru
Автор

Constants in JS are not true constants, so they should be named like others normal variables. For example in my opinion in camelCase

symoleon
Автор

const = SCREAMING_CASE
let = camelCase

juan
Автор

instead of "dateOfBirth" id do "birthDate" which is just as good i just like having shorter names

HyProDuxyMusicHandle
Автор

why is there an incosistent use of ' and "?
and incostistent use of or lack thereof spaces before and after =?

IveGotBeef
Автор

It just a style, style can be varied among companies, projects, groups, etc. Also, the purpose of camelCase is avoid error of using _ character that programming lang did not support in the past. But now nearly all lang support this. Good or bad is just a shallow thinking.

gago
Автор

Nothing bad in that it's upto you how you should name your variables, as long as we could understand what it does Nothing wrong in it.

fluffybunny
Автор

No one coding in JavaScript has any business telling me anything about how to code…

chswin
join shbcf.ru