aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Programming
  • Software Engineering
  • Work & Jobs

10 Tips For Writing Clean Code

  • root
  • December 15, 2022
  • 5 minute read

Clean code is a reader-focused development style that produces software that’s easy to write, read and maintain. Knowing how to produce clean code is an essential skill for software developers. Often, you may be tempted to consider your work complete when the application operates as expected. But we’re not merely writing code for computer consumption.

Clean code is about recognizing that your audience isn’t just a computer, it’s real-live humans! With this principle in mind, let’s review the reasons clean code matters and dive into some tips and tricks when it comes to how we can do it in practice.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

What is clean code?

Clean code is clear, understandable, and maintainable. When you write clean code, you’re keeping in mind the other people who may read and interpret your code at a later time. You’re helping others understand the purpose of your code so that they can make changes to it eventually.

Clean code principles lead to source code that’s highly modular and thus easier to read and test. If you think of these practices as part of a house, clean code is the foundation. Implementing clean code principles is a foundational skill that pays off especially well when it’s time to refactor code or bring code under test.

What are the 3 principles of clean code?

There are three core principles to remember when it comes to writing clean code:

  1. Choose the right tool for the job
  2. Optimize the signal-to-noise ratio
  3. Strive to write self-documenting code

1. Follow conventions

Using a naming convention is a great way to get started — it keeps things clear and lets you know exactly what you’re working with.

A naming convention basically means you decide you’ll call your variables by names that adhere to a certain set of rules. It can get hairy, and a lot of people don’t always agree on which is best. So to keep it simple. It can be something as simple as prefixing variable names with their data type, like this:

Read More  12 Programming Resources For Coders Of All Levels
int iMyInteger = 10; 

float fMyFloat = 10.5f;
MyIntiger Code Snippet

2. Indicate variable scope

The next thing, which follows nicely from using naming conventions, is using a convention to indicate variable scope. Again, there are no rules, and everyone has their own way of doing it — as long as it’s consistent throughout all of your code, it will always be clear what can be used from where.

A common convention goes as follows:

//private and protected variables are prefixed with an underscore 
int _iWindowSize = 900;

//public variables are left as they would be normally
int iWindowSize = 900;

//constant values are in all caps and separated with underscores
int I_WINDOW_SIZE = 900;
360 no scope code snippet

3. Say what you mean

This is pretty straightforward, but it’s probably the most common and maybe the easiest one to forget. Easily the most frustrating thing for another developer looking at your code is seeing a variable with a misleading name or, worse, named with a single letter.

Let’s take a look at an example of this:

int checkNum()
{
     if(n < max)
     {
          return -1;
     }else{
          return 1;
     }
}
variable with a misleading name

4. Whitespace is nice space

Using whitespace can be incredibly powerful and normally has absolutely no downside. Sometimes in languages like JavaScript where the file size of the source code itself is important, you might want your files to be small, and that whitespace can add a few extra kilobytes. When you can, keep all your whitespace during development so the code is readable. Then, use one of the many smart little programs that go through code and chop out all the whitespace just before you upload it.

5. Commenting saves lives — or at least headaches

Adding comments to your code can be invaluable—they can quickly show what a complex function is doing or explain the order of certain operations. Beyond explaining the purpose of the code itself, though, comments can help others understand what problems you were trying to solve with your code, which can help them come up with better solutions. Keep in mind that too much commenting can sometimes have a detrimental effect by creating messier code.

Read More  Introducing reCAPTCHA Enterprise’s Mobile SDK To Help Protect iOS, Android Apps

6. Automate to save time and space

Writing slightly more technical code doesn’t mean it has to be less readable. Multiple lines of duplicate code are not only harder to read, but they also increase the chance for error. The great thing about programming is that you can express complex commands in tidy, reusable, and clever ways.

Here’s an example of poor, duplicated code:

While this might look okay, and it is functional, it isn’t clear why any of it is happening at all. You can repair it with a simple comment that explains what this code does.

box1.x = 10;
box1.y = 20;
box2.x = 30;
box2.y = 20;
box3.x = 50; 
box3.y = 20;
box4.x = 70;
box4.y = 20;
Duplicated Code Snippet

And here’s an approach that cleans everything up:

boxArray = [box1, box2, box3, box4];

for(int i = 0; i < sizeOf(boxArray); i++)
{
     boxArray[i].x = 10 + i * 20;
     boxArray[i].y = 20;
}
Clean code snippet for duplicated code example

7. Remember the power of i

