Cleaner Code: 3 Ways You Can Write Cleaner Code

Coding with Lewis · Beginner ·📰 AI News & Updates ·4y ago
Skills: API Design60%

Key Takeaways

Writes cleaner code using common methods and best practices

Full Transcript

how many times do you write code one day and then come back to it later just for it to be unmaintainable or maybe you're just a beginner or intermediate dev looking to write cleaner code programming is a creative art form that's a hill i will fight and die on just like other art forms there's always ways you can improve your craft a code base is like a garden you want to set it up for success and tend to it when it needs you don't want to dread ever going back into it here's some ways you can write cleaner code my examples are in python and javascript but none of these are opinionated whatsoever and you can do it in whatever language you choose this is just things that help me on my programming journey but whatever you do to keep your code clean let me know in the comments i'd love to know comments and naming conventions you've probably seen or been a part of debates about commenting your code online some say goodcode is self documenting you've also probably read code that probably could have skipped on the comment i think there's a sweet spot between no comments and comments galore i think it's also important to ask yourself what your intentions with your comments are this article from free code camp makes a really good distinction between two types of comments documentation comments and clarification comments documentation comments as long comments you would write if you intend your users to use your source code at some time this is usually to help contributors or people consuming your api in some sense usually this is long and drawn out but on purpose the second type is clarification comments you're more likely to see or use these types of comments in your own source code when you want to remember something or refactor the code at a later point what i tend to do is a mix of both some code will need clarification for reading sakes and functions or classes would heavily benefit from a doc string explaining what it does and the parameters it might take in where good code is self documenting is within its naming conventions if i had code that had comments with poor naming convention you can see how it's even harder to read this code than it would be if i did something with how i named everything when naming variables or functions or classes it's important to be very explicit with what that thing represents or does this will eliminate your need for writing redundant comments when it comes to writing good comments ellen spurtus of the stack overflow blog has an amazing article giving a detailed look into the best and worst practices for writing code comments i recommend you read it but i think the perfect way to sum it all up is number three which is if you can't write a clear comment there may be a problem with your code try and make your code clear using naming conventions that are easy to read without comments then use comments as a secondary way to add context if it's still a little bit unclear after that maybe it's time to tidy your code up a little bit oh and don't comment out your code just delete it it's in version control anyway guard clauses if statements are the bread and butter of making smart code however it's never just one simple if this then that it's usually something along the lines of if this then that else this then that then this elsa there is nothing inherently wrong with this type of setup but it causes gigantic headaches when you have to look into a nested condition to add another condition elsewhere elsewhere get it if i had this renew membership code right here well you can see how adding a condition onto this at a later point is pretty tough so they are subscribed they are a lifetime member okay what if they're not okay what if they're under 18 or over 18 this hurts my head you have to use your cursor to keep track of what nested if statements you're in this just makes things really really tough for you game developers out there i know especially you know the struggle however guard clauses make this much easier guard clauses allows you to structure your code much easier so you can read it from top to bottom so when you write your code you test for conditions first if they come back negative you simply return or throw an exception the benefit of this is that it allows you to easily come back to your code and update it accordingly no more using your cursor as some sort of makeshift bookmark device has an even more lengthier article about this showing how you can use guard clauses in an even leaner way by making a guard clause its home function it's pretty impressive actually as long as you can get this simple concept of using guard clauses to improve your functions this will really make your code so much easier to come back to single responsibility principle and long functions a jack of all trades is a master of none who even said that and i'm pretty sure that's only like half of the quote there's just been so many times where i just jumped back into an old code base saw that function name and just cringed because i knew it was going to be a tough time to deal with why because it's like a thousand lines long a principle to follow is the single responsibility principle by robert martin which defines that there should never be more than one reason for a class to change in other words every class should have only one responsibility thanks wikipedia for that little tidbit this principle thrives when you're in the debugging stage errors are going to happen more often when you give a class or function multiple things to work on however if you let it only do one thing then you're more likely to isolate the issue that is happening let's say i wanted to build an application where i generate an email and send it off to somebody realistically the scope of this is small so i could easily put it into one function as the scope of my application changes and grows like generating cool pictures for my emails i now am finding unexpected errors and don't really know who to blame for it there is no way that i will be taking responsibility for my actions this way of thinking is really important if you have really long functions or classes which is also where this principle helps with in this instance i could reduce the application into two parts one can be generating the email and the other could be sending the email by creating a generate email and send email function it's easier to add on extra functionality at a later date also when things go wrong they always do i know who to blame it is always send email keep your functions and classes short it makes it easier to digest and maintain these three tips are just some ways you can improve your code trust me there's a lot more thanks so much for watching this video guys i am having a lot of fun doing these long-form videos i just got back from vacation you can check it out on my instagram but when i got back i got back to 40 000 subscribers like that is just mind-boggling 40 000 subscribers holy crap i i can't thank every single one i read every single one of your comments and thank you so much to every single one of you i i can't thank you enough like i know it sounds cheesy but i you guys make my day i also started a discord channel so if you're interested in talking code talking about content videos whatever it might be check the link in the description thanks for watching let me know if you want to see more code tips

Original Description

Here are 3 ways you can write cleaner code using common methods you can see online. 🧑‍💻 Writing Cleaner Code will make help you maximize productivity and readability of your code when you decide to go back into it at a later point. Let me know what tips you guys use in the comments. —- 💬 JOIN MY DISCORD https://discord.com/invite/bY6N9z26et —- CONNECT WITH ME ON SOCIAL 📸 Instagram: https://www.instagram.com/lewismenelaws/ 🎚 TikTok: https://www.tiktok.com/@lewismenelaws 🐣 Twitter: https://twitter.com/LewisMenelaws —- 0:00 Intro 0:45 Comments and Naming Conventions 3:06 Guard Clauses 4:53 Single Responsibility Principle and Long Functions 3:50 Web3 & Metaverse 6:53 Outro & Discord Announcement —- 🎵 Music Misha, Jussi Halme - Higher https://chll.to/a705fd1d —- ⛓️ Links in Video Free Code Camp - Article about comments: https://www.freecodecamp.org/news/code-comments-the-good-the-bad-and-the-ugly-be9cc65fbf83/ Ellen Spertus on code comments best practices - https://stackoverflow.blog/2021/12/23/best-practices-for-writing-code-comments/ DevIQ - Guard Clauses - https://deviq.com/design-patterns/guard-clause Single Responsibility Principle - https://en.wikipedia.org/wiki/Single-responsibility_principle
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Coding with Lewis · Coding with Lewis · 0 of 60

← Previous Next →
1 Converting your Callback Functions into Async/Await Functions in under 3 minutes! (React 16)
Converting your Callback Functions into Async/Await Functions in under 3 minutes! (React 16)
Coding with Lewis
2 Solution to Paul McWhorter’s Arduino Tutorial 3 Homework.
Solution to Paul McWhorter’s Arduino Tutorial 3 Homework.
Coding with Lewis
3 This Open Source project helps you build DeFi apps faster. #defi #ethereum #programming
This Open Source project helps you build DeFi apps faster. #defi #ethereum #programming
Coding with Lewis
4 This open source database is incredible 🤯 #opensource #technology #programming #database
This open source database is incredible 🤯 #opensource #technology #programming #database
Coding with Lewis
5 This Chrome extension will boost your productivity dramatically. #tech #opensource #productivity
This Chrome extension will boost your productivity dramatically. #tech #opensource #productivity
Coding with Lewis
6 This will help you get a job in tech 👩‍💻 #opensource #programming #career #productivity
This will help you get a job in tech 👩‍💻 #opensource #programming #career #productivity
Coding with Lewis
7 Shopify released a new JavaScript framework #shopify #ecommerce #opensource #technology
Shopify released a new JavaScript framework #shopify #ecommerce #opensource #technology
Coding with Lewis
8 This book is a MUST read for all developers. #technology #programming #productivity #books
This book is a MUST read for all developers. #technology #programming #productivity #books
Coding with Lewis
9 How to create AI without learning code. #artificialintelligence #programming #technology
How to create AI without learning code. #artificialintelligence #programming #technology
Coding with Lewis
10 How to build cross compatible mobile apps📱#technology #programming #opensource #productivity
How to build cross compatible mobile apps📱#technology #programming #opensource #productivity
Coding with Lewis
11 This app lets you see if your local McDonalds Ice Cream machine is broken. #programming #technology
This app lets you see if your local McDonalds Ice Cream machine is broken. #programming #technology
Coding with Lewis
12 Make your websites look like their from Windows 98 #technology #opensource #career #webdev
Make your websites look like their from Windows 98 #technology #opensource #career #webdev
Coding with Lewis
13 How do barcodes work? #technology #programming #explained
How do barcodes work? #technology #programming #explained
Coding with Lewis
14 This AI creates realistic art #technology #opensource #programming #artificialintelligence
This AI creates realistic art #technology #opensource #programming #artificialintelligence
Coding with Lewis
15 What is AWS? (Amazon Web Services) #technology #programming #artificialintelligence #computing
What is AWS? (Amazon Web Services) #technology #programming #artificialintelligence #computing
Coding with Lewis
16 Svelte in 15 seconds #programming #javascript #webdevelopment #opensource
Svelte in 15 seconds #programming #javascript #webdevelopment #opensource
Coding with Lewis
17 This game engine is essential if you want to create a video game #opensource #gamedev #programming
This game engine is essential if you want to create a video game #opensource #gamedev #programming
Coding with Lewis
18 How to start a tech company for FREE #technology #opensource #programming #business
How to start a tech company for FREE #technology #opensource #programming #business
Coding with Lewis
19 Every programmer should know this. #opensource #programming #softwareengineer
Every programmer should know this. #opensource #programming #softwareengineer
Coding with Lewis
20 This will make you a full stack developer #softwareengineer #technology #opensource
This will make you a full stack developer #softwareengineer #technology #opensource
Coding with Lewis
21 This is how to become a better developer this holiday season #technology #programming
This is how to become a better developer this holiday season #technology #programming
Coding with Lewis
22 This AI adds color to famous photos… #technology #opensource #programming #artificialintelligence
This AI adds color to famous photos… #technology #opensource #programming #artificialintelligence
Coding with Lewis
23 This is the future of NFTs. #technology #cryptocurrency #softwaredeveloper #nft
This is the future of NFTs. #technology #cryptocurrency #softwaredeveloper #nft
Coding with Lewis
24 This is a great way to learn code if you’re Canadian 🇨🇦 #learntocode #programming #software
This is a great way to learn code if you’re Canadian 🇨🇦 #learntocode #programming #software
Coding with Lewis
25 This is needed before you deploy your application 👩‍💻. #technology #opensource #programming
This is needed before you deploy your application 👩‍💻. #technology #opensource #programming
Coding with Lewis
26 Internet Explorer is FINALLY going away! #technology #software #productivity
Internet Explorer is FINALLY going away! #technology #software #productivity
Coding with Lewis
27 There’s a competitive community… in Excel? #technology #productivity #excel
There’s a competitive community… in Excel? #technology #productivity #excel
Coding with Lewis
28 These tech trends will dominate 2022. #technology #programming #business
These tech trends will dominate 2022. #technology #programming #business
Coding with Lewis
29 This chrome extension shows you what tech websites use. #opensource #technology #programming
This chrome extension shows you what tech websites use. #opensource #technology #programming
Coding with Lewis
30 This open source project will improve your design skills #technology #programming #softwareengineer
This open source project will improve your design skills #technology #programming #softwareengineer
Coding with Lewis
31 How to get thousands of programming books for FREE 📚 #technology #programming #opensource #books
How to get thousands of programming books for FREE 📚 #technology #programming #opensource #books
Coding with Lewis
32 This AI will help you write better… #artificialintelligence #opensource #programming
This AI will help you write better… #artificialintelligence #opensource #programming
Coding with Lewis
33 WTF is Django? (In 15 seconds) #django #technology #programming #business
WTF is Django? (In 15 seconds) #django #technology #programming #business
Coding with Lewis
34 This is what happens behind the scenes when you visit a website. #technology #programming #software
This is what happens behind the scenes when you visit a website. #technology #programming #software
Coding with Lewis
35 This terminal will BOOST your productivity 💪 #programming #technology #productivity #opensource
This terminal will BOOST your productivity 💪 #programming #technology #productivity #opensource
Coding with Lewis
36 This will make you a better programmer. 👩‍💻 #programming #technology #opensource #productivity
This will make you a better programmer. 👩‍💻 #programming #technology #opensource #productivity
Coding with Lewis
37 These are the BEST programming stickers to have. #programming #software #technology #opensource
These are the BEST programming stickers to have. #programming #software #technology #opensource
Coding with Lewis
38 3 High Paying Tech Jobs that will DOMINATE 2022 #career #programming #technology #software
3 High Paying Tech Jobs that will DOMINATE 2022 #career #programming #technology #software
Coding with Lewis
39 This JavaScript library will make your websites look MUCH better. #technology #programming
This JavaScript library will make your websites look MUCH better. #technology #programming
Coding with Lewis
40 WTF is ReactJS (in 15 seconds)?! #technology #programming #react #javascript #opensource
WTF is ReactJS (in 15 seconds)?! #technology #programming #react #javascript #opensource
Coding with Lewis
41 This open source project will make you an EXPERT at Node.JS #technology #programming #opensource
This open source project will make you an EXPERT at Node.JS #technology #programming #opensource
Coding with Lewis
42 This will help you learn to code in 2022! #technology #programming #opensource #productivity
This will help you learn to code in 2022! #technology #programming #opensource #productivity
Coding with Lewis
43 This will help you be a programmer in 2022 👩‍💻 #technology #programming #opensource #business
This will help you be a programmer in 2022 👩‍💻 #technology #programming #opensource #business
Coding with Lewis
44 Every developer should know this 👩‍💻 #technology #devops #technology #opensource #productivity
Every developer should know this 👩‍💻 #technology #devops #technology #opensource #productivity
Coding with Lewis
45 STOP writing CSS and use this instead 👩‍💻 #technology #programming #opensource #webdevelopment
STOP writing CSS and use this instead 👩‍💻 #technology #programming #opensource #webdevelopment
Coding with Lewis
46 If you want to learn Machine Learning in 2022, start here. #technology #programming #software
If you want to learn Machine Learning in 2022, start here. #technology #programming #software
Coding with Lewis
47 This will help you ACE your tech interview 👩‍💻 #technology #opensource #programming #business
This will help you ACE your tech interview 👩‍💻 #technology #opensource #programming #business
Coding with Lewis
48 This is how to decide what programming language to learn. 👩‍💻 #technology #programming #software
This is how to decide what programming language to learn. 👩‍💻 #technology #programming #software
Coding with Lewis
49 Learn these programming languages if you want to learn to code. 👩‍💻 #technology #programming
Learn these programming languages if you want to learn to code. 👩‍💻 #technology #programming
Coding with Lewis
50 Will Artificial Intelligence replace programmers?! 👩‍💻 #technology #programming #software
Will Artificial Intelligence replace programmers?! 👩‍💻 #technology #programming #software
Coding with Lewis
51 This will make you a better programmer 👩‍💻 #technology #programming #opensource #software
This will make you a better programmer 👩‍💻 #technology #programming #opensource #software
Coding with Lewis
52 This is how to make your terminal look like a pro #technology #programming #software #opensource
This is how to make your terminal look like a pro #technology #programming #software #opensource
Coding with Lewis
53 This roadmap will help you become a front end developer 👩‍💻 #programming #technology #software
This roadmap will help you become a front end developer 👩‍💻 #programming #technology #software
Coding with Lewis
54 Follow this roadmap to be a backend developer in 2022 👩‍💻 #technology #programming #software
Follow this roadmap to be a backend developer in 2022 👩‍💻 #technology #programming #software
Coding with Lewis
55 This roadmap will help you become a data scientist in 2022👩‍💻 #datascience #developer #programming
This roadmap will help you become a data scientist in 2022👩‍💻 #datascience #developer #programming
Coding with Lewis
56 This is how to decide if you should learn JavaScript #programming #technology #javascript #webdev
This is how to decide if you should learn JavaScript #programming #technology #javascript #webdev
Coding with Lewis
57 Use this to help you start a programming project. #programming #technology #opensource #software
Use this to help you start a programming project. #programming #technology #opensource #software
Coding with Lewis
58 This roadmap will help you become a DevOps engineer in 2022. #devops #software #programming
This roadmap will help you become a DevOps engineer in 2022. #devops #software #programming
Coding with Lewis
59 These non-tech skills are essential for programmers 👩‍💻 #technology #programming #software
These non-tech skills are essential for programmers 👩‍💻 #technology #programming #software
Coding with Lewis
60 Learn these languages if you want to code in 2022 👩‍💻 #programming #technology #software
Learn these languages if you want to code in 2022 👩‍💻 #programming #technology #software
Coding with Lewis

Related AI Lessons

Chapters (6)

Intro
0:45 Comments and Naming Conventions
3:06 Guard Clauses
4:53 Single Responsibility Principle and Long Functions
3:50 Web3 & Metaverse
6:53 Outro & Discord Announcement
Up next
‘ENOUGH IS ENOUGH’: Lebanon is STANDING UP to Iran, expert says
Fox Business
Watch →