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
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/03-comments/article.md
+50-48Lines changed: 50 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
-
# Comments
1
+
# التعليقات
2
2
3
-
As we know from the chapter <info:structure>, comments can be single-line: starting with`//`and multiline: `/* ... */`.
3
+
كما نعلم من الفصل <info:structure> ، يمكن أن تكون التعليقات سطر واحد: بدءًا من`//`و multiline: `/* ... */`.
4
4
5
-
We normally use them to describe how and why the code works.
5
+
نستخدمها عادةً لوصف كيف ولماذا يعمل الرمز.
6
6
7
-
At first sight, commenting might be obvious, but novices in programming often use them wrongly.
7
+
للوهلة الأولى ، قد يكون التعليق واضحًا ، لكن المبتدئين في البرمجة غالبًا ما يستخدمونها بشكل خاطئ.
8
8
9
-
## Bad comments
9
+
## التعليقات السيئة
10
10
11
-
Novices tend to use comments to explain "what is going on in the code". Like this:
11
+
يميل المبتدئون إلى استخدام التعليقات لشرح "ما يجري في الكود". مثله:
12
12
13
13
```js
14
14
// This code will do this thing (...) and that thing (...)
@@ -18,13 +18,13 @@ complex;
18
18
code;
19
19
```
20
20
21
-
But in good code, the amount of such "explanatory" comments should be minimal. Seriously, the code should be easy to understand without them.
21
+
ولكن في مدونة جيدة ، يجب أن تكون كمية هذه التعليقات "التفسيرية" ضئيلة. على محمل الجد ، يجب أن يكون الرمز سهل الفهم بدونها.
22
22
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".
23
+
هناك قاعدة رائعة حول ذلك: "إذا كان الرمز غير واضح إلى حد أنه يتطلب تعليقًا ، فربما يجب إعادة كتابته بدلاً من ذلك".
24
24
25
-
### Recipe: factor out functions
25
+
### الوصفة: قم بعمل إعادة اعتبار للدوال
26
26
27
-
Sometimes it's beneficial to replace a code piece with a function, like here:
27
+
في بعض الأحيان يكون من المفيد استبدال قطعة التعليمات البرمجية بدالة ، كما يلي:
28
28
29
29
```js
30
30
functionshowPrimes(n) {
@@ -43,7 +43,8 @@ function showPrimes(n) {
43
43
}
44
44
```
45
45
46
-
The better variant, with a factored out function `isPrime`:
46
+
البديل الأفضل ، مع دالة محسوبة
47
+
`isPrime`:
47
48
48
49
49
50
```js
@@ -65,11 +66,11 @@ function isPrime(n) {
65
66
}
66
67
```
67
68
68
-
Now we can understand the code easily. The function itself becomes the comment. Such code is called *self-descriptive*.
69
+
الآن يمكننا فهم الرمز بسهولة. تصبح الوظيفة نفسها التعليق. يسمى هذا الرمز * وصفي ذاتي *.
69
70
70
-
### Recipe: create functions
71
+
### الوصفة: إنشاء وظائف
71
72
72
-
And if we have a long "code sheet" like this:
73
+
وإذا كان لدينا "صفحة تعليمات برمجية" طويلة مثل هذا:
73
74
74
75
```js
75
76
// here we add whiskey
@@ -90,7 +91,7 @@ for(let t = 0; t < 3; t++) {
90
91
// ...
91
92
```
92
93
93
-
Then it might be a better variant to refactor it into functions like:
94
+
بعد ذلك ، قد يكون من الأفضل تغييرها إلى دوال مثل:
94
95
95
96
```js
96
97
addWhiskey(glass);
@@ -111,21 +112,22 @@ function addJuice(container) {
111
112
}
112
113
```
113
114
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.
115
+
مرة أخرى ، تخبر الوظائف نفسها عما يحدث. لا يوجد أي تعليق. وكذلك بنية الكود أفضل عند التقسيم. من الواضح ما تقوم به كل وظيفة ، وما تحتاجه وما تعيده.
115
116
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.
117
+
في الواقع ، لا يمكننا تجنب التعليقات "التفسيرية" تمامًا. هناك خوارزميات معقدة. وهناك "تعديلات" ذكية لأغراض التحسين. ولكن بشكل عام يجب أن نحاول الحفاظ على الكود بسيطًا وصفيًا ذاتيًا.
117
118
118
-
## Good comments
119
+
## تعليقات جيدة
119
120
120
-
So, explanatory comments are usually bad. Which comments are good?
121
+
لذا ، التعليقات التوضيحية عادة ما تكون سيئة. ما هي التعليقات الجيدة؟
121
122
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.
123
+
وصف العمارة
124
+
: تقديم نظرة عامة عالية المستوى على المكونات ، وكيفية تفاعلها ، وما هو تدفق التحكم في المواقف المختلفة ... باختصار - رؤية عين الطائر للرمز. هناك لغة خاصة [UML](http://wikipedia.org/wiki/Unified_Modeling_Language) لإنشاء مخططات معمارية عالية المستوى تشرح الكود. بالتأكيد تستحق الدراسة.
124
125
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.
126
+
معلمات وظيفة الوثيقة واستخدامها
127
+
: هناك بنية خاصة [JSDoc] (http://en.wikipedia.org/wiki/JSDoc) لتوثيق دالة: الاستخدام ، المعلمات ، القيمة المرتجعة.
128
+
129
+
على سبيل المثال:
127
130
128
-
For instance:
129
131
```js
130
132
/**
131
133
* Returns x raised to the n-th power.
@@ -139,42 +141,42 @@ function pow(x, n) {
139
141
}
140
142
```
141
143
142
-
Such comments allow us to understand the purpose of the function and use it the right way without looking in its code.
144
+
تسمح لنا هذه التعليقات بفهم الغرض من الوظيفة واستخدامها بالطريقة الصحيحة دون النظر في التعليمات البرمجية الخاصة بها.
143
145
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.
146
+
بالمناسبة ، يمكن للعديد من المحررين مثل [WebStorm](https://www.jetbrains.com/webstorm/) فهمهم أيضًا واستخدامهم لتوفير الإكمال التلقائي وبعض التحقق التلقائي من التعليمات البرمجية.
145
147
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/>.
148
+
أيضًا ، هناك أدوات مثل[JSDoc 3](https://github.com/jsdoc3/jsdoc) يمكنها إنشاء وثائق HTML من التعليقات. يمكنك قراءة المزيد من المعلومات حول JSDoc على<http://usejsdoc.org/>.
147
149
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.
150
+
لماذا تحل المهمة بهذه الطريقة؟
151
+
: ما هو مكتوب مهم. لكن ما هو * غير * مكتوب قد يكون أكثر أهمية لفهم ما يحدث. لماذا يتم حل المهمة بهذه الطريقة بالضبط؟ الكود لا يعطي إجابة.
150
152
151
-
If there are many ways to solve the task, why this one? Especially when it's not the most obvious one.
153
+
إذا كانت هناك طرق عديدة لحل المهمة ، فلماذا هذه المهمة؟ خاصة عندما لا يكون الأمر الأكثر وضوحًا.
152
154
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.
155
+
بدون هذه التعليقات يكون الوضع التالي ممكناً:
156
+
1. أنت (أو زميلك) تفتح الشفرة المكتوبة منذ بعض الوقت ، وترى أنها "دون المستوى الأمثل".
157
+
2. أنت تفكر: "كم كنت غبيًا في ذلك الوقت ، وكم أنا أذكى الآن" ، وأعد الكتابة باستخدام متغير "أكثر وضوحًا وصحة".
158
+
3. ... كانت الرغبة في إعادة الكتابة جيدة. ولكن في هذه العملية ، ترى أن الحل "الأكثر وضوحًا" غير موجود بالفعل. حتى أنك تتذكر لماذا ، لأنك جربته بالفعل منذ فترة طويلة. أنت تعود إلى البديل الصحيح ، لكن الوقت ضاع.
157
159
158
-
Comments that explain the solution are very important. They help to continue development the right way.
160
+
التعليقات التي تشرح الحل مهمة جدا. أنها تساعد على مواصلة التنمية بالطريقة الصحيحة.
159
161
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.
162
+
أي ميزات خفية للكود؟ أين يتم استخدامها؟
163
+
: إذا كان الرمز يحتوي على أي شيء خفي وغير بديهي ، فمن المؤكد أنه يستحق التعليق.
162
164
163
-
## Summary
165
+
## ملخص
164
166
165
-
An important sign of a good developer is comments: their presence and even their absence.
167
+
من التعليقات الهامة لمطور جيد التعليقات: حضورهم وحتى غيابهم.
166
168
167
-
Good comments allow us to maintain the code well, come back to it after a delay and use it more effectively.
169
+
تسمح لنا التعليقات الجيدة بالحفاظ على الشفرة جيدًا ، والعودة إليها بعد فترة تأخير واستخدامها بشكل أكثر فعالية.
168
170
169
-
**Comment this:**
171
+
** تعليق هذا: **
170
172
171
-
-Overall architecture, high-level view.
172
-
-Function usage.
173
-
-Important solutions, especially when not immediately obvious.
173
+
-العمارة الشاملة ، منظر عالى المستوى.
174
+
-استخدام الوظيفة.
175
+
-حلول مهمة خاصة عندما لا تكون واضحة على الفور.
174
176
175
-
**Avoid comments:**
177
+
** تجنب التعليقات: **
176
178
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.
179
+
-يخبر "كيف يعمل الرمز" و "ماذا يفعل".
180
+
-ضعهم فقط إذا كان من المستحيل جعل الكود بسيطًا وصفيًا ذاتيًا بحيث لا يتطلبهم.
179
181
180
-
Comments are also used for auto-documenting tools like JSDoc3: they read them and generate HTML-docs (or docs in another format).
182
+
تُستخدم التعليقات أيضًا في أدوات التوثيق التلقائي مثل JSDoc3: فهم يقرؤونها وينشئون مستندات HTML (أو مستندات بتنسيق آخر).
0 commit comments