When you have a code block with multiple loops one after the other, you need different iterator variables. There is always debate about what to use, and the answer is slightly subjective, but when they’re one after another, it makes sense to declare your iterator outside of the loop and reuse it. It’s not only better to look at, as it’s always clear that “i” is your iterator variable, but it’s also slightly more efficient.

Let’s look at an example of what I’m talking about:

//declare variable initially
int i ;

for(i = 0; i<10; i++)
{
     //loop stuff
}

for(i = 0; i<200; i++)
{
     //more loop stuff
}
declare your iterator outside of the loop and reuse it

8. Birds of a feather group similar variables together

When your projects start to get larger, your classes will likely have many variables. First, you should be keeping all of your variable declarations at the top of the page, or at the very least all together somewhere—this speeds up any kind of searching.

Read More  How-To: Install Apache Cassandra DB On Ubuntu 22.04

Second, even though they are all together, it often helps to arrange them in such a way that makes them even easier to comprehend. For example, grouping them all by what they are is a good way to go. It’s quite likely that you’ll have several types of the same object, so keep those all together in groups, and then maybe have a section for the miscellaneous ones underneath.

9. Keep it functional

Mile-long function definitions are an easy way to clutter your code. Normally it’s best to take a look at what’s actually being done. If a function is doing more than its name suggests, then perhaps some of the excess functionality could be split out into its own function.

10. Keep it classy

Similar to the functional problem, if there’s a large amount of functionality you’re keeping all in one place, it could be better to create a separate class to handle that functionality.

When it comes to writing, reading and maintainability, clean code is essential. The steps outlined above are not concrete rules. Use them as outline, or use them as a guide to find your own style and way of doing things. The important thing is this: Keep it tidy, clearly sectioned, and consistent. Anyone working with your code will appreciate the effort, and might even learn something from your example.

Source: Plural Sight


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

root

Related Topics
  • Clean code
  • Code
  • Coding
  • Developers
  • Programming
  • Software
  • Software Engineering
You May Also Like
View Post
  • Software Engineering
  • Technology

Claude 3.7 Sonnet and Claude Code

  • February 25, 2025
View Post
  • Engineering
  • Software Engineering

This Month in Julia World

  • January 17, 2025
View Post
  • Engineering
  • Software Engineering

Google Summer of Code 2025 is here!

  • January 17, 2025
View Post
  • Software Engineering

5 Books Every Beginner Programmer Should Read

  • July 25, 2024
goswifties_typewriter_20240510_wm
View Post
  • Featured
  • Work & Jobs

From Keys To Progress. How The Typewriter Revolutionised Communication And Empowered Society.

  • May 14, 2024
View Post
  • Technology
  • Work & Jobs

Leading Companies Launch Consortium To Address AI’s Impact On The Technology Workforce

  • April 4, 2024
View Post
  • People
  • Technology
  • Work & Jobs

Usher’s New Look Embarks on AI-Focused Collaboration with IBM to Set Students up for Career Success

  • March 7, 2024
View Post
  • Design
  • Engineering
  • Work & Jobs

Five Key Things To Consider When Building A Cloud FinOps Team

  • February 12, 2024

Stay Connected!
LATEST
  • Camping 1
    The Summer Adventures : Camping Essentials
    • June 27, 2025
  • Host a static website on AWS with Amazon S3 and Route 53
    • June 27, 2025
  • Prioritize security from the edge to the cloud
    • June 25, 2025
  • 6 edge monitoring best practices in the cloud
    • June 25, 2025
  • Genome 5
    AlphaGenome: AI for better understanding the genome
    • June 25, 2025
  • 6
    Pure Accelerate 2025: All the news and updates live from Las Vegas
    • June 18, 2025
  • 7
    ‘This was a very purposeful strategy’: Pure Storage unveils Enterprise Data Cloud in bid to unify data storage, management
    • June 18, 2025
  • What is cloud bursting?
    • June 18, 2025
  • 9
    There’s a ‘cloud reset’ underway, and VMware Cloud Foundation 9.0 is a chance for Broadcom to pounce on it
    • June 17, 2025
  • What is confidential computing?
    • June 17, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • Oracle adds xAI Grok models to OCI
    • June 17, 2025
  • Fine-tune your storage-as-a-service approach
    • June 16, 2025
  • 3
    Advanced audio dialog and generation with Gemini 2.5
    • June 15, 2025
  • Google Cloud, Cloudflare struck by widespread outages
    • June 12, 2025
  • 5
    Global cloud spending might be booming, but AWS is trailing Microsoft and Google
    • June 13, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.