This technique has several names: - defensive programming - fail-fast strategy in any case it's so much clearer. instead of focusing on the nominal case which is usually trivial it focuses on the negative cases which document under which conditions this behavior can't happen. this has the below benefits: - it's easier to test - it's easier to log - it's self documenting - it's easier to read, therefore easier to review and maintain. this style is one key difference between professional vs unprofessional code
Yeah, this is a good explanation of the technique and its upsides. It literally has only one downside that I experienced first hand in my career: the professor who was teaching the C programming language course I had to attend at university apparently thought this was the Anti-Christ and "awarded" you negative points when you used it in any homework programs that you had to hand in. To her (yes, a woman, and no, I am not sexist here, I had a C++ prof that was male and equally inept) there was no doubt about the superiority of nested if-statements to guide the lucky/no-errors-path. I, at that point in time, already had 3 or 4 years of professional programming experience outside of university, so I challenged her during class on this (I was in my mid-20s at the time I think - yeah, prolly my most rebellious phase in life). She dismissed all my arguments. Saying "that's not part of the C89 standard!" - whatever the fuck that meant, but clearly, she didn't want to hear my arguments is what I felt at the time. So yeah, I didn't challenge her on that (what I now know is a) ridonkulous answer, since it took me completely by surprise. The moral of my story is thus this: wizards/sorceresses in their ivory towers don't care about readable and maintainable code.
Superb content!!! I am a very big fan of readable & clean code and I must say last couple of your videos are very helpful. Keep creating these code refactoring series.
I LOVE this series! I myself am looking for opportunities to improve and expand on technique and patterns especially now at the beginning of my career; I did not know this approach was even coined a name. Thank you so much for investing time in this content!
Hello. This one is way better than the video about for loops. This really improves the coding and has good reasons and a logic behind the changes. Big up, and keep up the good work.
Please w need Golang Videos Like the Java/Spring Boot Ones, and Please some AWS (the always Free Services so we can practice more), and some Python Machine Learning with Tensorflow, pleasee and Thank you keep going
In Swift, the guard keyword even enforces that you leave the current scope. And you can unwrap optionals that way without introducing a new scope. E.g. for foo in bars { guard let baz = foo.optionalMember else { continue } // do something with baz which is no longer optional }
I really don’t understand why you don’t explain the reasoning that you think if-else is inferior to using guard clauses. It’s a different pattern and it’s a few less lines to write sure, but that alone does not make better.. I really wish you’d explain what makes guard clauses better from a programming perspective, going more in-depth into the reasoning.
This is also very common in a language like Swift which has a specific guard statement, checks the statement is true or the value is not null, else return and bail out early. I have moved over to Java Spring Boot recently and try to do the same thing in Java. The way more modern languages like Swift and Kotlin handle null values with optionals is just nicer in general.
Basically, I think all "else" statements that returns an error or exit the current process can be replaced by somthing like : **if (!condition) return;** It's like an evidence for us, but when I look at a new programmer's code, I still wonder why tf it doesn't make sense in their mind :/ Very good video :) EDIT : typo
Thanks Teach everyone better code so that we can get rid of bad code and go home from office earlier. Poor if statements are most common sources of bugs !!
Assalam alykum bro. I am Tajudeen Riliwan from Nigeria. I don't know anything about coding but will love to start. Do you have a course/training for complete beginners?
I know you said this wasn't the main contact but when you said we should be using joins on our query do you mean in terms of RDBMS/SQL? Like Inner/outter joins
If we have > 2 if statements ? Is it right to write them in service methods or its better to create some helper class to exclude condition logic from service with large methods
what's your goal? removing else should not be the goal. the goal is about readability. following the logic flow of thoughts. if it reads better, use else.
@@konqi6957 from what I've heard from other sources, when there's an if/else statement, the computer prepares the code from one of the sides, and if it didn't guess correctly which side is gonna be called, it needs to flush the code and load the one from the other side. I can see how it COULD improve performance if there's never an 'else' statement, and it always loads the same code, but if the condition isn't met, it just flushes it... However, I can't be 100% sure if that is indeed the case. As for readability, my code doesn't always become more readable when I do it like this :P
That might depend on your present knowledge. If you are already coding in some other language, you already know so much and learning time would be significantly shorter for you. If you are new to programming, you might have to invest some time in the fundamentals. Treat programming like mathematics. Just don't look into the theory and learn things, write some code and see what works and what doesn't. Learning how to fix things is really important as you would be doing this for half of your time. Amigoes code already have a roadmap for java developers. That looks cool and I use that to upskill. I would recommend that you can start looking for a job as soon as you can write your own code. Also, you will be learning a lot of things daily. Good luck.
@@sreedarshshaji5951 Thank you so much for the complete answer. I did not expect such an answer. I have learned java core OOP, Telegram bots . And I am trying to learn spring boot. As I you said I know a little about java. But I think Nobody accept juinior developers like me. Thanks for your reply anyway 😃😃😃👍👍
@@humoyunmirzomurodullayev6750 I was accepted as a junior developer in j2me since they couldn't find an experienced developer willing to take up the role. I wasn't the brightest, but took some effort and made things happen with that role. So, brother keep learning things and never let the hope down. All the best
Good video 👍👍👍 I see that you use exception classes everywhere.... I hate exception class... Question : development without exception VS development with exception Good or bad practices ?
@@Supreme_Schorpioen you must manage the errors without crash your application, but the methods extends to exceptions and must be required for all methods that use this exception. If you have an exception into repository method, you must extends exception into service... Or if you don't like this way, you use a try...catch Block, and that's very not pretty
This if-else thing seems to be some new fad. Generally, juggling with if-statements won't make much difference to the readability or really to anything at all. Any developer with at least two brain cells can follow the if-else statements just as easily as they can follow the function exit points. It's mostly a matter of taste and personal style. There are a lot more important things in your code base you could improve instead, better tests for instance.
What's the point of the religious behavior and greetings! It's the only reason that keeps me away from this channel you act like an Imam lol. Don't understand why it's hard for some people to act professional and keep their beliefs to themselves
This technique has several names:
- defensive programming
- fail-fast strategy
in any case it's so much clearer.
instead of focusing on the nominal case which is usually trivial it focuses on the negative cases which document under which conditions this behavior can't happen.
this has the below benefits:
- it's easier to test
- it's easier to log
- it's self documenting
- it's easier to read, therefore easier to review and maintain.
this style is one key difference between professional vs unprofessional code
Yeah, this is a good explanation of the technique and its upsides.
It literally has only one downside that I experienced first hand in my career: the professor who was teaching the C programming language course I had to attend at university apparently thought this was the Anti-Christ and "awarded" you negative points when you used it in any homework programs that you had to hand in.
To her (yes, a woman, and no, I am not sexist here, I had a C++ prof that was male and equally inept) there was no doubt about the superiority of nested if-statements to guide the lucky/no-errors-path.
I, at that point in time, already had 3 or 4 years of professional programming experience outside of university, so I challenged her during class on this (I was in my mid-20s at the time I think - yeah, prolly my most rebellious phase in life). She dismissed all my arguments. Saying "that's not part of the C89 standard!" - whatever the fuck that meant, but clearly, she didn't want to hear my arguments is what I felt at the time. So yeah, I didn't challenge her on that (what I now know is a) ridonkulous answer, since it took me completely by surprise.
The moral of my story is thus this: wizards/sorceresses in their ivory towers don't care about readable and maintainable code.
I really like this new series where u are helping us write better code
If you do code refactoring, start from bottom to top.
It avoids displacements of bookmarks and search results.
Very good advice! Any video that shows how to clean up code and make it more readable or efficient is vey much appreciated.
I would love to see some Golang on this channel! I started learning last year, but haven't done much with it since.
Shout out from Brazil ma man, thanks for all content pls keep the good job
Thanks for this video, it is indeed a very clean way of providing code, I will be using it from now on!
Optionals helped here a lot also the practice you showed here is a great addition to improve code readability.
I love your channel brother, every day I open youtube and I get one of your videos and I have no choice but to watch it.
Superb content!!! I am a very big fan of readable & clean code and I must say last couple of your videos are very helpful.
Keep creating these code refactoring series.
This goes against everything I was taught back in the good ol' days. However, I can see it definitely has its advantages. Very well explained.
Please keep doing these code reviews. Love your channel man
Thank you Nelson one more time u brought us a better way to write our code, HUGS From South Africa Mozambique💯
Jazaka allahu khairan.
Thank you brother.
I LOVE this series! I myself am looking for opportunities to improve and expand on technique and patterns especially now at the beginning of my career; I did not know this approach was even coined a name. Thank you so much for investing time in this content!
Hello. This one is way better than the video about for loops. This really improves the coding and has good reasons and a logic behind the changes. Big up, and keep up the good work.
@4:36 this is how I use if statements like a boss 😎
Jazakallah kher
I'm really liking those kind of videos, like a serie in netflix but of coding, teaching good ways to make a better code
Please w need Golang Videos Like the Java/Spring Boot Ones, and Please some AWS (the always Free Services so we can practice more), and some Python Machine Learning with Tensorflow, pleasee and Thank you keep going
Great video!!! I love the style. I think I did not see other guy doing this!!! That’s why I bought one of your courses!!! So far happy with it
Wow, I'm learning a lot with your videos. Thank you so much!!!
This shows why is important to write clean code it’s hard but more efficient
Tank you and جزاك الله خيرا أخي
Well, it's perfect. Thank you for the small clean code
In Swift, the guard keyword even enforces that you leave the current scope. And you can unwrap optionals that way without introducing a new scope. E.g.
for foo in bars {
guard let baz = foo.optionalMember else {
continue
}
// do something with baz which is no longer optional
}
I really don’t understand why you don’t explain the reasoning that you think if-else is inferior to using guard clauses. It’s a different pattern and it’s a few less lines to write sure, but that alone does not make better..
I really wish you’d explain what makes guard clauses better from a programming perspective, going more in-depth into the reasoning.
Thx !! More video about Golang please!!!
Early return is really useful for making your code cleaner, thanks for sharing
thanks Nelson for this one! is very useful
waalaikumsalam my moslem brother wuzzup
This is also very common in a language like Swift which has a specific guard statement, checks the statement is true or the value is not null, else return and bail out early. I have moved over to Java Spring Boot recently and try to do the same thing in Java. The way more modern languages like Swift and Kotlin handle null values with optionals is just nicer in general.
I don't understand but it look smart. And i like it
Great video !!. Loved the reversal execution. Recommending to do how to get data from db, chain it to dto and then send to ui , viceversa
Thank you so much for this video I learned new methods, thanks 🌹🌹🌹🌹
Great tips, brother 👏👏
Basically, I think all "else" statements that returns an error or exit the current process can be replaced by somthing like : **if (!condition) return;**
It's like an evidence for us, but when I look at a new programmer's code, I still wonder why tf it doesn't make sense in their mind :/
Very good video :)
EDIT : typo
Sir please make a video on Java OOPS concept
Great video Nelson, this series isbeing amazing to provide realistic examples of thinking about and architecting high quality code.
Thank you.
Thanks
Teach everyone better code so that we can get rid of bad code and go home from office earlier. Poor if statements are most common sources of bugs !!
Assalam alykum bro. I am Tajudeen Riliwan from Nigeria. I don't know anything about coding but will love to start. Do you have a course/training for complete beginners?
Thank you, waalaikumsalam.
Nelson please do u have a course on maven?
I like your classes 😉 very much! Thank you ❤️ for_Ever))
Can you make video on jdbc template with relationship advance
for java since 8 you can use optional and orElseThrows then you don't need the if statement.
Большое спасибо! Твои видео очень помогают!
Exactly, he does((:
Love your refactoring videos!
Have a nice day.
Thanks good tip to use. I'll use it, thanks my friend
You reading in my mind 😜 On my last recrutation I have got task with 5x if in method and they gave me a question"What is wrong or not with this code?"
So good but where can I find this project, Is it public ? I hope you reply my comment
Do you have a video somewhere detailing more about 4:02? Thanks
please make more code review & fix video XD
Thank you Amigos 😁👍👍
at least I learned how efficiently one can get rid of else block by negating if block
I know you said this wasn't the main contact but when you said we should be using joins on our query do you mean in terms of RDBMS/SQL? Like Inner/outter joins
If we have > 2 if statements ? Is it right to write them in service methods or its better to create some helper class to exclude condition logic from service with large methods
thanks, it's usefull
Nice video.
This is basic reshaper defaults , your best to buy a tool which offers you these change.
Good job Nelson
By default, I was doing this (bailing out) to prevent useless executions
what's your goal? removing else should not be the goal. the goal is about readability. following the logic flow of thoughts. if it reads better, use else.
Great teachings
do you teach OAuth with Java?
tks
Great video!
Da ist ein Bommel auf der Mütze! 😂
Just Amazing!
How do I join your discord? Tells me the invitation is invalid
Do a video on nested ifs
What do we get by this? Does it improve the code's performance or only its readability/length?
I don't think it gives better performance because the conditions are still evaluated.
@@konqi6957 from what I've heard from other sources, when there's an if/else statement, the computer prepares the code from one of the sides, and if it didn't guess correctly which side is gonna be called, it needs to flush the code and load the one from the other side.
I can see how it COULD improve performance if there's never an 'else' statement, and it always loads the same code, but if the condition isn't met, it just flushes it...
However, I can't be 100% sure if that is indeed the case.
As for readability, my code doesn't always become more readable when I do it like this :P
Never give up 🥉
Thanks very useful ... as a JS developer, right now I'm learning Python 😜
nice hat my bro.
وعليكم السلام
Thanks brother 👌
I wonder what it's your real name ?
Your brother Ahmed from sudan 🇸🇩
Wa elikumassala
wa alaikum assalam
good
Cute hat hehe :)
Walaikumusalam
An if else on the top level is cleaner not less clean
Good evening . In want you ask something. How long it takes me to learn java if i tried hard
Forever 🌝
That might depend on your present knowledge. If you are already coding in some other language, you already know so much and learning time would be significantly shorter for you.
If you are new to programming, you might have to invest some time in the fundamentals. Treat programming like mathematics. Just don't look into the theory and learn things, write some code and see what works and what doesn't.
Learning how to fix things is really important as you would be doing this for half of your time.
Amigoes code already have a roadmap for java developers. That looks cool and I use that to upskill. I would recommend that you can start looking for a job as soon as you can write your own code.
Also, you will be learning a lot of things daily. Good luck.
@@sreedarshshaji5951 Thank you so much for the complete answer. I did not expect such an answer. I have learned java core OOP, Telegram bots . And I am trying to learn spring boot. As I you said I know a little about java. But I think Nobody accept juinior developers like me. Thanks for your reply anyway 😃😃😃👍👍
Years.
@@humoyunmirzomurodullayev6750 I was accepted as a junior developer in j2me since they couldn't find an experienced developer willing to take up the role. I wasn't the brightest, but took some effort and made things happen with that role.
So, brother keep learning things and never let the hope down. All the best
Good video 👍👍👍
I see that you use exception classes everywhere.... I hate exception class...
Question : development without exception VS development with exception
Good or bad practices ?
I just started with java few monthes ago, what else can we use if we don't use exceptions?
@@Supreme_Schorpioen you must manage the errors without crash your application, but the methods extends to exceptions and must be required for all methods that use this exception. If you have an exception into repository method, you must extends exception into service...
Or if you don't like this way, you use a try...catch Block, and that's very not pretty
ok but my IDE suggests these improvements automatically
Return early rather than nest deeply...
4:10 xD
Javascript, we need just ( true? 'true' :'falsy')
This if-else thing seems to be some new fad. Generally, juggling with if-statements won't make much difference to the readability or really to anything at all. Any developer with at least two brain cells can follow the if-else statements just as easily as they can follow the function exit points. It's mostly a matter of taste and personal style. There are a lot more important things in your code base you could improve instead, better tests for instance.
the discord server is so big that discord always crashes
3rd
first
now result that else statement is going out due to all youtubers, else statement is the worst in the galaxy, it must die, lol
I think this is an antipattern
I don't know about Java and Go, but it's bad practice in Python. “Explicit is better than implicit” - The Zen of Python.
First
What's the point of the religious behavior and greetings! It's the only reason that keeps me away from this channel you act like an Imam lol.
Don't understand why it's hard for some people to act professional and keep their beliefs to themselves
good work nelson.i have looked for you in your fb messenger and discord account but no response