Tips For Writing Cleaner Code
Ali Samir
In the programming landscape, code clarity,
and readability crucially influence its
maintainability and collaboration speed.
Though every language has its quirks, crafting
clean code in JavaScript presents its own
distinct challenges and guidelines.
In this article, I'll highlight key considerations
for writing clean code, specifically in the
context of JavaScript.
Ali Samir
USE DESCRIPTIVE NAMES
Choose descriptive variable and function names. Rather
than vague terms like "name" or "n", use specific ones like
"physicianLastName". For clarity, function names should
start with a verb, such as "getPhysicianName", ensuring
their purpose is instantly clear.
Ali Samir
USE EMPTY LINES TO CREATE
A READABLE CODE
Using empty lines enhances code readability. They clearly
mark where functions end, separate variable declarations
from their operations, and can be added before return
values for clarity.
Ali Samir
AVOID PASSING MORE THAN THREE
ARGUMENTS TO A FUNCTION
Remember, aim for readable functions. Three parameters
are easier to track than a slew of them. If you need more
than three, send an object and access its keys as needed.
Revisit this if you're considering sending numerous
parameters to a function.
Ali Samir
ENSURE FUNCTIONS DO JUST ONE THING.
When in a rush, it's tempting to write multi-purpose
functions. However, it's best to ensure a function does only
one thing. Multiple simple functions are preferable to one
complex one.
Ali Samir
FUNCTIONS SHOULD BE CONCISE
Adhering to the Single Responsibility Principle (SRP)
ensures that a function does only one thing, making the
code more maintainable and less bug-prone.
If you find a function becoming too lengthy or complex,
it's often a signal to consider using a class. Within a
class, you can break tasks into smaller, focused
methods, improving clarity and manageability in your
code.
Ali Samir
SHORTEN THE LINE LENGTH
Remember, our goal is to craft code that's easily
readable. Steer clear of overly lengthy lines of code;
ideally, each line should fit within your screen's width,
eliminating the need for horizontal scrolling.
Don't forget, that numerous tools, such as Prettier, can
help manage and format your code effectively.
Ali Samir
REFRAIN FROM UTILIZING COMMENTS
Maintaining code is already a task; adding comments
complicates it further. The solution?
Use descriptive naming. If your code clearly conveys its
purpose, comments become redundant. While
occasional comments might be necessary, they should
be the exception, not the rule. If you're commenting
often, your code may lack clarity.
Ali Samir
CRAFT A CLEAR COMMIT MESSAGE
When making a commit, it's crucial to provide a detailed
message. Such descriptions can be invaluable in
recalling the purpose of our code months down the line.
Refrain from vague messages that lack clarity. For
instance, merely writing "refactoring" may not
sufficiently inform subsequent developers about the
commit's intent.
Ali Samir
UTILIZE UNIT TESTS AND ADOPT TDD
It's easy to view unit tests as time-drainers, but they're
invaluable. Imagine a new developer altering your old
code and unintentionally causing disruptions. With unit
tests, spotting these issues is simple. While deadlines
can pressure us to cut corners, investing in unit tests
now can prevent future complications.
Start by crafting your unit test - it'll initially fail. Then,
develop or refine your code. Once done, rerun the test;
it should pass. This strategy not only streamlines
troubleshooting but also bolsters the code's reliability.
Ali Samir
DISCOVER DESIGN PATTERNS
"Design Patterns" encompass a vast subject area. To
introduce, delving into Design Patterns reveals the
strategies that seasoned developers have devised to
tackle recurring challenges in software development.
It saves us from the need to recreate solutions from
scratch.
Ali Samir
Ali Samir
Software Engineer