10 Tips Menulis Code JavaScript yang Clean

Muhammad Afifudin
11 Aug 202107:35

Summary

TLDRIn this engaging video, the speaker, Awali, shares essential tips for writing clean JavaScript code. The script begins with an explanation of clean code, emphasizing its importance for readability and maintainability. Awali then presents ten practical tips, such as using English for variable naming, choosing verbs for functions, and plurals for arrays. The tips also cover avoiding ambiguous variable names, simplifying conditions, and ensuring functions have a single responsibility. The video aims to help developers write more understandable and error-free code, with a promise of more tips in a follow-up episode.

Takeaways

  • 😀 Clean code is defined as code that is easy to understand and modify by others.
  • 📝 Always use English for naming variables, even if the application is in another language.
  • 🏷️ Use verbs for function names and nouns for variable names to improve clarity.
  • 📚 Use plural forms for array variables to indicate that they contain multiple items.
  • 🔄 When iterating over an array, use the singular form of the array's variable name for clarity.
  • 🔑 Prefix boolean variables with 'is', 'can', or 'should' to make their purpose clear.
  • 🔒 Store conditions in variables to simplify the code and make it more readable.
  • 🚫 Avoid using 'not' in variable names as it can make reading the code more difficult.
  • 📈 Use specific and explicit names for variables instead of generic terms like 'data'.
  • 🔧 Functions should have a single responsibility to make the code easier to understand and maintain.
  • ⚙️ Use JavaScript linters to catch and correct inconsistencies in your code, such as formatting errors.

Q & A

  • What is the definition of 'clean code' according to the video script?

    -Clean code is defined as code that is easy to understand and modify, similar to how the quality of a sports car can be measured by the number of 'WTFs per minute' - if it's hard for others to understand, it might indicate poor code quality.

  • What is the first tip shared in the script for writing clean JavaScript code?

    -The first tip is to always use English for naming variables, regardless of the application's language, to maintain code cleanliness.

  • Why should verbs be used for functions and nouns for variables according to the script?

    -Using verbs for functions and nouns for variables helps to clearly distinguish between actions (functions) and data (variables), making the code more readable.

  • What is the recommendation for naming variables that represent arrays?

    -The recommendation is to use plural forms for array variables to accurately represent that they contain multiple elements.

  • Why should the singular form of the variable name be used during iteration over an array?

    -Using the singular form during iteration helps to clearly indicate the individual elements being processed within the array.

  • What is the purpose of using prefixes like 'is', 'can', or 'should' for boolean variables?

    -These prefixes make the purpose of the boolean variable clearer and reduce ambiguity, making the code easier to understand.

  • Why should conditions be stored in variables?

    -Storing conditions in variables makes the code more readable, as it allows others to understand the logic without having to parse complex conditional statements.

  • What is the advice against using the word 'data' when naming variables?

    -The word 'data' is too generic, and it's advised to use more specific or explicit variable names to represent the actual data being stored.

  • What is the main reason for keeping functions focused on a single responsibility?

    -Focusing functions on a single responsibility makes the code easier to understand, maintain, and debug.

  • Why is it important to use JavaScript linters?

    -JavaScript linters help identify and correct inconsistencies and errors in the code, promoting a more consistent and clean coding style.

  • What does the script suggest for those who are not fluent in English when naming variables?

    -The script suggests using tools like Google Translate to assist with naming variables in English, as it's important for code cleanliness.

Outlines

00:00

😀 Introduction to Clean Code in JavaScript

The speaker, Awali, begins by discussing the concept of clean code in JavaScript, emphasizing its importance for readability and maintainability. They introduce the idea that code quality can be measured by the number of 'WTFs per minute,' suggesting that clean code should be easily understandable by others. Awali references a popular job description that highlights the necessity of clean code for future modifications and feature additions. They also mention a quote from the internet that defines clean code as being easy to understand, modify, and maintain. The speaker then promises to share 10 tips to write clean JavaScript code.

05:02

📝 Tips for Writing Clean JavaScript Code

In the second paragraph, the speaker continues with the first five tips for writing clean JavaScript code. These include using English for variable naming, using verbs for functions and nouns for variables, utilizing plural forms for array variables, and using singular forms when iterating over arrays. The speaker also advises the use of prefixes like 'is', 'can', or 'should' for boolean variables to make their purpose clear and reduce ambiguity. They stress the importance of variable naming, stating that it should answer questions about the variable's purpose and functionality without needing comments. The paragraph concludes with a final tip.

