No video

3 Tips for Writing Simple and Clean Code

  Рет қаралды 45,911

ArjanCodes

ArjanCodes

Күн бұрын

Writing clean code is simpler than you think! In this video, I reveal the top 3 common mistakes that hinder your progress in writing clean code. Spoiler alert: precision is more important than you think!
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes...
💻 ArjanCodes Blog: www.arjancodes...
🐱💻 Git repository: git.arjan.code...
✍🏻 Take a quiz on this topic: www.learntail....
🎓 Courses:
The Software Designer Mindset: www.arjancodes...
The Software Designer Mindset Team Packages: www.arjancodes...
The Software Architect Mindset: Pre-register now! www.arjancodes...
Next Level Python: Become a Python Expert: www.arjancodes...
The 30-Day Design Challenge: www.arjancodes...
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
💬 Discord: discord.arjan....
🐦Twitter: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
📱Instagram: / arjancodes
♪ Tiktok: / arjancodes
👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Dale Hagglund
🎥 Video edited by Mark Bacskai: / bacskaimark
🔖 Chapters:
0:00 Intro
0:40 Mistake 1
5:15 Mistake 2
10:13 Mistake 3
15:33 Final thoughts
17:15 Outro
#arjancodes #softwaredesign #python
DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Пікірлер: 109
@ArjanCodes
@ArjanCodes 10 ай бұрын
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
@swagatopablo
@swagatopablo Жыл бұрын
Tip 2 is spot on. I am a religious follower of pep 484, and coming from C++ experience, I use python almost as if it's a statically typed language, with type hints for every variable and function. I find it makes the methods cleaner and their purpose more obvious from the signature. To me, type hints are not optional, they are part of the language.
@johnabrossimow
@johnabrossimow Жыл бұрын
The good thing about using type hints is that it forces you to use abc's or protocols, which improves quality and structure of code
@jaredraga
@jaredraga Жыл бұрын
I just discovered your channel right now while I was searching about "Single Responsibility Principle" Glad you're very much active AND putting out code principles! Great content
@ArjanCodes
@ArjanCodes Жыл бұрын
Awesome, thank you!
@zikomo8913
@zikomo8913 Жыл бұрын
Which challenge?
@cbunn81
@cbunn81 Жыл бұрын
11:22 A minor note on the refactoring: If you were to use this function with a set, you might not get the results you expect, since a set does not preserve insertion order like a list or tuple does. So the list of lengths returned would likely be difficult to correlate to the input set. Of course, this is a manufactured example, and in the real world, you'd probably use collections.Counter for something like this.
@DavidCSaint
@DavidCSaint Жыл бұрын
breaking up complex if-else statements in a function to dedicated functions was super useful. Thank you!
@ArjanCodes
@ArjanCodes Жыл бұрын
You’re welcome, David!
@kokokokoalabrothers6009
@kokokokoalabrothers6009 Жыл бұрын
Favorite programming channel ❤
@NiCo-gw2dh
@NiCo-gw2dh Жыл бұрын
That’s one of many things that I learned through your 30 days challenge. Thanks for that!
@ArjanCodes
@ArjanCodes Жыл бұрын
You are so welcome!
@hcubill
@hcubill Жыл бұрын
Love your new color scheme Arjan and your videos only get better and clearer. Thanks!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much!
@user-ml5em9eo2e
@user-ml5em9eo2e Жыл бұрын
4:18 if you had selected ''location.geolocation[0]'' and hit ctrl+d you can easily have selected all of them. Quite a neat trick
@Shubham__Saroj
@Shubham__Saroj Жыл бұрын
This video is fantastic as always. Grateful to know about the Sized typehints. Also, the best practices example along with Armstrong's quote is excellent.
@5thElem
@5thElem Жыл бұрын
Thanks Arjan for another piece of super useful content, even learned something new about the "Law of Demeter"... Regarding the count() function, i would prefer just to write "return [len(item) for item in items]", this saves a lot of boilerplate and is more expressive. Maybe the name count() is a bit counter-intuitive as well, because counting things almost always results in a single integer value. Type hints are a life saver for everybody who needs to dig into code written by somebody else regardless of the value they provide for code completion in IDEs. However, they can get a bit clumsy, especially when it comes to annotating callables or collections of complex objects. Aliases are a nice way to overcome that ;-)
@ondskabenselv
@ondskabenselv Жыл бұрын
I like the Sized, Sequence, and Iterable over a plain list for type annotations (which most of my colleagues use), though I'm sometime too lazy to bother because of the imports, and my teammates doesn't really see the benefit. I think using type hints like list[str] clashes with Pythons very dynamic approach to typing, and might prompt unnecessary data conversions to satisfy the type checker. I use types most places, even throwaway code, because it helps me to mentally lock down the function signature, rather than introducing a bunch of if statements for handling different data types, just like you mention.
@klmcwhirter
@klmcwhirter Жыл бұрын
Excellent thought process! Remember that so-called throw away code is being done to prove a concept or prototype a design idea. I can't tell you how many times those exercises have been rushed into production because I did too good a job of marketing them. It is good to develop the discipline that assumes POC code will be in production - tomorrow. Thanks for sharing this very practical perspective!
@gonecoastaltoo
@gonecoastaltoo Жыл бұрын
Such a great educator. Took me years to learn and apply these practices, yet Arjan presents it so concisely. Such good advice. Practice these things, get better at code, make happy maintainers. Thanks Arjan, great video.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much!
@thisoldproperty
@thisoldproperty Жыл бұрын
Passing an Iterable is a great piece of advice rarely seen/provided.
@KrzysztofKwlk3593
@KrzysztofKwlk3593 Жыл бұрын
In Final thoughts Arjan says "Do not focus on optimization" - totally agree, I spent 2 months creating "perfect solution" which later turned out to be not needed. My advise is to firstly create decent prototype, far from ideal and then refactor once new requirements will come in. Totally agree regarding input arguments -> at first do not try to be too much generic, it requires time and you may never benefit from it. Regarding output, be always specific, never try to be generic.
@ike45mc
@ike45mc Жыл бұрын
This great because this is almost exactly relatable to my current python project using geospatial data. I have gained so much knowledge by looking at other people's code (be careful who's code you read), and applying those concepts to my code. Your use of data classes is so simple and easy, and it makes handling some of the errors I received from my first edition of the project much easier.
@TheCorruptedClan
@TheCorruptedClan Жыл бұрын
Thanks for posting this. Been wanting to refactor my code and tips 2 and especially 3 are going to help. Hope to see more of these soon
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad it was helpful!
@BaronXOfficial
@BaronXOfficial Жыл бұрын
Arjan, you are by far the best Software Engineering related content creator and I appreciate you so much. Thanks for making complex and challenging topics seem cool and fun for all of us to benefit ❤
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you so much!
@klmcwhirter
@klmcwhirter Жыл бұрын
Here! Here! As someone who just wrapped up a 40+ years career I totally agree with your assessment with the apparent ease in which he presents topics in a very digestible manner. But as someone who has done a tremendous amount of training myself; I am here to tell you it is a LOT of work. My rule of thumb was 8 hrs of prep for 1 hr of delivery of the training (on average). @ArjanCodes - you are incredibly talented and IMHO have truly found your sweet spot. Please keep it up.
@DeKeL_BaYaZi
@DeKeL_BaYaZi 3 ай бұрын
I very much love the Idea of Iterable and Sized. Tbh I didnt even know those existed.
@joel-eq8tq
@joel-eq8tq Жыл бұрын
The tip about using external libraries is such a great one and something I took a long time to follow. As well as the benefits you listed, another one is there's usually better documentation that you can point others too.
@martinsamcreaone
@martinsamcreaone 7 ай бұрын
Thank you so much. Relevant and very well explained.
@ArjanCodes
@ArjanCodes 7 ай бұрын
You're very welcome!
@Shuyinz
@Shuyinz Жыл бұрын
Very nice! Please make more such type of videos!
@KonstantinPrydnikov1
@KonstantinPrydnikov1 Жыл бұрын
Good picture, clear sound 👍
@mrswats
@mrswats Жыл бұрын
TIL About the Sized type and all the similar ones about specific objects which implement certain protocols. Very nice!
@Tiriondil
@Tiriondil Жыл бұрын
Fibonacci number 🤣😂😂🤣 Thank you I needed that laugh!
@user-mi2bb8bm6s
@user-mi2bb8bm6s Жыл бұрын
Many thanks for your valuable videos from South Korea.
@paulschockner1445
@paulschockner1445 11 ай бұрын
Just came across your video and I just love your flow and style. I am a self-taught new-ish python programmer, and resources like this are invaluable to me. One suggestion I have to help people like me would be to use much simpler, straight-forward code as examples. For this video and a few others, I find myself more focused on trying to figure out the code for URI encryption etc instead of really just focusing on the main point. Other than that, I love what you are doing and keep it up
@ArjanCodes
@ArjanCodes 11 ай бұрын
Hi Paul, thank you! Your suggestion for simpler examples makes sense. The reason the examples may look complex is that I want to make sure they are practical and close to what production code could look like (you won’t find pizza toppings or animal class hierarchies in my videos). It’s a careful balance between keeping things simple enough so it supports the explanation, while at the same time not being a trivial example that’s hard to translate to the real world. I don’t always succeed at that, but it’s something I always strive to improve on.
@claudiafischering901
@claudiafischering901 Жыл бұрын
Thanks for sharing. I got 2 tipps very useful - iterable and raise error - I should use this more. Thanks.
@prestonfrasch9270
@prestonfrasch9270 Жыл бұрын
Thanks! This is helpful and encouraging going into a refactor! 🔧❤🙏🏼
@pablos2563
@pablos2563 Жыл бұрын
I find it useful that a function can return some type like a list when everything is fine, but also I allow it to return eg. None, when sth goes wrong, so that program can still go on. I know I can catch an exception of course, but that approach is also useful to me. I don't like when there is too much of stack trace in the logs. Especially when some failures are expected (up to some point).
@abomayeeniatorudabo8203
@abomayeeniatorudabo8203 Жыл бұрын
Thanks to you I'm getting better with type hints.
@ArjanCodes
@ArjanCodes Жыл бұрын
You’re welcome ☺️
@jj-big-slay-yo
@jj-big-slay-yo Жыл бұрын
Neat tips. Thanks. If I may suggest, there's Jeff Delaney who runs the Fireship channel. Try to be a bit like him, to be more concise and just stick to the bare minimum most crucial things. That way instead of havint to create a 17 minute video, you could just tell it all in 5 minutes tops.
@lukekurlandski7653
@lukekurlandski7653 Жыл бұрын
For number 2, I would prefer to see a friendly API called to_uri() that accepts and converts the three different datatypes, then calls a _to_uri() function that handles the logic. Just as easy to test, but way simpler to use.
@thomaseb97
@thomaseb97 Жыл бұрын
i think that would just add complexity to your codebase as generally you will know what type you're working with from the start so having a function trying to figure out which type you're working with adds complexity instead of just calling from the type directly, then you have a public accessible bare bones one which just takes the bytes, it would leave the user able to create their own functions too if you make your own object which you want to convert to an uri, and if you need the dynamic approach you can create your own, but they will all just use this one function
@PerisMartin
@PerisMartin Жыл бұрын
Your videos make me happy
@ArjanCodes
@ArjanCodes Жыл бұрын
Your comment makes me happy! :)
@TMQuest
@TMQuest Жыл бұрын
Very good tips, especially about type hints at the end. I've used Iterable before, but never knew about Sized. A disadvantage of Sized is that less seasoned Python developers might find them confusing, so maybe it's a bit overkill in a project where the Python knowledge is lower. I think it's neat though. Going from list to Iterable enlarges your options, but Iterables have a feeling that they are fundamentally "like lists" (think a tuple), so conceptually this does not differ very much. But going from a dict to Hashable will allow e.g., integers, which are conceptually very different from a hash map. I think that enlarging a type hint to objects that are conceptually very different from the original might make it more unclear what the function really needs the arguments for. Do you have any thoughts on this?
@user-ky4ek1jv1q
@user-ky4ek1jv1q Жыл бұрын
Thank you so much for your work, it's very helpful! Could you please consider a topic of Software Architecture? E.g. building delivery app from a scratch, consider how to design components, their relations, DB, deploy(docker), maybe microservices architecture. It's more like advanced level. It might be very helpful.
@hghlndr2
@hghlndr2 Жыл бұрын
Such finesse! +1 from me for always giving me new things to learn!
@CaseyFahey
@CaseyFahey Жыл бұрын
Love this
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you, Casey, glad you like it!
@ChrisBNisbet
@ChrisBNisbet Жыл бұрын
Hmm, doesn't going to the effort of being more generic than might be required at the time the code is first written go against the YAGNI principle a little bit? Maybe we should wait until we actually need/want to support more generic types?
@ArjanCodes
@ArjanCodes Жыл бұрын
Absolutely! I think spending some time on making the types more generic mostly makes sense if the code is not for your own internal use but for others.
@soldier22881
@soldier22881 Жыл бұрын
Thanks!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you! ❤️
@blanky_nap
@blanky_nap Жыл бұрын
Great video. There is a singledispatch decorator in standard library which overloads the function based on type of passed argument.
@ArjanCodes
@ArjanCodes Жыл бұрын
Good point!
@blanky_nap
@blanky_nap Жыл бұрын
To the question at the end of the video. I found useful the Postel’s law: be conservative in what you send, be liberal in what you accept". So yes, as example i try to accept types like Mapping (which can be dict or defaultdict, UserDict etc.), but return dict to be precise…
@wisanuupatumphun7831
@wisanuupatumphun7831 Жыл бұрын
I usually use those simple type hint as list, dict, str, int, and may be some class in my code, very rare to use custom structure for type hint. Anyway, may I request for 1 ep to show example of libraries that people missed out and would save the time for developers. Thanks anyway for new videos.
@IvanToshkov
@IvanToshkov Жыл бұрын
I really enjoy your videos. I especially like the code roasting ones as they teach you how to work with unfamiliar code. One small nitpick. Just as you shouldn't make the parameters types too broad, you should not make the return type too specific. The reason is, that the only changes you can do to the function's types without potentially breaking the code that calls it is to make the parameter types even broader, and the return type even more specific. A good rule of thumb is to start on the more conservative side with the interfaces and become more liberal once you're confident in what the code does and how it does it.
@ArjanCodes
@ArjanCodes Жыл бұрын
Hi Ivan, that’s a really interesting point regarding the return type - I hadn’t thought about it in that way. Thanks!
@TechExplorer123
@TechExplorer123 Жыл бұрын
Just a question about the third example. We annotate the return type as list[int] a.k.a. the actual python list type. Now my question: which is considered best practice? to use list[int] or List[int], where uppercase List is imported from typing? Could not find a PEP for it.
@loic1665
@loic1665 Жыл бұрын
Very good video! I totally agree with everything!
@subhampolpagedar203
@subhampolpagedar203 Жыл бұрын
Very much informative video! Thanks for making such quality videos! 😊
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad you like them!
@neoqiao
@neoqiao 11 ай бұрын
I really like your video. Just one question about the second mistake. If we raise exception, then we have to handle it at every call to it. Is it the same as we return URI | bool which we have to handle the returned bool value?
@michel_clemer6819
@michel_clemer6819 Жыл бұрын
Very nice!!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks!
@edgeeffect
@edgeeffect Жыл бұрын
Sound advice.... and not just for Pythonists..... I could apply plenty of this to JS, Rust, even PHP.
@Andrumen01
@Andrumen01 Жыл бұрын
Pretty much the message is to consider the "life cycle" of the product. During the whole life cycle of the product, i.e., conception through EOL, you typically find that 80% of the effort is done in post-production -> versioning to expand features, optimize speed, debugging and/or general maintenance. The remaining 20% is split 10% design, 5% planning and 5% testing. Yes, most of the time you'll be working in a team, so the amount of code per project per person is really not that much. On the other hand, if you're coding by yourself, then the numbers typically flip -> 80% split between design, planning and testing. The remaining 20% will be complaining about all the features you want to add but can't due to the limited you have (forget about maintenance and testing, takes too much effort). If you are the latter, better listen to Arjan!
@cetilly
@cetilly Жыл бұрын
This video was a gem! 👏🏻
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks man, glad you liked it!
@calmy-play
@calmy-play Жыл бұрын
can u tell name of extension for importing anything to my code in vscode?
@manofqwerty
@manofqwerty Жыл бұрын
I tink some of the type stuff comes down to YAGNI, but if really depends on your specificscenario.
@leidem
@leidem Жыл бұрын
In your Iterable example, wouldn’t the Iterator type be more appropriate? Trying to wrap my head around the difference between the two, but it seems like you’re not guaranteed to be able to iterate over an Iterable, as you’d have to call iter() on them first? Of course, most Iterables are also Iterators.
@peegee101
@peegee101 Жыл бұрын
Nice neon!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you! 😎
@samirhardstyle
@samirhardstyle Жыл бұрын
I always read the type: "List[str]" like "Iterable[str]".
@miguelvasquez9849
@miguelvasquez9849 7 ай бұрын
Some people advise function names to be specific, while others suggest they'll be more concrete. I am confused
@tomxu4254
@tomxu4254 Жыл бұрын
actually never used sized, but used iterable before.
@Alticroo
@Alticroo Жыл бұрын
I do a lot of type hints; especially if I have my own internal object models; I am at the point where I am looking for typehinting and validation for DataFrames;
@munteanionut3993
@munteanionut3993 Жыл бұрын
What is shellroast-py?
@yeetdeets
@yeetdeets Жыл бұрын
For mistake 2, I'd prefer to have a bland util function which converts int/str/bytes to str. It could be used anywhere in the codebase and easily tested since the usecase is narrow and clearcut. Then you can just call that on the input and hand the output to the original function which now only takes str and only need to be tested for that.
@ale711
@ale711 Жыл бұрын
Da quando anche in italiano? 👀
@jobdenotter
@jobdenotter Жыл бұрын
Hi Arjan! In VSCode you can use CTRL+D/CMD+D for replacing a recurring sequence of characters. At 4:07 this would have saved you quite a lot of keystrokes :) Just select the code you want to replace (location.geolocation[0]) and hit CTRL+D/CMD+D one time per extra cursor you want. You can then simply type geolocation to replace the code in all places at the same time. The VSCode docs have an entire "multiple-selections-multicursor" section covering similar shortcuts and options to save you some valuable keystrokes. It is an honor to gift this community over 500 of my keystrokes. Fun reading tip: "Do they deserve your keystrokes" by Scott Hanselman
@johanmartijn
@johanmartijn 11 ай бұрын
For item 2 it is such a shame that Python doesn't have templates as C++ does
@MuhammadIsmail-bi8ld
@MuhammadIsmail-bi8ld Жыл бұрын
Wait, aren't tip 2 & 3 sounds a bit contradictive? 🤔
@dornie_donko
@dornie_donko Жыл бұрын
I love python
@tastonic30
@tastonic30 11 ай бұрын
Father of code
@christerpherhunter930
@christerpherhunter930 Жыл бұрын
I pay a little more than an appropriate amount of time implementing type hints.
@JoshuaWise1994
@JoshuaWise1994 Жыл бұрын
Tip #4: when you walk away you don't hear me say pleeeease, oh baby, don't go
@justvashu
@justvashu 4 ай бұрын
Simple and clean is the the way that you’re making me feel
@gshan994
@gshan994 Жыл бұрын
List comprehension missing in eg 3
@tswdev
@tswdev Жыл бұрын
Iterable and Sized and such is a concept used ALOT in C#. It becomes second nature but because its an indirection task, it usually confuses newcomers. Although, I prefer to just explain it and have them understand whats going on..
@jumper0122
@jumper0122 Жыл бұрын
"How much attention do you give to typing" -- so much. Too much arguably. I can and do spend all day going over old code and streamlining it and adding typing. My old code was *rough*.
@frantisekcastek174
@frantisekcastek174 Жыл бұрын
thanks again, specifically for tips nr. 1, 2 and 3 😂
@ArjanCodes
@ArjanCodes Жыл бұрын
Haha, you're welcome!
@corgirun7892
@corgirun7892 10 ай бұрын
1. ask for more than you need. 2. not being specific enough. 3. too specific
@RealLexable
@RealLexable 11 ай бұрын
Go with the flow, because Clean Code isn't written by someone self anymore.
@f4bglv
@f4bglv Жыл бұрын
Off-topic: man, you definitively lost a lot of weight!
@ArjanCodes
@ArjanCodes Жыл бұрын
Yes, I've been working out and prioritising my health.
@olegkolesnik7710
@olegkolesnik7710 Жыл бұрын
thank you! here is a cat for you : ) /ᐠ.ꞈ.ᐟ\
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you too!
@Deadlious
@Deadlious Жыл бұрын
the second example seem a good use for functools.singledispatch
Refactoring a Command Line Shell | Code Roast Part 2
19:59
ArjanCodes
Рет қаралды 16 М.
The Ultimate Guide to Writing Functions
24:31
ArjanCodes
Рет қаралды 180 М.
Parenting hacks and gadgets against mosquitoes 🦟👶
00:21
Let's GLOW!
Рет қаралды 11 МЛН
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 30 МЛН
Fast and Furious: New Zealand 🚗
00:29
How Ridiculous
Рет қаралды 48 МЛН
How to Avoid Refactoring Legacy Code HELL
35:57
ArjanCodes
Рет қаралды 36 М.
3 Tips To Write Clean Code (from an ex-Google software engineer)
17:12
Clément Mihailescu
Рет қаралды 194 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 381 М.
7 Python Code Smells: Olfactory Offenses To Avoid At All Costs
22:10
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 200 М.
The Ultimate Guide to Writing Classes in Python
25:39
ArjanCodes
Рет қаралды 110 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,6 МЛН
The Only Unbreakable Law
53:25
Molly Rocket
Рет қаралды 323 М.
Dependency INVERSION vs Dependency INJECTION in Python
17:51
ArjanCodes
Рет қаралды 157 М.
5 Tips For Object-Oriented Programming Done Well - In Python
16:08
Parenting hacks and gadgets against mosquitoes 🦟👶
00:21
Let's GLOW!
Рет қаралды 11 МЛН