100% found this document useful (1 vote)
140 views5 pages

7 Ways To Write Cleaner, More Effective Code

Clean code is simple, readable, and well-organized. It has meaningful names, single responsibilities, and minimal comments. Tests should be included. Dependencies should be managed carefully with minimal bidirectional relationships. Overall organization through formatting, spacing, structure and naming helps code be understandable and maintainable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
140 views5 pages

7 Ways To Write Cleaner, More Effective Code

Clean code is simple, readable, and well-organized. It has meaningful names, single responsibilities, and minimal comments. Tests should be included. Dependencies should be managed carefully with minimal bidirectional relationships. Overall organization through formatting, spacing, structure and naming helps code be understandable and maintainable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

7 Ways To Write Cleaner, More Effective Code

Software engineering is more than just learning a programming language and writing code. You
are expected to write good software as a software engineer or developer. ¿So, what makes a good
piece of software? Reading some of the project's code can help you determine whether or not the
software is good. If the code is simple to comprehend and modify, it is undoubtedly good software,
and engineers enjoy working on it.

Nobody likes to continue working on a project with bad or chaotic code (it may be a nightmare at
times...). Due to time constraints, some developers forgo developing clean code. They rush to go
quicker, but in reality, they end up moving slower. It causes further bugs, which they will have to
solve later when they return to the same area of code. This process takes far longer than the time
spent writing the code. According to a study, the ratio of time spent reading code to time spent
developing code is well over 10 to 1.

What Makes a Clean Code: Let's have a look at some of the features of clean code before we get
into it...

1. Code that is well-written should be readable. When someone reads your code, they should
feel as if they are reading poetry or prose.
2. Elegant programming should be simple. It should be enjoyable to read and bring a grin to
your face.
3. Clean code should be straightforward and simple to comprehend. It must adhere to the
single-responsibility principle (SRP).
4. Clean code should be simple to comprehend, alter, and maintain.
5. All of the tests should be run with clean code.

How to Write Clean and Better Code?

1. Use names that are meaningful to you.

You'll be naming variables, functions, classes, arguments, modules, packages, directories, and
other things a lot. Make it a habit to give your code meaningful names. Whatever names you give
your code, they should serve three functions: what it performs, why it exists, and how it is used.
Consider the following scenario:
int a; // number of users.
In the above example, you must provide a comment alongside the name declaration of a variable,
which is not a good practice. The name you give your code should be descriptive of its purpose. It
should state what a variable, function, or method is for. As an example, a preferable variable name
for the following example might be: int number_of_users. Choosing meaningful names may take
some time, but it will make your code lot cleaner and easier to read for other developers as well as
yourself. Also, keep the names to three or four words maximum.

2. Single Responsibility Principle (SRP)

In any programming language, classes, functions, or methods are an excellent way to organize
code, so while you're developing code, you should pay attention to how to construct a function that
communicates its meaning. The majority of newcomers make the error of writing a function that
can handle and do practically anything (perform multiple tasks). It makes your code more difficult
for developers to understand, which causes issues when they need to patch errors or locate code.
So, when writing a function, keep these two factors in mind to keep it clean and simple to
understand...

1. They need to be small.


2. They should just focus on one subject and do it well.

Your function should follow the single responsibility paradigm, as stated in the previous two
sections. That is to say, it should not have a nested structure or more than two indent levels. If your
function performs a certain duty, this strategy will make your code lot more understandable, and
other developers will be able to easily comprehend or build another feature if you use it.

Also, no more than three arguments should be used in your function. More arguments do more
jobs, thus try to reduce the number of arguments to a minimum. Passing more than three arguments
makes your code muddled, bulky, and difficult to debug if a problem arises. If your function has
try/catch/finally statements, create a new function that only contains the try-catch-finally
statements. Also, pay attention to the name of your function. Use a descriptive name for your
function that expresses exactly what it does.

Example:

function subtract(x, y) {
return x - y;
}
The function name in the preceding example makes it plain that its goal is to execute subtraction
for two numbers, and it also has only two arguments. The links 7 Common Programming Principles
That Every Developer Must Follow and SOLID Principle provide more information on how to write
a decent function.

