<!-- плохо -->
<div id="main">
<div class="article">
<div class="header">
<h1>Blog post</h1>
<p>Published: <span>21st Feb, 2015</span></p>
</div>
<p>…</p>
</div>
</div>
<!-- хорошо -->
<main>
<article>
<header>
<h1>Blog post</h1>
<p>Published: <time datetime="2015-02-21">21st Feb, 2015</time></p>
</header>
<p>…</p>
</article>
</main>
<!-- плохо -->
<h1>
<figure>
<img alt=Company src=logo.png>
</figure>
</h1>
<!-- хорошо -->
<h1>
<img alt=Company src=logo.png>
</h1>
<!-- плохо -->
<!doctype html>
<html lang=en>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8" />
<title>Contact</title>
<link rel=stylesheet href=style.css type=text/css />
</head>
<body>
<h1>Contact me</h1>
<label>
Email address:
<input type=email placeholder=you@email.com required=required />
</label>
<script src=main.js type=text/javascript></script>
</body>
</html>
<!-- хорошо (но не семантично) -->
<!doctype html>
<html lang=en>
<meta charset=utf-8>
<title>Contact</title>
<link rel=stylesheet href=style.css>
<h1>Contact me</h1>
<label>
Email address:
<input type=email placeholder=you@email.com required>
</label>
<script src=main.js></script>
</html>
<!-- плохо -->
<h1><img alt="Logo" src="logo.png"></h1>
<!-- хорошо -->
<h1><img alt="My Company, Inc." src="logo.png"></h1>
<!-- плохо -->
<!doctype html>
<title>Привет, мир.</title>
<!-- хорошо -->
<!doctype html>
<html lang=ru>
<meta charset=utf-8>
<title>Привет, мир.</title>
</html>
<!-- плохо -->
<!doctype html>
<meta charset=utf-8>
<script src=analytics.js></script>
<title>Hello, world.</title>
<p>...</p>
<!-- хорошо -->
<!doctype html>
<meta charset=utf-8>
<title>Hello, world.</title>
<p>...</p>
<script src=analytics.js></script>
/* плохо */
div {
color: red
}
/* хорошо */
div {
color: red;
}
/* плохо */
div {
width: 100%;
padding: 10px;
box-sizing: border-box;
}
/* хорошо */
div {
padding: 10px;
}
/* плохо */
img {
display: block;
}
/* хорошо */
img {
vertical-align: middle;
}
/* плохо */
div {
width: 100px;
position: absolute;
right: 0;
}
/* хорошо */
div {
width: 100px;
margin-left: auto;
}
display: block;
display: flex;
position: relative;
position: sticky;
position: absolute;
position: fixed;
/* плохо */
div:first-of-type :last-child > p ~ *
/* хорошо */
div:first-of-type .info
/* плохо */
img[src$=svg], ul > li:first-child {
opacity: 0;
}
/* хорошо */
[src$=svg], ul > :first-child {
opacity: 0;
}
/* плохо */
.bar {
color: green !important;
}
.foo {
color: red;
}
/* хорошо */
.foo {
color: red;
}
.foo.bar {
color: green;
}
/* плохо */
li {
visibility: hidden;
}
li:first-child {
visibility: visible;
}
/* хорошо */
li + li {
visibility: hidden;
}
/* плохо */
div h1, div p {
text-shadow: 0 1px 0 #fff;
}
/* хорошо */
div {
text-shadow: 0 1px 0 #fff;
}
/* плохо */
div {
transition: all 1s;
top: 50%;
margin-top: -10px;
padding-top: 5px;
padding-right: 10px;
padding-bottom: 20px;
padding-left: 10px;
}
/* хорошо */
div {
transition: 1s;
top: calc(50% - 10px);
padding: 5px 10px 20px;
}
/* плохо */
:nth-child(2n + 1) {
transform: rotate(360deg);
}
/* хорошо */
:nth-child(odd) {
transform: rotate(1turn);
}
/* плохо */
div {
transform: scale(2);
-webkit-transform: scale(2);
-moz-transform: scale(2);
-ms-transform: scale(2);
transition: 1s;
-webkit-transition: 1s;
-moz-transition: 1s;
-ms-transition: 1s;
}
/* хорошо */
div {
-webkit-transform: scale(2);
transform: scale(2);
transition: 1s;
}
/* плохо */
div:hover {
animation: move 1s forwards;
}
@keyframes move {
100% {
margin-left: 100px;
}
}
/* хорошо */
div:hover {
transition: 1s;
transform: translateX(100px);
}
/* плохо */
div {
margin: 0px;
font-size: .9em;
line-height: 22px;
transition: 500ms;
}
/* хорошо */
div {
margin: 0;
font-size: .9rem;
line-height: 1.5;
transition: .5s;
}
/* плохо */
div {
color: hsl(103, 54%, 43%);
}
/* хорошо */
div {
color: #5a3;
}
/* плохо */
div::before {
content: url(white-circle.svg);
}
/* хорошо */
div::before {
content: "";
display: block;
width: 20px;
height: 20px;
border-radius: 50%;
background: #fff;
}
/* плохо */
div {
// position: relative;
transform: translateZ(0);
}
/* хорошо */
div {
/* position: relative; */
will-change: transform;
}
/* плохо (хотя и намного быстрее) */
const arr = [1, 2, 3, 4];
const len = arr.length;
var i = -1;
var result = [];
while (++i < len) {
var n = arr[i];
if (n % 2 > 0) continue;
result.push(n * n);
}
/* хорошо */
const arr = [1, 2, 3, 4];
const isEven = n => n % 2 == 0; // инициализация анонимной стрелочной функции
const square = n => n * n; // синтаксис: (param1, param2, …, paramN) => expression
const result = arr.filter(isEven).map(square);
/* плохо */
const merge = (target, ...sources) => Object.assign(target, ...sources);
merge({ foo: "foo" }, { bar: "bar" }); // => { foo: "foo", bar: "bar" }
/* хорошо */
const merge = (...sources) => Object.assign({}, ...sources);
merge({ foo: "foo" }, { bar: "bar" }); // => { foo: "foo", bar: "bar" }
/* плохо */
const toArray = obj => [].slice.call(obj);
/* хорошо */
const toArray = (() =>
Array.from ? Array.from : obj => [].slice.call(obj)
)();
/* плохо */
if (x === undefined || x === null) { /* ... */ }
/* хорошо */
if (x == undefined) { /* ... */ }
/* плохо */
const sum = arr => {
var sum = 0;
var i = -1;
for (;arr[++i];) {
sum += arr[i];
}
return sum;
};
sum([1, 2, 3]); // => 6
/* хорошо */
const sum = arr => arr.reduce((x, y) => x + y);
sum([1, 2, 3]); // => 6
/* плохо */
const createDivs = howMany => {
while (howMany--) {
document.body.insertAdjacentHTML("beforeend", "<div></div>");
}
};
createDivs(5);
/* плохо */
const createDivs = howMany =>
[...Array(howMany)].forEach(() =>
document.body.insertAdjacentHTML("beforeend", "<div></div>")
);
createDivs(5);
/* хорошо */
const createDivs = howMany => {
if (!howMany) return;
document.body.insertAdjacentHTML("beforeend", "<div></div>");
return createDivs(howMany - 1);
};
createDivs(5);
/* плохо */
const sortNumbers = () =>
Array.prototype.slice.call(arguments).sort();
/* хорошо */
const sortNumbers = (...numbers) => numbers.sort();
const greet = (first, last) => `Hi ${first} ${last}`;
const person = ["John", "Doe"];
/* плохо */
greet.apply(null, person);
/* хорошо */
greet(...person);
/* плохо */
["foo", "bar"].forEach(func.bind(this));
/* хорошо */
["foo", "bar"].forEach(func, this);
/* плохо */
const person = {
first: "John",
last: "Doe",
greet() {
const full = function() {
return `${this.first} ${this.last}`;
}.bind(this);
return `Hello ${full()}`;
}
}
/* хорошо */
const person = {
first: "John",
last: "Doe",
greet() {
const full = () => `${this.first} ${this.last}`;
return `Hello ${full()}`;
}
}
/* плохо */
const result = [1, 2, 3].map(num => String(num)); // => ["1", "2", "3"]
/* хорошо */
const result = [1, 2, 3].map(String); // Приведение всех элементов к типу string
const plus1 = a => a + 1;
const mult2 = a => a * 2;
/* плохо */
mult2(plus1(5)); // => 12
/* хорошо */
const pipeline = (...funcs) => val => funcs.reduce((a, b) => b(a), val);
const addThenMult = pipeline(plus1, mult2);
addThenMult(5); // => 12
/* плохо */
const contains = (arr, value) =>
Array.prototype.includes
? arr.includes(value)
: arr.some(el => el === value);
contains(["foo", "bar"], "baz"); // => false
/* хорошо */
const contains = (() =>
Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
)();
contains(["foo", "bar"], "baz"); // => false
/* плохо */
var me = new Map();
me.set("name", "Ben").set("country", "Belgium");
/* хорошо */
const me = new Map();
me.set("name", "Ben").set("country", "Belgium");
/* плохо */
var grade, result = 50;
if (result < 50)
grade = "bad";
else if (result < 90)
grade = "good";
else
grade = "excellent";
// grade = "good";
/* хорошо */
let result = 50;
const grade = (() => {
if (result < 50)
return "bad";
if (result < 90)
return "good";
return "excellent";
})(); // grade = "good";
const shared = { foo: "foo" };
const obj = Object.create(shared, {
bar: {
value: "bar",
enumerable: true
}
});
/* плохо */
for (var prop in obj) {
if (obj.hasOwnProperty(prop))
console.log(prop); // => bar
}
/* хорошо */
Object.keys(obj).forEach(prop => console.log(prop));
/* плохо */
const me = {
name: "Ben",
age: 30
};
var meSize = Object.keys(me).length;
meSize; // => 2
me.country = "Belgium"; // можно
meSize++;
meSize; // => 3
/* хорошо */
const me = new Map();
me.set("name", "Ben");
me.set("age", 30);
me.size; // => 2
me.set("country", "Belgium");
me.size; // => 3
/* плохо */
const sum = a => b => a + b;
sum(5)(3); // => 8
/* хорошо */
const sum = (a, b) => a + b;
sum(5, 3); // => 8
/* плохо */
foo || doSomething();
/* плохо */
void function() { /* IIFE */ }();
/* плохо */
const n = ~~3.14;
/* хорошо */
if (!foo) doSomething();
/* хорошо */
(function() { /* IIFE */ }());
/* хорошо */
const n = Math.floor(3.14);
/* плохо */
let last = arr[arr.length - 1];
/* плохо */
const product = (a, b) => a * b;
const triple = n => n * 3;
/* хорошо */
const first = arr => arr[0]; // => first(arr);
const last = arr => first(arr.slice(-1)); // => last(arr);
/* хорошо */
const product = (a, b) => a * b;
const triple = product.bind(null, 3);
/* плохо */
var _ = require("underscore");
_.compact(["foo", 0]));
_.unique(["foo", "foo"]);
_.union(["foo"], ["bar"], ["foo"]);
/* хорошо */
const compact = arr => arr.filter(el => el);
const unique = arr => [...Set(arr)];
const union = (...arr) => unique([].concat(...arr));
compact(["foo", 0]);
unique(["foo", "foo"]);
union(["foo"], ["bar"], ["foo"]);
К сожалению, не доступен сервер mySQL