Mindmap

Keywords

💡Clean Code

Clean code refers to a style of writing code that is easy to understand, maintain, and modify. In the video, the concept is introduced by comparing it to the quality of a sports car, which can be measured by the number of 'WTFs per minute'. The video emphasizes that clean code should be easily understandable by others, which is a key aspect of writing maintainable and readable JavaScript code.

💡Variable Naming

Variable naming is a fundamental aspect of programming that affects code readability. The video script stresses the importance of using English for variable names, even if the application is in another language, to maintain code cleanliness. Examples from the script include using verbs for functions (e.g., 'get', 'delete') and nouns for variables, which helps in conveying the purpose of the variable or function clearly.

💡Verbs for Functions

The use of verbs in function names is a practice that helps to describe the action performed by the function. In the script, it's suggested to use action words like 'get', 'head', 'delete', and 'validated' to name functions, which makes it clear what the function does, contributing to the overall clarity of the code.

💡Nouns for Variables

Nouns should be used for variable names to represent data entities. The video mentions using singular or plural forms of nouns for variables, which helps in identifying the type of data the variable holds. For example, using 'user' or 'users' instead of generic terms like 'data' makes the code more descriptive and easier to understand.

💡Plurals for Arrays

When naming variables that represent arrays, the script suggests using plural forms to indicate that the variable contains multiple items. This is exemplified in the script by contrasting a singular form like 'user' with the correct plural form 'users', which holds a collection of user objects, enhancing the code's readability.

💡Singular Form in Iterations

During iterations over arrays, the script advises using the singular form of the array's name for the iteration variable. This practice helps in making the code more readable and intuitive, as it clearly indicates that the iteration is over individual elements of the array, as shown in the script with examples like 'user' instead of a generic term like 'item'.

💡Prefixes for Boolean Variables

Boolean variables often represent true or false conditions. The video suggests using prefixes like 'is', 'can', 'should' to clearly indicate the purpose of the boolean variable. For instance, changing a variable name from 'expired' to 'isExpired' makes it immediately clear what the variable represents, reducing ambiguity.

💡Positive Naming Convention

The script emphasizes the importance of using positive naming conventions for variables, meaning avoiding double negatives in variable names. Positive naming makes the code easier to read and understand at a glance. For example, 'isNotExpired' is less clear than 'isActive', which is a positive and straightforward naming approach.

💡Avoid Generic Variable Names

The video advises against using overly generic names like 'data' for variables, as they do not convey specific information about the content they hold. Instead, specific and explicit variable names should be used, such as 'studentData' or 'teacherData', which provides clear context about the data being stored or manipulated.

💡Single Responsibility Principle

This principle in software development suggests that a function should have only one reason to change, meaning it should perform a single task. The script illustrates this by showing an example of a function that does too much and then breaking it down into smaller, more focused functions, each with a specific responsibility, which leads to cleaner and more maintainable code.

💡JavaScript Linters

JavaScript linters are tools that analyze code for potential errors and enforce style consistency. The video mentions using linters to catch errors and correct inconsistencies in code, such as improper spacing or punctuation. The script refers to a linter named 'ESLint', which helps maintain a clean and consistent codebase.

Highlights

The concept of clean code is introduced as code that is easy to understand and maintain.

Clean code is defined by the ability to be easily understood by others, reducing the 'WTF per minute' count.

The importance of using English for variable naming, regardless of the application's language.

Using verbs for function names and nouns for variable names to improve code readability.

Recommendation to use plural forms for array variables to reflect their content.

Using the singular form of array variable names during iteration for clarity.

Adding prefixes like 'is', 'can', or 'should' to variable names for better clarity.

Transcripts

play00:00

halo halo teman-teman semua Pada

play00:02

kesempatan kali ini saya pengen sharing

play00:04

tips gimana nulis code javascript yang

play00:07

clean

play00:08

Hai saya Awali dulu dengan pembahasan

play00:10

Apa itu clean code

play00:12

Hai nah ini ada sebuah Jobs populer yang

play00:16

bilang bahwa kualitas sport itu bisa

play00:18

diukur dengan jumlah WTF permenit

play00:20

Maksudnya gimana tuh kalau kalian lihat

play00:23

ini di pintunya ada tulisan quote review

play00:26

jadi ini ceritanya Kok kita lagi di

play00:28

review oleh orang lain ketika orang yang

