Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for 6 ways minimalism can help you write clean code
Paula Santamaría
Paula Santamaría

Posted on • Edited on

     

6 ways minimalism can help you write clean code

Introduction

In this article I'll share how you can applyminimalism concepts in code to make it clean and be more productive.

If you already know what minimalism is and want to go straight to the pointclick here and you'll be redirected to the first of six examples in this article of how Iapply minimalism in code.

Around three years ago I was working on a software development company. I’d been working there for 5 years, and it was slowly becoming less and less fulfilling for many reasons (unrelated to this article). I wanted to resign and try freelancing, but didn’t know where to begin.

I started asking for help online and someone recommended me to watchMinimalism: A Documentary About the Important Things.

But, what is minimalism

Well, in the words ofthe minimalists themselves:

Minimalism is a lifestyle that helps people question what things add value to their lives. By clearing the clutter from life’s path, we can all make room for the most important aspects of life: health, relationships, passion, growth, and contribution.

After embracing minimalism I found it to be more than just a way to save money by living with less. It teaches us how tolive intentionally, focusing on what we really care. It helps us look past other's opinions and the pressure of "what's expected" of us. But, most importantly, it encourages you toconsider the value of things before incorporating them into your life.

I believe minimalism has also helped me be more productive and a better dev: Writing and committingonly valuable code makes the code cleaner, easier to read and maintain and helps me make a better use of my time.

Here are some examples ofhow I apply minimalism in code:

1. Avoiddependency clutter

Or as I like to call it: Think before younpm install

There're some great libraries/modules/packages out there that solve a lot of issues and save us time. There's nothing wrong with relaying on them, butshould we include a library if we're only going to use it once or twice in the whole project?

Every new dependency we add to our projects has the following effects:

  • Increases the size of the project
  • You (and your team) have to learn how to properly use the new dependency
  • Regular updates may be required (to solve a security issue, for example). This also means you'll have to thoroughly test the project with every update, and even do some refactoring.

Also, you should spend some time researching the package to make sure is reliable, secure, up to date, etc.

I'm not saying you should avoid using external libraries at all costs, but you should definitelythink before doing so.

2. Commented-out code rarely has value

(thanks@mikeschinkel for the title suggestion)

You're changing how a particular block of code works, so you start commenting some old lines and writing new ones. You test it, works ok, so you commit...no!

This is something I see a lot, and I believe it comes from thefear of not being able to "go back".You shouldn't feel insecure about code you're committing & pushing. If you reached that point it means you already tested the changes enough, so why keep the old code? And even if you really need to go back, you could just look at your git history to find it.

Commented code is just noise: It's not useful to the software, it's distracting to the people reading your code, and it's just not pretty. Get rid of it!

3. Less is more: Don’t write code “just in case”

Sometimes we make the mistake of getting ahead of ourselves and start writing code that, we think, may be useful in the future. As with commented code, we're just adding noise in exchange of thepossibility of it to eventually become valuable.

Let's say that, for example, you're working on the login feature for your website. So you write a class calledUserService that contains a method calledLogin.

And then you say to yourself "As long as I'm working on this class, I may as well add a method that searches users by name. It'll probably be useful for the next Sprint". What's the problem here? Well:

  • Maybe the "search users" feature never comes up, so you’re just losing time.
  • You're adding unrelated code to your commit/branch/PR. This makes itharder to understand for code reviewers and/or PR reviewers.
  • This code requires testing. Would you spend even more time adding and executing the required tests or just leave the code untested? Both options sound bad.

So if you wait for the feature to be required instead of adding the code "just in case" you'd probably be making a better use of your time (and your team's).

4. Challenge your ideas

Minimalism is aboutquestioning things. Don't do stuff just because "we have always done it this way". Challenging ideas will help you get a better understanding of them and probably find better solutions or even issues with previous approaches.

Of course, there’re times to question things and there’re times to let things go to be able to move forward fast. Don’t get caught inanalysis paralysis trying to question every little thing.

5. Take advantage of what you already have

Or what we callreusability. In software development is desirable for code to be reusable: it saves time, makes the code cleaner and easier to maintain, etc.

Sometimes is tempting to start a feature from scratch... play with new code, even try to improve what’s already done. It may be ok to do that in some cases, but be aware of the advantages of reusability and thecosts of writing new code: possible bugs, more time spent building, documenting and testing the feature, you may have to introduce the team to the new solution, etc.

6. Avoid "shiny object" syndrome

There are features that are more fun to build, but we need to focus on what's important: what do our users need the most?

The priority should be ourusers’ needs, regardless of how fun or interesting a feature might be. Be aware of your own bias and try not to be influenced by it.

Some resources

If you're interested in getting into minimalism here are a few resources that may help you get started:

