Skip to content

Commit e63610e

Browse files
sbielenicajakubdrozdek
authored andcommitted
Translate 'Comments' page (#26)
1 parent 84bbbca commit e63610e

File tree

1 file changed

+73
-76
lines changed

1 file changed

+73
-76
lines changed

Diff for: 1-js/03-code-quality/03-comments/article.md

+73-76
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,53 @@
1-
# Comments
1+
# Komentarze
22

3-
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 `/* ... */`.
45

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.
67

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.
89

9-
## Bad comments
10+
## Złe komentarze
1011

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:
1214

1315
```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;
1921
```
2022

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.
2224

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".
2426

25-
### Recipe: factor out functions
27+
### Przepis: wydziel funkcję
2628

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:
2830

2931
```js
3032
function showPrimes(n) {
31-
nextPrime:
33+
nextPrime:
3234
for (let i = 2; i < n; i++) {
33-
34-
*!*
35-
// check if i is a prime number
35+
// sprawdź czy i jest liczbą pierwszą
3636
for (let j = 2; j < i; j++) {
3737
if (i % j == 0) continue nextPrime;
3838
}
39-
*/!*
40-
4139
alert(i);
4240
}
4341
}
4442
```
4543

46-
The better variant, with a factored out function `isPrime`:
47-
44+
Lepszy wariant z wydzieloną funkcją `isPrime`:
4845

4946
```js
5047
function showPrimes(n) {
51-
5248
for (let i = 2; i < n; i++) {
53-
*!*if (!isPrime(i)) continue;*/!*
54-
55-
alert(i);
49+
if (!isPrime(i)) continue;
50+
alert(i);
5651
}
5752
}
5853

@@ -65,22 +60,22 @@ function isPrime(n) {
6560
}
6661
```
6762

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ę".
6964

70-
### Recipe: create functions
65+
### Przepis: stwórz funkcje
7166

72-
And if we have a long "code sheet" like this:
67+
Jeśli mamy długi fragment kodu, tak jak tu:
7368

7469
```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++) {
7772
let drop = getWhiskey();
7873
smell(drop);
7974
add(drop, glass);
8075
}
8176

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++) {
8479
let tomato = getTomato();
8580
examine(tomato);
8681
let juice = press(tomato);
@@ -90,91 +85,93 @@ for(let t = 0; t < 3; t++) {
9085
// ...
9186
```
9287

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:
9489

9590
```js
9691
addWhiskey(glass);
9792
addJuice(glass);
9893

9994
function addWhiskey(container) {
100-
for(let i = 0; i < 10; i++) {
95+
for (let i = 0; i < 10; i++) {
10196
let drop = getWhiskey();
10297
//...
10398
}
10499
}
105100

106101
function addJuice(container) {
107-
for(let t = 0; t < 3; t++) {
102+
for (let t = 0; t < 3; t++) {
108103
let tomato = getTomato();
109104
//...
110105
}
111106
}
112107
```
113108

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.
115110

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ę.
117112

118-
## Good comments
113+
## Dobre komentarze
119114

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?
121116

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.
124119

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:
127124

128-
For instance:
129125
```js
130126
/**
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+
*/
137133
function pow(x, n) {
138134
...
139135
}
140136
```
141137

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.
143141

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/>.
145143

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.
147146

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.
150148

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:
152150

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.
157154

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.
159156

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.
162159

163-
## Summary
160+
## Podsumowanie
164161

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.
166163

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ć.
168165

169-
**Comment this:**
166+
**Komentuj:**
170167

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.
174171

175-
**Avoid comments:**
172+
**Unikaj komentarzy:**
176173

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.
179176

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

Comments
 (0)