play00:30

review kita kesulitan memahami maksud

play00:33

dari kota tersebut itu bisa jadi

play00:35

pertanda bahwa chord gitar kurang bagus

play00:37

jadi bisa dibilang gampangnya definisi

play00:40

cleancut itu adalah kordkita mudah

play00:42

dipahami oleh orang lain apa Nggak ini

play00:45

beda ya dengan kode itu error apa nggak

play00:47

kalau aku teror apa nggak itu ya

play00:50

Tergantung kuatnya bisa jalan apa enggak

play00:52

Tapi kalau kulitnya itu clean apa enggak

play00:55

itu tergantung bisa mudah dipahami orang

play00:57

lain apa Nggak

play01:00

ini juga saya ambil sebuah kutipan dari

play01:03

internet

play01:03

clean code adalah God yang mudah

play01:06

dipahami dan mudah diubah mudah diubah

play01:09

disini Maksudnya seperti ditambah fitur

play01:12

baru uh atau dimintain untuk jangka

play01:15

panjang seperti itu

play01:17

Nah di video ini Saya mau share 10 tips

play01:20

biar kuat kalian lebih klik tanpa

play01:22

berlama-lama langsung saja tips yang

play01:25

pertama ini basic banget selalu Gunakan

play01:28

Bahasa Inggris untuk menamai variabel

play01:31

walaupun aplikasi yang mau kita bikin

play01:33

itu berbahasa Indonesia penamaan

play01:35

variabel tetap menggunakan bahasa

play01:37

Inggris kontennya bahasa Indonesia nggak

play01:39

masalah kalau kalian belum lancar bahasa

play01:42

Inggris Ya silahkan manfaatkan Google

play01:44

translate karena kalau enggak pakai

play01:47

bahasa Inggris ya jadi enggak clean

play01:49

chordnya di Hai tips yang kedua gunakan

play01:52

verb untuk fungsi dan non untuk variabel

play01:56

propitu artinya kata kerja Contohnya

play01:58

seperti get head delete validated dan

play02:02

lain-lain gunakan kata kerja tersebut

play02:04

untuk menemui fungsi jadi gini get your

play02:08

share eyuser dan lain-lain

play02:11

Ya udah kan bentuk kata dasar jangan

play02:13

gunakan bentuk lain seperti mapping user

play02:16

di letting you share tuh jangan

play02:19

Hai selanjutnya gunakan kata benda untuk

play02:22

variabel bisa pakai bentuk tunggal

play02:25

maupun bentuk jamak

play02:27

Hai tips ketiga gunakan bentuk jamak

play02:30

atau plurals untuk variabel array

play02:32

ini merupakan contoh code yang gak Klin

play02:35

karena dia menggunakan bentuk tunggal

play02:38

untuk menyimpan sebuah Rey Rey itu kan

play02:41

Isinya banyak ya jadi harusnya jamak hal

play02:45

ini juga berlaku untuk nama

play02:47

fungsi-fungsi yang Ridernya array maka

play02:50

kata Bendanya harus pakai bentuk

play02:52

pluralnya

play02:53

jadi seperti ini baru klik

play02:57

tips keempat pada saat kalian melakukan

play03:00

iterasi pada sebuah arey gunakan bentuk

play03:03

singular dari nama variabel array nya

play03:05

jangan sekedar pakai satu huruf doang

play03:08

misalnya a gitu seperti ini atau kata

play03:12

lain yang terlalu umum seperti item Hai

play03:15

yang bagus adalah seperti ini gunakan

play03:17

bentuk singular atau tunggal dari are

play03:20

yang di iterasi

play03:24

Hai tips kelima gunakan prefiks atau

play03:26

awalan is he scan atau should untuk

play03:31

menemui variabel Bulian tujuannya biar

play03:34

lebih jelas kurangi apapun itu yang bisa

play03:37

membuat kita ambigu seperti dicontoh ini

play03:40

di sini ada variabel expired ini bisa

play03:44

membingungkan ya

play03:45

expired itu Bulian apa Ded

play03:49

Hai kalau kita ubah nama variabelnya

play03:51

jadi is expired gini kan jadi lebih

play03:53

jelasnya

play03:56

Oke sebelum lanjut ke tips selanjutnya

play03:58

mungkin sebagian dari kalian nyadar

play04:00

kalau dari tadi bahasanya soal penamaan

play04:02

variabel Kenapa demikian

play04:05