Top comments(47)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
kenovienadu profile image
Ovienadu Ken
Software Engineer at 21CTL
  • Email
  • Location
    Lagos, Nigeria
  • Work
    Full Stack Engineer at 21st Century Technologies
  • Joined

It's a beautiful piece. You're very correct. Commented code looks ugly and makes the code difficult for others to read and understand.

You mentioned the dependency clutter. But then you also encourage code reusability. Can you please clarify on this.

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined
• Edited on• Edited

Thank you Ken!

When I was talking about reusability I was referring mostly to reusing your own code. Like, for example, don't write the same function twice with a different name.
But I'm also in favor of reusing code through dependencies, as long as the cost of integrating that dependency is worth taking.
Like with any form of minimalism, there's no "one size fits all", it's about being aware of the cost vs value ratio in your particular case.

CollapseExpand
 
kenovienadu profile image
Ovienadu Ken
Software Engineer at 21CTL
  • Email
  • Location
    Lagos, Nigeria
  • Work
    Full Stack Engineer at 21st Century Technologies
  • Joined

🤗🤗🤗 then I couldn't agree with you more

CollapseExpand
 
theweeappshop profile image
Tony Ross
Been a developer for 25+ years. Now a Tech Manager for a med-tech startup plus run my own successful app-dev business.
  • Location
    Nigg, Scottish Highlands
  • Work
    Developer at The Wee App Shop
  • Joined

Disagree about commented code; but there's a time and a place for it. If you have the capability to minify your code, then comments have no place. However, in the overall readability/understandability of a large system, and to assist with debugging, comments are invaluable.

Great article though.

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

I may have explained myself poorly on that point. I meant thatcommented-out code has no value. I'm in favor of comments that explain the purpose of code, as long as they add additional value.

Thread Thread
 
theweeappshop profile image
Tony Ross
Been a developer for 25+ years. Now a Tech Manager for a med-tech startup plus run my own successful app-dev business.
  • Location
    Nigg, Scottish Highlands
  • Work
    Developer at The Wee App Shop
  • Joined

Ah, my mis-understanding Paula. Totally agree with you then.

Thread Thread
 
moopet profile image
Ben Sinclair
I've been a professional C, Perl, PHP and Python developer.I'm an ex-sysadmin from the late 20th century.These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
  • Location
    Scotland
  • Education
    Something something cybernetics
  • Pronouns
    They/them
  • Work
    General-purpose software person
  • Joined

Commented-out code may sometimes - very rarely - have value, but usually as a warning not to do something. Like in a function document string, something saying "this used to be done like so, but there's a gotcha, so don't refactor it back".

CollapseExpand
 
mauro_codes profile image
Mauro Garcia
Entrepreneur, Fullstack developer, Minimalist. Passionate about learning and trying new technologies
  • Location
    Buenos Aires, Argentina
  • Joined

Here are some additional resources about minimalism that I found useful:

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

I forgot to includeGet rid of it 🤦‍♀😂, the app that we build and was on the US news!!

Paula Santamaría profile image
Paula Santamaría
@pauxdsantamaria
twitter logo
The app@maurogarcia_19 and I built was on the news!twitter.com/AppGetridofit/…
22:52 PM - 11 Feb 2019
Get rid of it @AppGetridofit
A few days ago we were chosen "app of the day" by KTVN Channel 2 News @KTVN! Thank you all for your support!https://t.co/bYDcGZZzTZ
Twitter reply actionTwitter retweet action 0Twitter like action 4
CollapseExpand
 
shymi profile image
shymi
A honking dev, who tries not to burn out again.
  • Location
    Sofia(BG)
  • Joined

I love the 3-rd point. A while ago we had to deliver to a client a minimal part of the product, which was rewritten from 0. The first thing, that our architect told us, was "Don't try to guess what will be the next requirement. Just cover the current ones". Working in a software company can be a dynamic job and no one knows what will be the next big thing, that the client would have interest in.

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined
• Edited on• Edited

Thanks for stopping by, Shymi!

Completely agree. It's hard enough to complete the required work on time, it'll be even harder if we keep adding requirements ourselves! 😅
Sometimes we think that the extra-work will be appreciated by the client, but it usually leads to incomplete features that give the impression of instability.

CollapseExpand
 
codemouse92 profile image
Jason C. McDonald
Author. Speaker. Time Lord. (Views are my own)
  • Email
  • Location
    Time Vortex
  • Pronouns
    he/him
  • Work
    Author of "Dead Simple Python" (No Starch Press)
  • Joined
• Edited on• Edited

Excellent tips! I follow these myself.

My only addition...

Commented code is just noise: It's not useful to the software, it's distracting to the people reading your code, and it's just not pretty. Get rid of it!