3. Don't Make Unnecessary Remarks

Developers frequently use comments to indicate the purpose of a line in their code. True, comments
are really useful for illustrating what the code does, but they also necessitate extra code
maintenance. When code is being developed, it moves around a lot, but if the comment stays in
the same place, it might cause a lot of problems. It can cause developers to get perplexed, and it
can also cause them to become sidetracked by irrelevant comments. It's not as if you shouldn't use
comments at all; in fact, they're occasionally necessary... If you're working with a third-party API
and need to explain any behavior, you can use comments to do so, but don't use comments where
they aren't required.

Today's programming languages have English-like syntax, which is sufficient to describe the intent
of a line in your code. Use sensible names for variables, functions, and files to avoid comments in
your code.

4. Write code that is easy to understand.

When writing code, many people, especially beginners, make the error of writing everything in a
single line and not using adequate spacing, indentation, or line breaks. Their code becomes clumsy
and difficult to maintain as a result. There's always the possibility that another human will discover
your code and have to work with it. When other developers try to read and understand the jumbled
code, they waste their time. As a result, always pay attention to how your code is formatted. When
you return to your own code after a few days to make modifications, you will also save time and
energy. As a result, make sure your code has adequate indentation, spacing, and line breaks to
make it readable for others. The maintainability of your code is influenced by your coding style and
formatting. A software developer's coding style will be remembered for the rest of his or her life.

//Bad Code
async getUsers(){var url = '/getUsers'; const res = await this.callGetApi
('get', url, {'filter' : this.filter,})
if (res.status == 200) {this.listUsers = res.data;}
this.fullscreenLoading = false;
},
//Good Code
async getUsers(){
var url = '/getUsers'

const res = await this.callGetApi('get', url, {


'filter' : this.filter,
})

if (res.status == 200) {
this.listUsers = res.data;
}
this.fullscreenLoading = false;
},

5. Write Unit Tests.

In development, writing unit tests is critical. It cleans up your code and makes it more versatile and
maintainable. It gets easy to make code modifications and eliminate bugs. Test Driven
Development (TDD) is a software development technique in which requirements are converted into
particular test cases, and then the program is modified to pass additional tests. The three rules of
TDD, according to Robert C. Martin (Uncle Bob), demonstrate...

1. You may not write any production code unless it is necessary to pass a failing unit test.
2. You can only write as much of a unit test as is required to fail, and compilation failures are
considered failures.
3. You may only create as much production code as is necessary to pass the one failed unit
test.
6. Dependencies Should Be Handled With Caution

When it comes to software development, dependencies must be carefully managed. If at all


feasible, keep your dependencies in a single direction. It means, for example, that a kitchen class
is reliant on a dishwasher class. This is a one-directional reliance as long as the dishwasher isn't
also dependant on the kitchen class. The kitchen class is simply utilizing the dishwasher, but the
dishwasher is unconcerned and can be used by everyone. It isn't necessary for it to be a kitchen.
This one-directional dependency example is easy to maintain, but it's hard to have one-directional
reliance all of the time, so we should aim to have as many as feasible. Things become much more
complicated when there is dependency in numerous ways. Because both entities are dependent
on each other under bidirectional dependency, they must coexist even if they are separate. It's
difficult to update some systems when their dependencies don't all point in the same direction. As
a result, always be cautious when managing your dependencies.

7. Make sure that your project is well-organized.

This is a fairly typical problem in software development: we create and delete a lot of files and
directories in our projects, which can make it difficult for other developers to comprehend and work
on them. We agree that you can't create a perfect folder or file organization on day one, but as your
project grows larger, you'll need to pay more attention to the arrangement of your folders, files, and
directories. A well-organized folder and file make everything apparent, making it easier to
comprehend a comprehensive project, locate a specific folder, and make modifications to it. As a
result, ensure that your directory or folder structure is well-organized (Same applies for your code
as well).

You might also like