jawabannya simple karena kalau orang

play04:08

ngebaca chord itu pasti baca variabel

play04:10

dulu baru baca operasinya Sebagai

play04:14

tambahan ini saya kutip dari internet

play04:16

nama variabel fungsi class itu harus

play04:21

menjawab semua pertanyaan-pertanyaan

play04:22

besar itu tadi harus ngasih tahu kenapa

play04:26

variabel tersebut ada apa yang dia

play04:28

lakukan dan gimana Cara pakainya

play04:31

Hai kalau nama tadi sampai butuh comment

play04:34

berarti penamaan variabel tadi enggak

play04:37

menggambarkan tujuannya apa

play04:41

Hai baik kita lanjut tips keenam simpan

play04:44

kondisi ke dalam variabel

play04:47

hai ketika kita disuguhkan quote seperti

play04:49

ini kita pasti loading dulu nih untuk

play04:52

bisa tahu maksud dari kondisi ini tuh

play04:54

apa ini bisa kita improve dengan

play04:57

menyimpan logika kondisi tadi ke dalam

play04:59

sebuah variabel dengan demikian kita

play05:02

baca koran itu seperti baca tulisan

play05:04

bahasa Inggris aja gitu

play05:07

Hai kemudian kalau kondisinya ada banyak

play05:09

Sederhanakan lagi simpan

play05:11

variabel-variabel kondisi tadi ke dalam

play05:14

variabel lain jadi seperti ini Bagusnya

play05:18

sih maksimal dua variabel aja ya di sini

play05:20

ya biar nggak pusing bacanya

play05:22

Hai tips ketujuh penamaan variabel

play05:26

Bulian harus positif alias enggak boleh

play05:29

pakai not Hal ini dikarenakan membaca

play05:32

double negatif kayak gini itu susah

play05:34

tidak-tidak expired pusingkan bacanya

play05:38

bagusnya positif seperti ini

play05:41

Hai tips ke-8 hindari menamai variabel

play05:45

dengan datang karena semua yang di

play05:48

simpan di variabel itu ya pasti

play05:50

data-data siswa data guru data nilai dan

play05:54

lain-lain jadi data itu terlalu umum

play05:57

maka dari itu hindari menamai variabel

play06:00

dengan data gunakan nama variabel yang

play06:03

spesifik atau eksplisit

play06:07

tips kesembilan jangan bikin fungsi yang

play06:10

melakukan banyak hal sekaligus satu

play06:13

fungsi sebaiknya hanya diberi satu

play06:14

tanggungjawab saja contoh yang kurang

play06:17

bagus yaitu seperti ini fungsi ini

play06:20

melakukan banyak hal sekaligus

play06:22

sebaiknya fungsi ini dipecah lagi

play06:24

menjadi beberapa bagian di sini saya

play06:27

pecah lagi fungsi tersebut menjadi tiga

play06:29

fungsi lain yang masing-masing melakukan

play06:31

tugas yang spesifik dengan demikian

play06:34

chord kita jadi lebih mudah dipahami

play06:36

serta bakal lebih mudah untuk proses

play06:39

dibagi

play06:40

Hai

play06:41

tips kesepuluh selalu gunakan javascript

play06:45

l l itu Gunanya buat ngorek si error

play06:48

pada kulit kalian dan mengoreksi chord

play06:50

kalian yang enggak konsisten misal

play06:53

spasinya Nggak konsisten pemakaian titik

play06:55

koma ya enggak konsisten dan sejenisnya

play06:58

ini enggak Saya bahas secara detail di

play07:00

video ini saya cuma mau kasih kemarin

play07:03

aja nanti outputnya bakal seperti yang

play07:05

ada di bagian kanan ini dia mengoreksi

play07:08

kesalahan-kesalahan yang kita buat

play07:10

Hai kalian bisa cari tutorialnya

play07:12

printernya namanya qslim

play07:15

s5es nya itu dari kata ecmascript ya

play07:19

Hai sekian 10 tips menulis code

play07:21

javascript yang clean pada episode kali

play07:24

ini saya udah siapin juga 10 tips lagi

play07:27

di part2 sampai jumpa di video

play07:30

selanjutnya terima kasih

Rate This

5.0 / 5 (0 votes)

Related Tags
Clean CodeJavaScriptCode QualityVariable NamingFunction VerbsPlural VariablesSingular IterationConditional LogicPositive NamingSingle ResponsibilityCode Consistency