Cleaner Code: 3 Ways You Can Write Cleaner Code

Поділитися
Вставка
  • Опубліковано 24 гру 2024

КОМЕНТАРІ • 154

  • @dienekes4364
    @dienekes4364 Рік тому +14

    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.

    • @davidmurphy563
      @davidmurphy563 11 днів тому

      That's the most defensive comment I've ever read. There's literally nothing to take from it other than who to blame for a turgid office spat.
      The answer is nobody cares.

  • @jimmea6317
    @jimmea6317 2 роки тому +58

    People who say coding isn’t an art form are just salty they can’t do it themselves

    • @capybaraponque611
      @capybaraponque611 2 місяці тому +2

      it really isn't an art form, and I've been developing for years. You must think you're special.

    • @jimmea6317
      @jimmea6317 2 місяці тому

      @@capybaraponque611 that's a very condescending and unprovoked response, but I suppose you get out of your work what you put into it after all

    • @capybaraponque611
      @capybaraponque611 2 місяці тому +2

      @@jimmea6317 if code is art then literally everything in life is art.

    • @KanjiCoder_RTFM
      @KanjiCoder_RTFM Місяць тому

      ​@@capybaraponque611Yes , there is literally a book called "The ART of war" .

    • @capybaraponque611
      @capybaraponque611 Місяць тому +1

      @@KanjiCoder_RTFM what the fk does war have to do with this? smooth brain

  • @Jabberwockybird
    @Jabberwockybird Рік тому +9

    I like guard clauses, but 4:18 broke the logic of the original code.

  • @ziakhan-tk7rk
    @ziakhan-tk7rk 2 роки тому +18

    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 2 роки тому

      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 2 роки тому

      @@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 2 роки тому +1

      ​@@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.

  • @stevethackery9853
    @stevethackery9853 Рік тому +3

    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.

  • @daniel-godin
    @daniel-godin Рік тому +2

    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!!

  • @CodingWithLewis
    @CodingWithLewis  2 роки тому +4

    Thanks for waiting everyone! Let me know what you think :)
    More frequent videos coming now that I am back from vacation 🌴

  • @Eric-sy3dm
    @Eric-sy3dm Рік тому +1

    I loved watching your video. Those are some good lessons learned.

  • @randyleberknight7049
    @randyleberknight7049 Рік тому +3

    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.

  • @mansoorsoomro8585
    @mansoorsoomro8585 2 роки тому +4

    Dude, Guard clauses have made my life so much easier. Great Video!

  • @maurolimaok
    @maurolimaok 2 місяці тому

    Half a million now!
    CONGRATS!

  • @khalidsaifullahfuad
    @khalidsaifullahfuad 2 роки тому +1

    Really love your videos. You deserve way more subscribers. Best of luck

  • @sketchionic6356
    @sketchionic6356 5 місяців тому

    Separation of concerns is the principle I benefit a lot from. And also make sure to go through the SOLID principles.

  • @MayukhSaikia
    @MayukhSaikia 2 роки тому +3

    Awesome video. Learnt a lot. Specifically the guard clauses

    • @CodingWithLewis
      @CodingWithLewis  2 роки тому

      Glad it was helpful Mayukh 😃 Thanks for watching!

    • @MayukhSaikia
      @MayukhSaikia 2 роки тому +1

      @@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 роки тому +1

      @@CodingWithLewis Thanks

  • @danielleblanc5923
    @danielleblanc5923 Рік тому +1

    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.

  • @reddaxtor5662
    @reddaxtor5662 2 роки тому +2

    I have found a really great channel. It feels like found a mentor.

  • @devkishore1388
    @devkishore1388 2 роки тому +2

    As always, helpful video

  • @ahmedmanhy9899
    @ahmedmanhy9899 Рік тому

    here after one year and u r 340k subs, u deserve a lot Lewis all the support.

  • @iqbalibrahim4713
    @iqbalibrahim4713 7 місяців тому

    I love the second advice, will go read about it right now

  • @proflead
    @proflead 3 місяці тому

    Still relevant! Thanks for sharing!

  • @Fungustus1
    @Fungustus1 2 місяці тому

    look at you! you were celebrating 40k subs, now you're at ~600k subs!!! Well deserved and thank you!!!

  • @RobelGetnet1997
    @RobelGetnet1997 2 роки тому +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

  • @hankobrat
    @hankobrat 2 роки тому +1

    Thanks for the good tips and sources. Keep them coming. 🙂

  • @methanesulfonic
    @methanesulfonic 2 роки тому +2

    I'm just starting to learn to code and your channel is a goldmine, there's so much to learn. thanks a bunch!

  • @Aura_Mancer
    @Aura_Mancer 2 роки тому +1

    Watching the video right now. I'm a beginner, and the guard clauses was very needed! Thank you

  • @BatCorkill
    @BatCorkill 2 місяці тому

    3:45 really hits home with me.

  • @arkandash
    @arkandash 2 роки тому +1

    Thanks, this video helps me out.

  • @BlackdestinyXX
    @BlackdestinyXX 2 роки тому +1

    Love your editing style! Keep make videos!

  • @Yagamiar15
    @Yagamiar15 2 роки тому

    Quality content man... Loved this

  • @Zeioth
    @Zeioth Рік тому

    100% on the spot. Recommended.

  • @malicious8909
    @malicious8909 2 роки тому +1

    Good editing skills, keep it up. Thanks for all those shorts about open source projects too :)

  • @xX12VincEXx
    @xX12VincEXx 2 роки тому +2

    Really great video. Helped a lot. Please make more of the longer type videos :).

  • @dglasswine
    @dglasswine Рік тому

    Short, focused and really helpful

  • @oyleolduboyleoldu3677
    @oyleolduboyleoldu3677 2 роки тому +1

    keep up the good work man I really appreciate your videos 🙏

  • @by_asilbek
    @by_asilbek Рік тому

    Thank u so much is amazing!

  • @andreasbr830
    @andreasbr830 2 роки тому +1

    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.

  • @walaamohamed2067
    @walaamohamed2067 Рік тому

    Amazing and clear one !

  • @bob456fk6
    @bob456fk6 Рік тому

    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 Рік тому +1

      Much better to just extract each level of nested code into its own well named method.

  • @turtlecode
    @turtlecode 2 роки тому +1

    Really great video.

  • @emmanuelgenga7421
    @emmanuelgenga7421 2 роки тому +1

    This is an awesome video... I have issues with hard to read code for projects...

  • @dushalkumar
    @dushalkumar 2 роки тому +1

    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 😂

  • @blendermaster8934
    @blendermaster8934 2 роки тому +1

    Amazing Video About Improving to Code!!

  • @emmanuelgenga7421
    @emmanuelgenga7421 2 роки тому +1

    Also if you have any advice on coding for a 2nd year student I'd be glad to hear from you.. thanks

  • @pepperdayjackpac4521
    @pepperdayjackpac4521 2 роки тому +1

    3:02 what is inversion control?

    • @CodingWithLewis
      @CodingWithLewis  2 роки тому +1

      in "version control" aka git :) means it's in your code history

  • @laxmanadhikari3989
    @laxmanadhikari3989 2 роки тому +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.

  • @edwardnewgate2534
    @edwardnewgate2534 19 днів тому

    thanks very kind of you brother

  • @md.mohiuddinmaruf7737
    @md.mohiuddinmaruf7737 9 місяців тому

    Great video, Make a playlist on SOLID Principles in detail.

  • @bsummer
    @bsummer 2 роки тому +1

    0:25 Love it. LOL

  • @maurolimaok
    @maurolimaok 2 місяці тому

    "A jack of all trades, is a master of none!"
    What about MacGyver?
    I'm beginning The Odin Project, at 56yo. This stuff will be useful.
    Thanks a lot.

  • @GfoxSim
    @GfoxSim Рік тому

    Very very very useful.

  • @anderssjoqvist7816
    @anderssjoqvist7816 Рік тому

    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 Рік тому

      Why not use
      void SendMail(Listreceivers, String Message)
      Or
      def send(receivers, message)

  • @RogerKaptunik
    @RogerKaptunik Рік тому

    Thats funny. I just came from a video here which told me to remove these guard clauses

  • @HairyandFinanciallySolvent
    @HairyandFinanciallySolvent 2 роки тому +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 Рік тому

      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 Рік тому

      ​@@airman122469or just use actual vim.

  • @kissgergo5202
    @kissgergo5202 Рік тому

    4:25 this code doesn't do the same thing though, you never send the email notification in this version

  • @waitun3019
    @waitun3019 6 місяців тому

    One complaint:
    Some clips are so short that I had to literally pause just to get the joke .
    Overall, Love it.
    subscribed

  • @PeterPankowski
    @PeterPankowski Рік тому

    Wow…extreme helpfull

  • @Allium-Cepa
    @Allium-Cepa Рік тому

    solid, cyclomativ complexity, coding guidelines = everything you need to know

  • @tntredstone
    @tntredstone 2 роки тому

    I like using cases/matches in python to make if statments readable

  • @rebok232
    @rebok232 Рік тому

    when the class has more than 1 arg(or few smaller args) in kotlin then i always use the named arguments

  • @liwordsonvijayabalan8387
    @liwordsonvijayabalan8387 2 роки тому +2

    Great production value on this one. Best dev channel on UA-cam!

  • @Westkane11
    @Westkane11 Рік тому

    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;

  • @HoD999x
    @HoD999x Рік тому

    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

  • @thewelder3538
    @thewelder3538 Рік тому

    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 Рік тому

      That's really only useful if you need Performance critical Code. (Like Video games, ...) otherwise it's fine to use.

    • @thewelder3538
      @thewelder3538 Рік тому

      @@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 Рік тому

      @@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 Рік тому +1

      @@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 Рік тому +1

      @@thewelder3538 I've found that most "clean" Code advice goes in that direction

  • @StarDust_2077
    @StarDust_2077 5 місяців тому

    If you have to write more than 3 of statements just use switch or a map

  • @djtomoy
    @djtomoy Рік тому +1

    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 8 місяців тому

    if you have a clarifying comment, is a great sign, that you could do some simple function extraction with a meaningful name.

  • @josepharoh7958
    @josepharoh7958 8 місяців тому

    Where's the discord?

  • @camerobaird5939
    @camerobaird5939 Рік тому

    Yeah, I almost never use comments but I think I split my code into smaller functions a bit too much.

  • @LudvikM
    @LudvikM 9 місяців тому

    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".

  • @しめい-l4m
    @しめい-l4m Місяць тому

    those little rules become nothing when prior dev decides to put everything into a local object "globalFunctions" and somehow thought it's a good idea to throw errors and non-errors such as success event that should end the iteration in that function (it's React btw)

  • @navjotsingh2457
    @navjotsingh2457 11 місяців тому

    ty

  • @ZipClipChannel
    @ZipClipChannel Рік тому

    no matter how clean your code is, there's always that one nit-picky PR reviewer that literally complains about small nothings

  • @TheOriginalJohnDoe
    @TheOriginalJohnDoe Рік тому

    Him: “I’ll use Python and JavaScript”
    Also him: uses PHP

  • @Mynth16
    @Mynth16 23 дні тому

    2 years later and I'm still commenting // NO COMMENT

  • @gvijay2000
    @gvijay2000 2 роки тому +1

    Lovely

  • @PrometheusMMIV
    @PrometheusMMIV Рік тому

    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.

  • @artwork-studios
    @artwork-studios 5 місяців тому

    The way I write cleaner code is.... Umm....... Ehhhh...... Formatting my code Is the most you'll ever see me do actually

  • @gingeral253
    @gingeral253 Рік тому

    You said 40 thousand subscribers? More like 400 thousand.

  • @Fluiding
    @Fluiding Рік тому

    5:03 this goes against java

  • @Anna36936
    @Anna36936 2 роки тому +1

    Your videos are absolutely amazing, informative, helpful and so entertaining as well. I can't thank you enough!

  • @SkyFly19853
    @SkyFly19853 2 роки тому +1

    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 Рік тому

      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.

  • @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
    ❤‍🔥❤‍🔥❤‍🔥

  • @brockdaniel8845
    @brockdaniel8845 11 місяців тому

    Hey uncle Bob. F CleanCode™ and DRY.

  • @dailymeow3283
    @dailymeow3283 2 роки тому +3

    I don't comment, so my code is the ugly 😸

  • @ninjazhu
    @ninjazhu Рік тому

    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 Рік тому

      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 Рік тому

      I have. Our code needs to handle adult offenders differently.

  • @argi0774
    @argi0774 Рік тому

    The thing is: if you are only coding in Python and JS, you don't even know the word "efficiency"

    • @lynskyrd
      @lynskyrd Рік тому

      spoken like a true C++Ahole or are you one of those Object-Oriented snobs that create classes... 'just because'.

  • @doekewartena5729
    @doekewartena5729 Рік тому

    Your clean code does a totally different thing :/

  • @airman122469
    @airman122469 Рік тому +1

    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 Рік тому

      I think this might be the most stupid thing I read today, and still it's 9AM.

  • @Megaheropap
    @Megaheropap 2 роки тому

    40k subsribers? More like 50

    • @CodingWithLewis
      @CodingWithLewis  2 роки тому +1

      Check out my shorts :) where most of my views are!

  • @KilgoreOnDrugs
    @KilgoreOnDrugs 9 місяців тому

    Lewis can't code