I'd apply that only to comments describingwhat the code is andhow it does it. Commenting intent ("why") has proven to be a massive time-saver for both myself and anyone else reading or working on my code.More about that here.

With that said, what you described with commenting is otherwise spot-on.

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

Couldn't agree more. Comments explaining the purpose of the code are totally ok, as long as they provide value (which I believe they do). I even wrote an article about writing comments to document your JS code using JSDoc.

Thanks for your comment, Jason! 🙏

CollapseExpand
 
mikeschinkel profile image
Mike Schinkel
  • Joined

Great article, agree with it all.

One comment through; your title for #2 made me think you were arguing tonot comment code rather than what I realized were you arguing after reading closely; i.e. not leaving commented-OUT code in your commits.

Maybe consider changing the title to(I reworded to keep it short?):

"Commented-out code rarely has value"

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

You're absolutely right. Your suggestion is clearer and sounds better too, I'll change it. Thank you, Mike!

CollapseExpand
 
dimitriszx profile image
Dimitris Zarachanis
  • Location
    Athens, Greece
  • Work
    Front-End Developer
  • Joined

Some really great tips here! I plead guilty of leaving commented code just in case. Also, I have never expected to find Matt d'Avella here 😂

CollapseExpand
 
mauro_codes profile image
Mauro Garcia
Entrepreneur, Fullstack developer, Minimalist. Passionate about learning and trying new technologies
  • Location
    Buenos Aires, Argentina
  • Joined

Did you mean Mustache Matt? :D

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

dramatically disappointed

That's ok, just promise me you won't do it again 😂

And yeah, Matt is awesome!

CollapseExpand
 
pierreturnbull profile image
Pierre Turnbull
Just another dev.
  • Location
    Paris
  • Work
    Fullstack JS dev at Paris
  • Joined
• Edited on• Edited

"Don't do stuff just because "we have always done it this way""

I would add this:

"Don't do stuff just because "we have always done it this way", or because "everyone else does""

Although an idea's popularity can be a useful indicator of its relevance, it isnever a proof that it is ideal (or worse, ideal inyour specific case).
This is a mistake that some people do, probably to avoid thinking, and it sometimes causes them to make terrible decisions.

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

That's right, mindfulness all the way!

CollapseExpand
 
xwero profile image
david duymelinck
Learned to code in the wild west time of php 4, also the time xml and xpath where the new hot thing.
  • Location
    Belgium
  • Joined
• Edited on• Edited

Commented code is a thing from the time before version control solutions.
I stopped doing it when i started using subversion.
But there are codebases I inherit from other developers. Most of the time I just remove it when I start working with the code in one commit.

There are rare occasions where commented code is a convenience. In those cases I add the comment tag KEEP_CODE followed by the date. I do a periodical check for this tag and get a report for code that is older than a month. That is my expiry date for commented code.

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

That's a nice tip. Last week I was looking for an ESLint rule to detect commented code and I run into aGithub issue where they discussed that there're some cases where commented out code may be useful, like code usage examples. I have to agree that it's not a black or white thing.

CollapseExpand
 
xwero profile image
david duymelinck
Learned to code in the wild west time of php 4, also the time xml and xpath where the new hot thing.
  • Location
    Belgium
  • Joined

The case that comes to mind at the moment is when the client is a/b testing features and they are not sure if they want to keep it of remove it after the testing. When my report comes in I mail them about it, and most of the time they will have a definite decision.

The only time I would put code usage examples in commented code, is when the project has a comments to documentation parser likeJSDoc

CollapseExpand
 
ical10 profile image
Husni Rizal
Love to create things. Curious about tech and science. Into yoga and meditation.
  • Location
    Jakarta
  • Education
    Université de Paris - Master of Earth Science
  • Work
    Full-stack JavaScript Developer
  • Joined

Hi. I came here after watching Less Is Now on Netflix.
A well-constructed article indeed. I agree with your points there. I'm only in my first year as a software dev so I consider myself lucky discovering this quite early during my career.

Can't wait to read your next pieces!

CollapseExpand
 
enriqueedelberto profile image
Edelberto Enrique Reyes
I am a software engineer with 6 years of experience using .Net and Angular for projects.
  • Location
    Colombia
  • Education
    Bachelor Systems Engineer
  • Work
    Software engineer
  • Joined

Thanks a lot for your post.
For me, the best part is "Don't write code just in case", because sometimes we think that something can help you in the future, but, you add more unnecessary code.

CollapseExpand
 
paulasantamaria profile image
Paula Santamaría
Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

Thank you!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Passionate about creating stuff. Gamer, digital artist and guitarist on my free time. She/her
  • Location
    Buenos Aires, Argentina
  • Education
    IT Management Degree from UADE (Argentine University of Enterprise)
  • Work
    Freelance
  • Joined

More fromPaula Santamaría

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp