Тёмный

Cleaner Code: 3 Ways You Can Write Cleaner Code 

Coding with Lewis
Подписаться 566 тыс.
Просмотров 79 тыс.
50% 1

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
/ discord
--
CONNECT WITH ME ON SOCIAL
📸 Instagram:
/ lewismenelaws
🎚 TikTok:
/ lewismenelaws
🐣 Twitter:
/ 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 chll.to/a705fd1d
--
⛓️ Links in Video
Free Code Camp - Article about comments: www.freecodecamp.org/news/cod...
Ellen Spertus on code comments best practices - stackoverflow.blog/2021/12/23...
DevIQ - Guard Clauses - deviq.com/design-patterns/gua...
Single Responsibility Principle - en.wikipedia.org/wiki/Single-...

Наука

Опубликовано:

 

22 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 130   
@jimmea6317
@jimmea6317 2 года назад
People who say coding isn’t an art form are just salty they can’t do it themselves
@daniel-godin
@daniel-godin 9 месяцев назад
Really great video! I appreciate that you not only gave clear and concise information, you also referenced places we could learn more about these concepts. Then you put the actual links in the description. Thank you!!
@randyleberknight7049
@randyleberknight7049 10 месяцев назад
Excellent! This tutorial shows clear maturity and professionalism! Every beginner should trust this guy! And we can all learn here, or reinforce what we may have learned already.
@ziakhan-tk7rk
@ziakhan-tk7rk 2 года назад
Well finally a video where I can watch from the starting till the end without even getting bored for a second I hope this continues for all your videos.
@CodingWithLewis
@CodingWithLewis 2 года назад
Glad you enjoyed it zia :)
@Volkskomissar
@Volkskomissar Год назад
NEVER seen this guy coding anything! I doubt that he is succesfull at coding. All i´ve learned from him is: if you cant make it, upload shorts with fotm buzz words and stock videos
@ziakhan-tk7rk
@ziakhan-tk7rk Год назад
@@Volkskomissar Well that doesn't mean that he can't code. No one would share or show code unless he is doing an open source project. He will have to make shorts like that only then people will watch.. People are interested in more content like this which drives curiosity and this is no different from other YT shorts. Everyone does it. It's upto him to show or not to show the code. May be you or I can request him to do some tutorials which can speak about his skills. By the way I am not doubting just telling
@Volkskomissar
@Volkskomissar Год назад
​@@ziakhan-tk7rk Thank you, that's a view I hadn't thought of at all, unfortunately. When I reflect on my statement, I realize that behind it is actually the following desire: "Your short videos are interesting I would believe your info more if you would back them up with tutorials or projects.
@dienekes4364
@dienekes4364 9 месяцев назад
Back in 2005, I wrote code as a contractor for a company that transmitted a set of data to an external server. It was okay, written in C# 1.2. A couple of years later, I was hired back by the same company for some other projects. The manager had been replaced and the new guy was complaining about my original code, saying that it was unmaintainable and every time someone touched it, it broke. They wanted it re-written in C# 2.0. Not only did I just do an in-place upgrade to the .EXE file, I added new features that weren't available in 1.2. I re-compiled and had it up and running within a couple of hours. I went back to that manager and told him that every person who said my code was unmaintainable should be fired on the spot because my code was clean and _EASILY_ maintainable. I have no idea who he had that "touched" it, but if they broke it, it was because of their lack of skill, not my code.
@malicious8909
@malicious8909 2 года назад
Good editing skills, keep it up. Thanks for all those shorts about open source projects too :)
@khalidsaifullahfuad
@khalidsaifullahfuad 2 года назад
Really love your videos. You deserve way more subscribers. Best of luck
@laxmanadhikari3989
@laxmanadhikari3989 2 года назад
I started really loving your content. Thank you for all these I learned many things from you. I have a small request can you please make some short videos on how to create efficient database architecture for both SQL and Non-SQL databases.
@stevethackery9853
@stevethackery9853 9 месяцев назад
Coding is an art form, but it's also a rigorous engineering discipline. All the fundamentals - encapsulation, defined interfaces, modularity, resilience, reusability, error-checking, exceptions, etc - are pure engineering. Building on those engineering fundamentals is where pure art can be created.
@Eric-sy3dm
@Eric-sy3dm 9 месяцев назад
I loved watching your video. Those are some good lessons learned.
@methanesulfonic
@methanesulfonic 2 года назад
I'm just starting to learn to code and your channel is a goldmine, there's so much to learn. thanks a bunch!
@CodingWithLewis
@CodingWithLewis 2 года назад
Great to hear!
@user-kl4mc3ed5h
@user-kl4mc3ed5h 8 месяцев назад
great best guard clauses
@Yagamiar15
@Yagamiar15 2 года назад
Quality content man... Loved this
@iqbalibrahim4713
@iqbalibrahim4713 2 месяца назад
I love the second advice, will go read about it right now
@by_asilbek
@by_asilbek 11 месяцев назад
Thank u so much is amazing!
@ahmedmanhy9899
@ahmedmanhy9899 Год назад
here after one year and u r 340k subs, u deserve a lot Lewis all the support.
@oyleolduboyleoldu3677
@oyleolduboyleoldu3677 2 года назад
keep up the good work man I really appreciate your videos 🙏
@CodingWithLewis
@CodingWithLewis 2 года назад
Thanks so much :D
@Aura_Mancer
@Aura_Mancer 2 года назад
Watching the video right now. I'm a beginner, and the guard clauses was very needed! Thank you
@CodingWithLewis
@CodingWithLewis 2 года назад
Pleasure is mine ☺️
@walaamohamed2067
@walaamohamed2067 11 месяцев назад
Amazing and clear one !
@dglasswine
@dglasswine 10 месяцев назад
Short, focused and really helpful
@hankobrat
@hankobrat 2 года назад
Thanks for the good tips and sources. Keep them coming. 🙂
@CodingWithLewis
@CodingWithLewis 2 года назад
Thanks hankobrat :D
@mansoorsoomro8585
@mansoorsoomro8585 2 года назад
Dude, Guard clauses have made my life so much easier. Great Video!
@CodingWithLewis
@CodingWithLewis 2 года назад
Glad they helped! THey really helped me!
@GfoxSim
@GfoxSim 8 месяцев назад
Very very very useful.
@Zeioth
@Zeioth 10 месяцев назад
100% on the spot. Recommended.
@emmanuelgenga7421
@emmanuelgenga7421 2 года назад
This is an awesome video... I have issues with hard to read code for projects...
@RobelGetnet1997
@RobelGetnet1997 2 года назад
good stuff! I'm a Game Dev, was kinda spooked when you called me out at Guard Clauses 😅 but I've been implementing them for a while, thanks for letting me know what they're called
@CodingWithLewis
@CodingWithLewis 2 года назад
Good stuff!
@xX12VincEXx
@xX12VincEXx 2 года назад
Really great video. Helped a lot. Please make more of the longer type videos :).
@CodingWithLewis
@CodingWithLewis 2 года назад
Would love to :) Thanks for watching!
@Anna36936
@Anna36936 2 года назад
Your videos are absolutely amazing, informative, helpful and so entertaining as well. I can't thank you enough!
@Jabberwockybird
@Jabberwockybird 10 месяцев назад
I like guard clauses, but 4:18 broke the logic of the original code.
@PeterPankowski
@PeterPankowski 10 месяцев назад
Wow…extreme helpfull
@CodingWithLewis
@CodingWithLewis 2 года назад
Thanks for waiting everyone! Let me know what you think :) More frequent videos coming now that I am back from vacation 🌴
@emmanuelgenga7421
@emmanuelgenga7421 2 года назад
Also if you have any advice on coding for a 2nd year student I'd be glad to hear from you.. thanks
@devkishore1388
@devkishore1388 2 года назад
As always, helpful video
@CodingWithLewis
@CodingWithLewis 2 года назад
Glad you think so! :)
@bsummer
@bsummer 2 года назад
0:25 Love it. LOL
@BlackdestinyXX
@BlackdestinyXX 2 года назад
Love your editing style! Keep make videos!
@CodingWithLewis
@CodingWithLewis 2 года назад
Thanks so much Blackdestiny! :D
@arkandash5895
@arkandash5895 2 года назад
Thanks, this video helps me out.
@CodingWithLewis
@CodingWithLewis 2 года назад
I’m glad it helped 😀
@reddaxtor5662
@reddaxtor5662 2 года назад
I have found a really great channel. It feels like found a mentor.
@CodingWithLewis
@CodingWithLewis 2 года назад
Glad you enjoy ☺️
@md.mohiuddinmaruf7737
@md.mohiuddinmaruf7737 4 месяца назад
Great video, Make a playlist on SOLID Principles in detail.
@danielleblanc5923
@danielleblanc5923 10 месяцев назад
The guard tests (or functions) implement the "fail fast" principle. If the parameters are wrong fail as quickly as possible. But beware of complex "check everything" guard functions that could take a toll on performance. If performance is really an issue, you have to find the right balance by separating "safe" internal functions and public APIs that need extra security. Generally speaking any time or effort skipped on design will be spent tenfold on debugging. If code looks messy it is because your thinking is ... messy or at least cluttered.
@navjotsingh2457
@navjotsingh2457 6 месяцев назад
ty
@andreasbr830
@andreasbr830 2 года назад
Thank you for the video. I think the length is perfect. It is a little Snack for in between and it is deep enough to improve your skills. The shorts are Unfortunately to short und of course not deep enough.
@blendermaster8934
@blendermaster8934 2 года назад
Amazing Video About Improving to Code!!
@CodingWithLewis
@CodingWithLewis 2 года назад
Glad you think so! :D
@MayukhSaikia
@MayukhSaikia 2 года назад
Awesome video. Learnt a lot. Specifically the guard clauses
@CodingWithLewis
@CodingWithLewis 2 года назад
Glad it was helpful Mayukh 😃 Thanks for watching!
@MayukhSaikia
@MayukhSaikia 2 года назад
@@CodingWithLewis Welcome. Please make a video on how to make unique projects for CS degrees/personal portfolio. Specifically I was searching for some 'multi-stack' projects like using two very different languages like big projects do. I dont know whether I am thinking right or wrong.
@CodingWithLewis
@CodingWithLewis 2 года назад
@@MayukhSaikia would love to!
@MayukhSaikia
@MayukhSaikia 2 года назад
@@CodingWithLewis Thanks
@dushalkumar
@dushalkumar Год назад
Which compilers we choose like something looks stylish i use turbo c++ but it's looks like any games to my mom think I'm paying game 😂
@tntredstone
@tntredstone 2 года назад
I like using cases/matches in python to make if statments readable
@liwordsonvijayabalan8387
@liwordsonvijayabalan8387 2 года назад
Great production value on this one. Best dev channel on RU-vid!
@CodingWithLewis
@CodingWithLewis 2 года назад
👑👑👑
@RogerKaptunik
@RogerKaptunik 10 месяцев назад
Thats funny. I just came from a video here which told me to remove these guard clauses
@gvijay2000
@gvijay2000 2 года назад
Lovely
@anderssjoqvist7816
@anderssjoqvist7816 10 месяцев назад
Is "def send_email(to_email, email): email.send(to_email)" a good example of clean code? Because it's still somewhat confusing to me. I would use different words for (email) address and (email) message but, on the other hand, I don't know the rules of clean code. Maybe we need to limit the number of words we introduce?
@keit99
@keit99 9 месяцев назад
Why not use void SendMail(Listreceivers, String Message) Or def send(receivers, message)
@rebok232
@rebok232 10 месяцев назад
when the class has more than 1 arg(or few smaller args) in kotlin then i always use the named arguments
@bob456fk6
@bob456fk6 10 месяцев назад
When nesting code, I find it very helpful to put a Number as a Comment for each bracket. Then it's easy to tell when the code within a set of brackets begins and ends. Example: { //10 lines of code here ; { //20 more lines of code here; } //20 { //30 lots of code here; } //30 } //10
@wardibald
@wardibald 10 месяцев назад
Much better to just extract each level of nested code into its own well named method.
@waitun3019
@waitun3019 Месяц назад
One complaint: Some clips are so short that I had to literally pause just to get the joke . Overall, Love it. subscribed
@pepperdayjackpac4521
@pepperdayjackpac4521 2 года назад
3:02 what is inversion control?
@CodingWithLewis
@CodingWithLewis 2 года назад
in "version control" aka git :) means it's in your code history
@kissgergo5202
@kissgergo5202 9 месяцев назад
4:25 this code doesn't do the same thing though, you never send the email notification in this version
@josepharoh7958
@josepharoh7958 3 месяца назад
Where's the discord?
@StarDust_2077
@StarDust_2077 4 дня назад
If you have to write more than 3 of statements just use switch or a map
@djtomoy
@djtomoy 10 месяцев назад
I like to leave some mystery and ambiguity in my code that way when it breaks (and it will!) I’m the only person who can fix it…more money for me 🤑!!!
@diogolsq5295
@diogolsq5295 3 месяца назад
if you have a clarifying comment, is a great sign, that you could do some simple function extraction with a meaningful name.
@HairyandFinanciallySolvent
@HairyandFinanciallySolvent 2 года назад
What looks clean imo depends in large part on the size of your monitor and structure of your IDE sections ... I have no clue how people do serious programming on laptops. I see people screen sharing and I'm at a loss
@CodingWithLewis
@CodingWithLewis 2 года назад
Lol yep! 😂. You could always zoom your code out a little bit but monitors are essential
@airman122469
@airman122469 10 месяцев назад
If you use a tight column count it’s actually really easy to program on a laptop. Also, the less you have to use the mouse, the better on a laptop. So basically, use neovim or another vim-like clone.
@keit99
@keit99 9 месяцев назад
​@@airman122469or just use actual vim.
@LudvikM
@LudvikM 4 месяца назад
There was a brilliant joke on How I Met Your Mother that I keep coming back to. Characters X and Y faced a tough decision, and their happy and exciting conclusion was "do you know who can take care of it? Future X and Y! YEAH!" Of course, at some future point they were like "damn X and Y from the past! Those guys are THE WORST". Well, that's the mindframe my team seems to be in when they produce code, with the exception that, instead of blaming themselves and learn from it when shit hits the fan, they USE how messed up things are to justify their lazy and shortsighted ways to produce MORE unmaintainable and buggy code. They'd watch this video and say "yeah, I wish our applications were like that, but at this point there's nothing that can be done... Now let me resume the making of our next set of bugs".
@Allium-Cepa
@Allium-Cepa 10 месяцев назад
solid, cyclomativ complexity, coding guidelines = everything you need to know
@camerobaird5939
@camerobaird5939 10 месяцев назад
Yeah, I almost never use comments but I think I split my code into smaller functions a bit too much.
@addictedtocode4929
@addictedtocode4929 Год назад
hi lewis, i wish that i had a feedback on your content, so i might be able to help you and (thank you) but, your contents are already amazing, and the only way you can improve, is to keep making contents ❤‍🔥❤‍🔥❤‍🔥
@TheOriginalJohnDoe
@TheOriginalJohnDoe 9 месяцев назад
Him: “I’ll use Python and JavaScript” Also him: uses PHP
@gingeral253
@gingeral253 10 месяцев назад
You said 40 thousand subscribers? More like 400 thousand.
@brockdaniel8845
@brockdaniel8845 6 месяцев назад
Hey uncle Bob. F CleanCode™ and DRY.
@thewelder3538
@thewelder3538 10 месяцев назад
If you use guard clauses, maybe it makes your code easier to read, but it certainly makes the code less performant. I'm not going to write a detailed explanation as to why because it requires understanding how branch prediction actually works. But like it or not, if/else will, in the majority of cases out perform using if statements to eliminate conditions. Every branch taken costs you a least a cycle and every if statement is going to end up as a branch instruction, or if you're lucky, maybe the compiler will make your code branchless, but good luck with that. Okay, so what's a few cycles here and there, eh? We're all friends here. Well, run that routine using those extra few cycles a few million times and now it's orders of magnitude slower. So whilst it may seem good to eliminate a few conditions right out of the gate, don't think of it as a panacea to solve your coding woes. Although that being said, breaking things down into smaller and smaller functions is a much MUCH worse idea, as all those stack frames have to be built and it's not free! Finally, DON'T if possible use exceptions either. That's another classic way to make your code run like dog shit. Treat an exception like it's name suggests, an Exception. An error is not an exception, an I/O failure is not an exception etc. etc.
@keit99
@keit99 9 месяцев назад
That's really only useful if you need Performance critical Code. (Like Video games, ...) otherwise it's fine to use.
@thewelder3538
@thewelder3538 9 месяцев назад
@@keit99 Well, I guess that it's kind of lucky that I write games for a living, then.... Look, I'm not saying that exceptions aren't useful, I'm saying that they really shouldn't be used for general error handling. If you think of what the actual meaning of the word exception is, it's kind of obvious. Performance does matter; if your application took 3 weeks instead of 5 seconds to produce results, should you care? Obviously this is an absurd example, and I'm not talking about hyper optimized hand crafted assembly, but performance always matters, just maybe not a few milliseconds here and there. But a good coding practice is going to serve you better than trying to optimize your code because it is taking 3 weeks instead of 5 seconds. Using exceptions where you should be using them, is one of these practices, with the added bonus that you get someone performance back too, although that's not generally your focus. Best way to understand the concept is to code in C where you can't use exceptions so that when you code in C++, you get good use of them.
@keit99
@keit99 9 месяцев назад
@@thewelder3538 fair enough. And I Think I may have missed the point. But yes Exceptions aren't great for error handling (unless the error could Lead to crashes). I kinda thought more of guard clauses (although I still would use them in Moderation)
@thewelder3538
@thewelder3538 9 месяцев назад
@@keit99 The problem is, this video starts out demonstrating these intrinsicly bad concepts and ideas, hence my comment. There are going to be people watching this who are learning and this video isn't going to help them. It's going to steer them down a road of bad practices that is going to cost serious money in the real-world when their code doesn't perform to expectations. I'm not saying that exceptions and guard clauses are bad, they serve a purpose, but it isn't the panacea to error handling that this video makes it out to be. These are the kind of lessons I've learned in 35+yrs of game development and some of them have been hard lessons too.
@keit99
@keit99 9 месяцев назад
@@thewelder3538 I've found that most "clean" Code advice goes in that direction
@HoD999x
@HoD999x 10 месяцев назад
1. comment what would otherwise be a "why would anyone do this?" 2. simpler is always better, minimize the number of things one has to keep in mind to understand the code 3. make sure test cycles are quick 4. forget all explicit principles
@Fluiding
@Fluiding 9 месяцев назад
5:03 this goes against java
@ZipClipChannel
@ZipClipChannel 10 месяцев назад
no matter how clean your code is, there's always that one nit-picky PR reviewer that literally complains about small nothings
@ninjazhu
@ninjazhu 10 месяцев назад
I would have chosen if ((age > 18) && (isActive === false)) { renew(); } because it is simple and doesn't warrant 7 lines of code. I haven't seen many situations though someone needs to be 19 years old for something...
@gabydewilde
@gabydewilde 10 месяцев назад
You can also chain them wiht &&'s !member.isSubscribed() && (member.age >= 18) && renew(); member.isSubscribed() && !member.isLifeTimeMember && member.sendEmailNotification(); I figure sinds we won't be sending 100 emails per second we can test member.isSubscribed() twice without issues.
@MiningForPies
@MiningForPies 9 месяцев назад
I have. Our code needs to handle adult offenders differently.
@PrometheusMMIV
@PrometheusMMIV 10 месяцев назад
2:21 "int elapsedHoursInDays" That is not a good variable name. I don't even know what that's supposed to mean. Is it counting hours or days? And since it's an int, it obviously can't be counting partial days.
@dailymeow3283
@dailymeow3283 2 года назад
I don't comment, so my code is the ugly 😸
@CodingWithLewis
@CodingWithLewis 2 года назад
😂😂
@SkyFly19853
@SkyFly19853 2 года назад
Why not give short names to variables and use a separate text file to explain those variable names? Because of that, your code would be clearer and faster to process?
@CodingWithLewis
@CodingWithLewis 2 года назад
You’d have to cross reference 2 files in that instance.
@SkyFly19853
@SkyFly19853 2 года назад
@@CodingWithLewis At least, entire code would be more organized itself.
@fadimoussa8382
@fadimoussa8382 2 года назад
Having to reference a separate text file to figure out the purpose of a variable is a no-no. When reading code it should be easy and fast to read and understand.
@Mrmcwarpather
@Mrmcwarpather Год назад
@@SkyFly19853 this man making a dictionary for his code 💀💀
@apmcd47
@apmcd47 10 месяцев назад
The second file will get out of date really quickly. It would make more sense to put the explanation in a comment at definition of the variable, but once you've done that you may as well create better variable names.
@doekewartena5729
@doekewartena5729 10 месяцев назад
Your clean code does a totally different thing :/
@argi0774
@argi0774 9 месяцев назад
The thing is: if you are only coding in Python and JS, you don't even know the word "efficiency"
@lynskyrd
@lynskyrd 9 месяцев назад
spoken like a true C++Ahole or are you one of those Object-Oriented snobs that create classes... 'just because'.
@Megaheropap
@Megaheropap 2 года назад
40k subsribers? More like 50
@CodingWithLewis
@CodingWithLewis 2 года назад
Check out my shorts :) where most of my views are!
@airman122469
@airman122469 10 месяцев назад
There’s no such thing as “self-documenting” code. If anyone tells you their code is self-documenting, feel free to laugh at them.
@promant6458
@promant6458 9 месяцев назад
I think this might be the most stupid thing I read today, and still it's 9AM.
@Westkane11
@Westkane11 10 месяцев назад
Good video. Just a one i observed. Your refactoring with the guard clauses left out the "member.sendEmailNotification()" part, so it can only "member.renew()". One way to fix this is to put the "member.isLifeTimeMember()" as the last guard, with member.sendEmailNotification()" in the true part of the check followed by return;
@turtlecode
@turtlecode 2 года назад
Really great video.
@CodingWithLewis
@CodingWithLewis 2 года назад
Glad you enjoyed it Turtle Code :)
@daddy-noks
@daddy-noks 3 месяца назад
i just yk make it super unreadable
@KilgoreOnDrugs
@KilgoreOnDrugs 4 месяца назад
Lewis can't code
Далее
My 10 “Clean” Code Principles (Start These Now)
15:12
Junior Vs Senior Code - How To Write Better Code
22:13
Stop Recommending Clean Code
27:05
Просмотров 459 тыс.
25 VS Code Productivity Tips and Speed Hacks
11:35
Просмотров 2,3 млн
Naming Things in Code
7:25
Просмотров 2 млн
The 3 Laws of Writing Readable Code
5:28
Просмотров 400 тыс.
How Senior Programmers ACTUALLY Write Code
13:37
Просмотров 1,4 млн
how NASA writes space-proof code
6:03
Просмотров 2,1 млн
These 7 Coding Skills Give You an UNFAIR Advantage
7:55
Don't Write Comments
5:55
Просмотров 782 тыс.