You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we know from the chapter <info:structure>, comments can be single-line: starting with `//` and multiline: `/* ... */`.
3
+
Jak wiemy z rozdziału pt. "<info:structure>", komentarze mogą być jednoliniowe, gdy
4
+
rozpoczniemy linię znakami `//`, lub wieloliniowe, jeśli użyjemy `/* ... */`.
4
5
5
-
We normally use them to describe how and why the code works.
6
+
Zwykle używamy ich do opisania, jak i dlaczego kod działa.
6
7
7
-
At first sight, commenting might be obvious, but novices in programming often use them wrongly.
8
+
Na pierwszy rzut oka koncepcja komentowania może być oczywista, jednak początkujący programiści często używają komentarzy niepoprawnie.
8
9
9
-
## Bad comments
10
+
## Złe komentarze
10
11
11
-
Novices tend to use comments to explain "what is going on in the code". Like this:
12
+
Nowicjusze mają skłonność do używania komentarzy, aby wyjaśnić "co się dzieje w programie".
13
+
Przykładowo:
12
14
13
15
```js
14
-
//This code will do this thing (...) and that thing (...)
15
-
// ...and who knows what else...
16
-
very;
17
-
complex;
18
-
code;
16
+
//Ten kod zrobi to ( ... ) oraz to ( ...)
17
+
// ...i kto wie co jeszcze...
18
+
bardzo;
19
+
skomplikowany;
20
+
kod;
19
21
```
20
22
21
-
But in good code, the amount of such "explanatory" comments should be minimal. Seriously, the code should be easy to understand without them.
23
+
Jednak w dobrym kodzie ilość takich "wyjaśniających" komentarzy powinna być minimalna. Poważnie, kod powinien być łatwy do zrozumienia bez nich.
22
24
23
-
There's a great rule about that: "if the code is so unclear that it requires a comment, then maybe it should be rewritten instead".
25
+
Jest pewna świetna zasada, która tego dotyczy: "jeśli kod jest tak niezrozumiały, że wymagane są komentarze, to możliwe, że zamiast komentowania powinien być napisany od nowa".
24
26
25
-
### Recipe: factor out functions
27
+
### Przepis: wydziel funkcję
26
28
27
-
Sometimes it's beneficial to replace a code piece with a function, like here:
29
+
Czasami korzystnie jest zastąpić kawałek kodu funkcją, tak jak w tym przypadku:
28
30
29
31
```js
30
32
functionshowPrimes(n) {
31
-
nextPrime:
33
+
nextPrime:
32
34
for (let i =2; i < n; i++) {
33
-
34
-
*!*
35
-
// check if i is a prime number
35
+
// sprawdź czy i jest liczbą pierwszą
36
36
for (let j =2; j < i; j++) {
37
37
if (i % j ==0) continue nextPrime;
38
38
}
39
-
*/!*
40
-
41
39
alert(i);
42
40
}
43
41
}
44
42
```
45
43
46
-
The better variant, with a factored out function `isPrime`:
47
-
44
+
Lepszy wariant z wydzieloną funkcją `isPrime`:
48
45
49
46
```js
50
47
functionshowPrimes(n) {
51
-
52
48
for (let i =2; i < n; i++) {
53
-
*!*if (!isPrime(i)) continue;*/!*
54
-
55
-
alert(i);
49
+
if (!isPrime(i)) continue;
50
+
alert(i);
56
51
}
57
52
}
58
53
@@ -65,22 +60,22 @@ function isPrime(n) {
65
60
}
66
61
```
67
62
68
-
Now we can understand the code easily. The function itself becomes the comment. Such code is called *self-descriptive*.
63
+
Teraz jesteśmy w stanie z łatwością zrozumieć ten kod. Funkcja sama w sobie staje się komentarzem. Taki kod nazywany jest "samoopisującym się".
69
64
70
-
### Recipe: create functions
65
+
### Przepis: stwórz funkcje
71
66
72
-
And if we have a long "code sheet" like this:
67
+
Jeśli mamy długi fragment kodu, tak jak tu:
73
68
74
69
```js
75
-
//here we add whiskey
76
-
for(let i =0; i <10; i++) {
70
+
//tutaj dodajemy whiskey
71
+
for(let i =0; i <10; i++) {
77
72
let drop =getWhiskey();
78
73
smell(drop);
79
74
add(drop, glass);
80
75
}
81
76
82
-
//here we add juice
83
-
for(let t =0; t <3; t++) {
77
+
//tutaj dodajemy sok
78
+
for(let t =0; t <3; t++) {
84
79
let tomato =getTomato();
85
80
examine(tomato);
86
81
let juice =press(tomato);
@@ -90,91 +85,93 @@ for(let t = 0; t < 3; t++) {
90
85
// ...
91
86
```
92
87
93
-
Then it might be a better variant to refactor it into functions like:
88
+
Wtedy lepszym rozwiązaniem może być refaktoryzacja na takie funkcje:
94
89
95
90
```js
96
91
addWhiskey(glass);
97
92
addJuice(glass);
98
93
99
94
functionaddWhiskey(container) {
100
-
for(let i =0; i <10; i++) {
95
+
for(let i =0; i <10; i++) {
101
96
let drop =getWhiskey();
102
97
//...
103
98
}
104
99
}
105
100
106
101
functionaddJuice(container) {
107
-
for(let t =0; t <3; t++) {
102
+
for(let t =0; t <3; t++) {
108
103
let tomato =getTomato();
109
104
//...
110
105
}
111
106
}
112
107
```
113
108
114
-
Once again, functions themselves tell what's going on. There's nothing to comment. And also the code structure is better when split. It's clear what every function does, what it takes and what it returns.
109
+
Po raz kolejny, funkcje same opisują, co się dzieje. Nie ma tu co komentować. Również struktura kodu jest lepsza, gdy jest on podzielony. Jest jasne, co robi każda funkcja, co przyjmuje i co zwraca.
115
110
116
-
In reality, we can't totally avoid "explanatory" comments. There are complex algorithms. And there are smart "tweaks" for purposes of optimization. But generally we should try to keep the code simple and self-descriptive.
111
+
W rzeczywistości nie zawsze jesteśmy w stanie uniknąć "wyjaśniających" komentarzy. Mamy czasami do czynienia ze złożonymi algorytmami. Zdarzają się sprytne "poprawki" na rzecz optymalizacji. Jednak ogólnie powinniśmy starać się, aby kod był prosty i samoopisujący się.
117
112
118
-
## Good comments
113
+
## Dobre komentarze
119
114
120
-
So, explanatory comments are usually bad. Which comments are good?
115
+
Ustaliliśmy już, że wyjaśniające komentarze są przeważnie złe. W takim razie które komentarze są dobre?
121
116
122
-
Describe the architecture
123
-
: Provide a high-level overview of components, how they interact, what's the control flow in various situations... In short -- the bird's eye view of the code. There's a special language [UML](http://wikipedia.org/wiki/Unified_Modeling_Language)to build high-level architecture diagrams explaining the code. Definitely worth studying.
117
+
Opisz architekturę
118
+
: Dostarcz wysokopoziomowy przegląd komponentów, opisz, jak ze sobą współdziałają, jaki jest przepływ danych w różnych sytuacjach... W skrócie -- przedstaw spojrzenie na kod z lotu ptaka. Istnieje specjalny język [UML](http://wikipedia.org/wiki/Unified_Modeling_Language)służący do budowania wysokopoziomowych diagramów, które opisują architekturę i wyjaśniają kod. Zdecydowanie warto zgłębić ten temat.
124
119
125
-
Document function parameters and usage
126
-
: There's a special syntax [JSDoc](http://en.wikipedia.org/wiki/JSDoc) to document a function: usage, parameters, returned value.
120
+
Udokumentuj parametry oraz użycie funkcji
121
+
: Istnieje specjalna składnia [JSDoc](http://en.wikipedia.org/wiki/JSDoc) pozwalająca na dokumentowanie funkcji: sposób jej użycia, oczekiwane parametry i zwracaną wartość.
122
+
123
+
Na przykład:
127
124
128
-
For instance:
129
125
```js
130
126
/**
131
-
* Returns x raised to the n-th power.
132
-
*
133
-
* @param {number} x The number to raise.
134
-
* @param {number} n The power, must be a natural number.
135
-
* @return {number} x raised to the n-th power.
136
-
*/
127
+
* Zwraca x podniesiony do n-tej potęgi.
128
+
*
129
+
* @param {number} x Liczba do potęgowania.
130
+
* @param {number} n Wykładnik potęgi; musi być liczbą naturalną.
131
+
* @return {number} x podniesiony do n-tej potęgi.
132
+
*/
137
133
function pow(x, n) {
138
134
...
139
135
}
140
136
```
141
137
142
-
Such comments allow us to understand the purpose of the function and use it the right way without looking in its code.
138
+
Takie komentarze pozwalają nam poznać przeznaczenie funkcji i używać jej w poprawny sposób bez zerkania do jej treści.
139
+
140
+
Tak na marginesie, wiele edytorów, takich jak [WebStorm](https://www.jetbrains.com/webstorm/), jest w stanie dobrze je zrozumieć oraz używać ich do automatycznego uzupełniania i sprawdzania kodu.
143
141
144
-
By the way, many editors like [WebStorm](https://www.jetbrains.com/webstorm/) can understand them as well and use them to provide autocomplete and some automatic code-checking.
142
+
Istnieją również takie narzędzia jak [JSDoc 3](https://github.com/jsdoc3/jsdoc), które są w stanie generować dokumentację HTML z tych komentarzy. Możesz dowiedzieć się więcej na ten temat pod tym linkiem: <http://usejsdoc.org/>.
145
143
146
-
Also, there are tools like [JSDoc 3](https://github.com/jsdoc3/jsdoc) that can generate HTML-documentation from the comments. You can read more information about JSDoc at <http://usejsdoc.org/>.
144
+
Dlaczego zadanie jest rozwiązane w taki sposób?
145
+
: Co zostało napisane jest ważne. Jednakże to, czego _nie_ napisano, może być jeszcze ważniejsze w zrozumieniu, o co chodzi w kodzie. Dlaczego zadanie zostało rozwiązane dokładnie w taki sposób? Kod nie odpowie na to pytanie.
147
146
148
-
Why is the task solved this way?
149
-
: What's written is important. But what's *not* written may be even more important to understand what's going on. Why is the task solved exactly this way? The code gives no answer.
147
+
Jeżeli jest wiele sposobów na rozwiązanie zadania, dlaczego został wybrany właśnie ten sposób? Zwłaszcza gdy nie jest najbardziej oczywisty.
150
148
151
-
If there are many ways to solve the task, why this one? Especially when it's not the most obvious one.
149
+
Bez takich komentarzy następująca sytuacja staje się możliwa:
152
150
153
-
Without such comments the following situation is possible:
154
-
1. You (or your colleague) open the code written some time ago, and see that it's "suboptimal".
155
-
2. You think: "How stupid I was then, and how much smarter I'm now", and rewrite using the "more obvious and correct" variant.
156
-
3. ...The urge to rewrite was good. But in the process you see that the "more obvious" solution is actually lacking. You even dimly remember why, because you already tried it long ago. You revert to the correct variant, but the time was wasted.
151
+
1. Ty (bądź twój współpracownik) otwiera kod napisany jakiś czas temu i widzi, że jest nieoptymalny.
152
+
2. Myślisz sobie: *Jaki byłem wtedy głupi i o ile jestem teraz mądrzejszy* i przepisujesz kod na "bardziej oczywisty i poprawny" wariant.
153
+
3. ... Chęć przepisania była w porządku. Jednak w trakcie tego procesu zauważasz, że "bardziej oczywiste" rozwiązanie nie jest idealne. Nawet mgliście pamiętasz dlaczego, ponieważ już raz przyszło ci spróbować tego rozwiązania dawno temu. Powracasz do poprawnego wariantu, ale poświęcony czas został już bezpowrotnie stracony.
157
154
158
-
Comments that explain the solution are very important. They help to continue development the right way.
155
+
Komentarze opisujące rozwiązanie są bardzo ważne. Pomagają kontynuować proces wytwarzania oprogramowania w poprawny sposób.
159
156
160
-
Any subtle features of the code? Where they are used?
161
-
: If the code has anything subtle and counter-intuitive, it's definitely worth commenting.
157
+
Jakieś nieoczywistości w kodzie? Jeśli tak, to gdzie są?
158
+
: Jeśli kod ma jakieś aspekty, które są subtelne lub sprzeczne z intuicją, zdecydowanie warto zawrzeć to w komentarzu.
162
159
163
-
## Summary
160
+
## Podsumowanie
164
161
165
-
An important sign of a good developer is comments: their presence and even their absence.
162
+
Ważnym znakiem rozpoznawczym dobrego programisty są komentarze: ich obecność, ale także ich brak.
166
163
167
-
Good comments allow us to maintain the code well, come back to it after a delay and use it more effectively.
164
+
Dobre komentarze pozwalają nam lepiej utrzymywać kod, wracać do niego po jakimś czasie i efektywnie go używać.
168
165
169
-
**Comment this:**
166
+
**Komentuj:**
170
167
171
-
-Overall architecture, high-level view.
172
-
-Function usage.
173
-
-Important solutions, especially when not immediately obvious.
168
+
-Ogólną architekturę, spojrzenie na kod z lotu ptaka.
169
+
-Sposób użycia funkcji.
170
+
-Istotne rozwiązania, zwłaszcza gdy nie są od razu oczywiste.
174
171
175
-
**Avoid comments:**
172
+
**Unikaj komentarzy:**
176
173
177
-
-That tell "how code works" and "what it does".
178
-
-Put them in only if it's impossible to make the code so simple and self-descriptive that it doesn't require them.
174
+
-Które mówią "jak kod działa" i "co robi".
175
+
-Dodawaj je tylko wtedy, gdy niemożliwe jest napisanie kodu tak prostego i samoopisującego się, że nie potrzebuje takich komentarzy.
179
176
180
-
Comments are also used for auto-documenting tools like JSDoc3: they read them and generate HTML-docs (or docs in another format).
177
+
Komentarze są również używane do narzędzi automatycznie generujących dokumentację takich jak JSDoc3. Narzędzia te czytają komentarze i generują dokumentację w formacie HTML (lub innym).
0 commit comments