How To Create Variables That Don't Suck - Writing Clean Java Code

  Рет қаралды 55,519

Coding with John

Coding with John

Күн бұрын

Tips on creating good names for your variables and methods in Java!
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
Or get the audio version FREE here with an Audible free trial!
www.audibletrial.com/johnclean...
Have you ever read a piece of Java code and thought, what is going on here?
Writing good variable and method names for your Java programs can make it much easier for others to quickly see what's going on, and is the first step to writing clean, readable code.
You just have to know what makes a good name first. In this beginner's Java tutorial video, we'll go over a variety of tips for creating good, meaningful variable names in Java.
Learn or improve your Java by watching it being coded live!
Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java learners.
Let me know what else you'd like to see!
Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletrial.com/johnclean...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Phone I use for recording:
amzn.to/3HepYJu
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com/donate/?hosted...
☕Complete Java course:
codingwithjohn.thinkific.com/...
codingwithjohn.com

Пікірлер: 156
@Pennervomland
@Pennervomland 2 жыл бұрын
the first thing my prof in uni would teach us was clean code. Names, Comments, Javadoc, Invariants, low number of nested if statements, low number of parameters in functions, a maximum of 20 lines per function (use helper functions to make that work). I hated doing all those things at first but the more I got into coding/programming the more I hated other people for not following these guidelines and how much I loved these guidelines
@JannisAdmek
@JannisAdmek 2 жыл бұрын
20 is quite short, I agree that most methods have only a few lines but sometimes a method does just one sequential think that's a bit more complicated (like parsing) and IMHO if you break that up tooo much, it makes it harder to read.
@Pennervomland
@Pennervomland 2 жыл бұрын
@@JannisAdmek I thought the same at first but it's incredible how easy it is to write 20 lines per method if you're making good use of helper methods. and it makes it MUCH easier to read imo. Brackets/comments aren't counted obviously. My prof religiously believed in that and it really did work. he showed us some of his bigger programs (10000+ lines of code) and even I as a coding beginner was able to understand it. At least what he showed us.
@JannisAdmek
@JannisAdmek 2 жыл бұрын
@@Pennervomland I fully agree with you that methods should be short. All I'm daying is if you have one sequential method where you don't need to reuse any of the logic it's easy if I can look at the function in one go without having to jump around :)
@KristopherFields05
@KristopherFields05 2 жыл бұрын
Ha, my professor (using that term VERY loosely) just showed us Javadoc yesterday. Brought any sort of pacing we might've had to a screeching halt. We're in the second semester of this stuff and it's painful. John is a lifesaver for sure.
@KilgoreTroutAsf
@KilgoreTroutAsf 9 ай бұрын
@rajatpandey159
@rajatpandey159 2 жыл бұрын
John, you have helped me a lot. Thank you so much.
@monisha2144
@monisha2144 2 жыл бұрын
You are videos are so simple and easy to understand.. Thank you
@himbazor
@himbazor 2 жыл бұрын
I'm loving each video of this channel. Thank you for every video and your time! Excelent work.
@AshrafZaman
@AshrafZaman 2 жыл бұрын
I love how you explain things, in a very engaging way! Please do a series on Design Patterns 🙏😇
@andres98
@andres98 2 жыл бұрын
I'm so happy that I discovered your channel some months ago. I have 2YOE working as a Software Engineer with Java and I love the way you share your ideas. Keep it up!
@carlosgas7800
@carlosgas7800 2 жыл бұрын
I just learnt the basics of Java programming this semmester at uni and I just love learning more about it from your videos. Thank you very much!
@lonesareer
@lonesareer 2 жыл бұрын
Thanks John , you are doing wonders to teach us how to write good quality code
@danielzywalewski7722
@danielzywalewski7722 2 жыл бұрын
Hello John. Your tutorials are amazing. Thanks to You, I finally understand foundations of Java language, and I passed Exam for Java certificate. Keep going with java tutorials
@khaledamiramin4219
@khaledamiramin4219 2 жыл бұрын
John you are the best, already I could understood Junit and java concept, thank you a lot
@gone143
@gone143 2 жыл бұрын
My favorite KZbin channel these days
@leonardojaneis8745
@leonardojaneis8745 2 жыл бұрын
Hi John, greetings from Brazil! Excellent Java videos!
@nikkolumahang
@nikkolumahang 2 жыл бұрын
The naming convention also applies to other languages not just Java. Great video. Thanks for sharing!
@francksgenlecroyant
@francksgenlecroyant 2 жыл бұрын
Another gift from John 😍, super cool
@mira1m198
@mira1m198 2 жыл бұрын
Great explanation of the topic 👏
@benatakaan613
@benatakaan613 Жыл бұрын
You are an amazing teacher!
@Vinicius28636
@Vinicius28636 2 жыл бұрын
Awesome video again! Thank you!
@carafachera4781
@carafachera4781 2 жыл бұрын
Bro I love your work, thnx 😁
@The_Vict0r
@The_Vict0r 2 жыл бұрын
Great video as always, thanks! As an old subscriber of channels like Andertones and Rick Beato I did not immediately realize what I have a special confidence in this channel. Hope this makes sense ;)
@sencer7974
@sencer7974 2 жыл бұрын
Thank you. I watch your videos with joy. Super funny 😊
@adimoolammohan8912
@adimoolammohan8912 2 жыл бұрын
thanks for making this video ,your are saved mytime lot of
@Supersoaker-xy2cr
@Supersoaker-xy2cr 2 жыл бұрын
2:15 damn you took my most commonly used variable name right out of my head
@MiguelFarah
@MiguelFarah 2 жыл бұрын
Although you did mention in passing the "oh, it was me!" bit, I think this particular point is never stressed upon enough when the subject of self-descriptive code (including good variable names) comes up in classes or tutorials. Back in my code auditing days, I always insisted a lot on this point - "Think of the poor guy who'll have to maintain this in six months!" "That guy is going to be YOU!". Good, self-descriptive code eases you back in when you revisit it long after you first wrote it, without the context of its initial development in the short-term memory. A good name, a properly written loop, a good comment on a particular line (like «We don't need to check for a null value here because the constructor makes sure to replace any given null with "".») saves a LOT of time whenever doing maintenance, be the maintainer the guy who wrote it OR someone else. Cheers! //
@r3jjs
@r3jjs 2 жыл бұрын
I took a programming class in Uni decades ago. The final exam was unique to each person. They were given their own first assignment from the class back and asked to explain what it did.
@micleh
@micleh 2 жыл бұрын
I've been looking forward to a new episode the entire week. I wished you could do them more often. As always, great content. When instructors advise you to make methods to one thing only, I'm constantly reminded of my computer science training where one colleague who deemed himself good at programming, wrote a BlackJack game in a single method, i.e. the main method. The instructors were fascinated by the program actually working over the network and made no mention of the single method and more than a thousand lines of code. Being a mathematician, he also used single letter or cryptic variable names. You would really have liked to have his code as a perfect example of what not to do.
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
I'm considering making a video on how to make awful code that no one can understand or maintain. I might need to get some tips from this guy.
@dragxdd
@dragxdd 2 жыл бұрын
How does one make a one threaded linear-coded game? Like the whole game's code is in a while loop or something XD
@HorrorInAction
@HorrorInAction 2 жыл бұрын
Hey John!I am learning lot from your Vedios.Thanks man ✨.If it is possible can you please make a vedio on any applications like cab booking or ticket booking using Java.It will be really helpful for me to understand the concepts.
@arjacb
@arjacb 2 жыл бұрын
Do you think you could do a video on designing programs better? i.e what design patterns exist, which ones to use, how to use them, etc. I often run into design problems when trying to make something more complex so it’s be awesome if you could! Love your videos btw
@connorpemberton978
@connorpemberton978 2 жыл бұрын
This channel is a gold mine
@unkownuser8455
@unkownuser8455 2 жыл бұрын
Helpful! Already dropped a like for your great explanation : D
@BoloH.
@BoloH. 2 жыл бұрын
I've watched several of these tutorials even though 1) I'm a veteran programmer 2) I've never had to use Java professionally. These are just good content. What would be a great short video is a tutorial to how to read documentation and why it is beneficial since a lot of beginners don't really leverage the benefits of that.
@thorbenjohr
@thorbenjohr 2 жыл бұрын
Very important is using packages to organize the code, like if you have a lot of animal classes like dog, cat, parrot, etc. Make a package called AnimalClasses and put the 3 named above into it. Creates a clean hierarchy
@OE9371
@OE9371 2 жыл бұрын
unreadable code means the company can’t kick you out tho
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
That could be a fun idea for a video - how to create awful code that no one can maintain
@MiguelFarah
@MiguelFarah 2 жыл бұрын
@@CodingWithJohn Been there. Reimplemented that.
@alfredomenezes8814
@alfredomenezes8814 2 жыл бұрын
True!
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
I'm proud to say that a video on creating terrible code will be up tomorrow morning. Hope it lives up to the hype!
@MiguelFarah
@MiguelFarah 2 жыл бұрын
@@CodingWithJohn Oh, I have all kinds of horror stories in this regard.
@NEnigma777
@NEnigma777 2 ай бұрын
Seriously, I may actually pass my class because of you
@nicolasjoulin3004
@nicolasjoulin3004 2 жыл бұрын
Hey, really like your videos. Although I am fairly familiar with most of the concept you talk about they are great for consolidating knowledge ! Any plan on doing a build tooling video (Maven, Gradle and the sort) ? although I am generally comfortable writing java code, the build tooling is still very much arcane to me ^^
@amineguettaby5507
@amineguettaby5507 2 жыл бұрын
your are the best john
@zepptonian
@zepptonian 2 жыл бұрын
Another naming convention is to prefix method names that return boolean with ‘is’ or ‘has’. For example, isActive() [for a User class] or hasHorns() [for a class that extends Animal].
@AHMED-ry2gy
@AHMED-ry2gy 2 жыл бұрын
Hey john, please make a video on lambda expressions and java streams, thank you
@mrmichalification
@mrmichalification 2 жыл бұрын
love that fast writing :)
@sssaamm29988
@sssaamm29988 2 жыл бұрын
Hi John.Thanks a lot for the java tutorials.can you pls make a video on threadLocal?
@alikhaled6253
@alikhaled6253 2 жыл бұрын
Best Java teacher
@AnthonHax
@AnthonHax 2 жыл бұрын
Clean Code (R.C.Martin) is a nice book for people trying to improve things like this
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Yep! Mentioned in the video and linked in the description too. Probably one of my favorite programming books. It's been around for a while at this point but the concepts still apply.
@AnthonHax
@AnthonHax 2 жыл бұрын
@@CodingWithJohn for sure, thanks for the content! I do learn a lot with this channel.
@clon123sam
@clon123sam 2 жыл бұрын
So cool that you switched to IntelliJ. Would you create a IntelliJ shortcuts video just like you did for eclipse?
@marcelobaldado514
@marcelobaldado514 2 жыл бұрын
I really like and learn a lot from this, we are currently taking java in our programming class. I hope that in the near future I'll be able to master java :(((
@andres98
@andres98 2 жыл бұрын
I noticed you started using IntelliJ it would be awesome to have a video for tips&tricks / customization / best shortcuts as you did with Eclipse
@kobaabzianidze4677
@kobaabzianidze4677 2 жыл бұрын
Another great video, John can you shoot video about how you have started your first job/internship? and tell us about Roadmap of becoming java software engineer, Also what should intern know from your view as a Team lead java engineer.
@Teloi
@Teloi 2 жыл бұрын
Another tip of mine is that if a method's name is a yes/no question (ex: isValidInput) make it return a boolean. Please It makes code so much cleaner and more self-explanatory
@19891214ful
@19891214ful 2 жыл бұрын
Johny boy did it again. 😎❤️
@sadeeshadulanjana4459
@sadeeshadulanjana4459 2 жыл бұрын
John, can you do a video on JavaCC and domain-specific languages?
@Mr5nan
@Mr5nan 2 жыл бұрын
Hi, can you make a thorough video on stream and all possible combinations and implementations of it? You can make it in several parts :)
@troeteimarsch
@troeteimarsch 2 жыл бұрын
Great! I'm going to send this video to my teacher at school :D His code is plain crazyness to read :D 'String sn' is supposed to be a surname but to my mind it reads "sniffer" (huehue). 'What String sniffer are we talking about again?' :D It works, but...yeah :D
@velk20
@velk20 2 жыл бұрын
Hello there, it will be awesome if you do video about setup eclipse like yours black theme and so on.
@ervinkooo2281
@ervinkooo2281 2 жыл бұрын
Hello sir, could you please make a video about the interface comparable?
@LoesserOf2Evils
@LoesserOf2Evils 2 жыл бұрын
What about using conventional abbreviations in method and variable name? WeightInLb vs. WeightInPounds? My profession (library and information resource management) is trending away from abbreviations excerpting common and conventional ones, e.g., measures. Many thanks.
@marly7549
@marly7549 2 жыл бұрын
Hi, really my first language not is english but I understand you and you help me.
@sadeeshadulanjana4459
@sadeeshadulanjana4459 2 жыл бұрын
John, please do a video on reflection API in java also
@engineersintravel4377
@engineersintravel4377 Жыл бұрын
Oh, man, I love you!
@benjaminkroner1485
@benjaminkroner1485 2 жыл бұрын
Can you make a video about the comparable interface?
@tiger7858
@tiger7858 2 жыл бұрын
please make a video about lambda expression in java
@rjstegbauer
@rjstegbauer 2 жыл бұрын
My convention for a variable in a loop is to double up the letter "ii" instead of "i". That's still easy to type, easier to *see* and more distinct when *searching* for it in the code.
@TusharDwivedi19
@TusharDwivedi19 2 жыл бұрын
which theme do you use ? can you also make a video on that or just add the details in description.
@virtualfi
@virtualfi 2 жыл бұрын
Since I play guitar and have lots of "how to play guitar" on my feed, I had real mindf*ck on first 30sec of video. My brain had to tune in :)
@El_Fuerte
@El_Fuerte 2 жыл бұрын
I think it would be kinda cool if you could show some bigger java projects you did. When I learned java at school we usually just did some really small programs to learn about a keyword or function but never did something big or really interesting.
@cabalunaharoldedself.5237
@cabalunaharoldedself.5237 Жыл бұрын
Hi john, I'm one of your supporter I've watch almost half of you java tutorials. but there is a big question on my mind can you make a video explaining the maven and gradle it makes me so confused. I hope you notice this comment... by the way john, thank you so much!!!
@hussainaqeel7371
@hussainaqeel7371 2 жыл бұрын
Hey john! Could you please tell us your eclipse theme? It looks amazing and I'd love to try it
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Sure, it's a plugin called Darkest Dark
@hussainaqeel7371
@hussainaqeel7371 2 жыл бұрын
@@CodingWithJohn Sounds great! Thanks for the quick reply, absolutely love your videos and nature!
@dynamichashsolutions7704
@dynamichashsolutions7704 2 жыл бұрын
Please make a video about date and datetime with zones, those are very confusing if you don't have experience.
@dianafarhat9479
@dianafarhat9479 2 жыл бұрын
Hey John, thanks for the great work. Can you please make a video that explains how we can change fields in children classes (if they're private in the super class). Is it possible or should we just use methods? Thank you.
@drannoc9812
@drannoc9812 2 жыл бұрын
You have to make getter/setter methods I think
@dianafarhat9479
@dianafarhat9479 2 жыл бұрын
@GabrielJ All right, thank you!! ❤️
@mr.pirata8414
@mr.pirata8414 2 жыл бұрын
Will you do Spring?
@Marcus_Graf
@Marcus_Graf 2 жыл бұрын
When you zoom in into code, this totally makes sense. But how do you go about documenting larger projects. I sometimes get lost in old code projects because I did not document the strategy or philosophy of a specific solution. I then still have to reverse engineer what I did. Any tips on that??
@leonardonetagamer
@leonardonetagamer 2 жыл бұрын
Don't do that
@johnmisley5411
@johnmisley5411 2 жыл бұрын
Can you go over trees?
@HariKrishna-mi6is
@HariKrishna-mi6is 2 жыл бұрын
Waiting? For your video ☺️
@sumitnegi1119
@sumitnegi1119 2 жыл бұрын
sir its my personal request please make detail videos on collection framework in which cover list set map queue all conept
@Speiger
@Speiger 2 жыл бұрын
By the way the class names are in "PascalCase" not CamalCase and First letter up.
@maxxh
@maxxh 2 жыл бұрын
Id really like to see a video on Streams and/or lambda expressions these always confuse me a little bit
@MajidJafari
@MajidJafari 2 жыл бұрын
Would it be bad practice to call the instance variable inside the Cat class only weight instead of weightInKg, but have the Javadoc comment contain information about the unit?
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Generally, yeah, it's still better to have that information in the name. I usually will only add a comment to describe why a certain piece of code is doing what it's doing (for example, if there's a business reason for the code to do something that would look really weird without that context) rather than describing what it's doing or clarify what a variable is. Often, if you feel the need to comment a piece of code to clarify what it is or does, it probably means the code or name could be better.
@MajidJafari
@MajidJafari 2 жыл бұрын
@@CodingWithJohn I see, thanks.
@ettiennevanzyl5377
@ettiennevanzyl5377 2 жыл бұрын
Could you make a video or perhaps by replying to this comment, explain when one would use double.Nan and when to use null?
@capitanperegrina
@capitanperegrina 2 жыл бұрын
That "It was me!" happens a lot. ha ha ha ha ha
@flipper71100
@flipper71100 2 жыл бұрын
About the method naming convention it doesn’t work every time no matter how small my methods are . In a real life project a lot of wrapper method does two three things or even more than three things at the same time , which is calling multiple methods then build list of parameters finally pass those to another method in that case it becomes really difficult to give a appropriate Nate to that wrapper method, if any one is interested I can give a couple of examples from my repo.
@Sarakani
@Sarakani 2 жыл бұрын
6:24 line 53 is the 500IQ move
@mastershonobi110
@mastershonobi110 2 жыл бұрын
I noticed you didn’t add “Captain Crunch” in your Cereal Enum! Lmao..teasing..😜
@StillAlekh
@StillAlekh 2 жыл бұрын
Please do JVM, JDK, JRE. I have been coding since 2 years yet struggle to understand it. Please help.
@jasonformol3263
@jasonformol3263 Жыл бұрын
5:25 in the stream, you named a variable "a". Is that a correct variable 's name ?
@CodingWithJohn
@CodingWithJohn Жыл бұрын
It's definitely a valid name technically, but it's pretty terrible if you want a good, meaningful name.
@mariomolnar3184
@mariomolnar3184 2 жыл бұрын
Commenting and documentation writing video next!
@jensBendig
@jensBendig 2 жыл бұрын
Why not making Weight a class and then .setKg(float kg); ?
@Hufsa
@Hufsa 2 жыл бұрын
yeah makes sense, even when I knew about some conventions I didn't do what I had and when the code grew I started to suffer
@johanna31278
@johanna31278 2 жыл бұрын
2:16: 🤣 10 out of 10
@yacoubabudayyeh7536
@yacoubabudayyeh7536 Жыл бұрын
Enta zalme mo7taram :)
@angellyca9718
@angellyca9718 2 жыл бұрын
❤️😡 learn with jhonn ❤️🐒
@front-endfatih4030
@front-endfatih4030 2 жыл бұрын
1:44 that’s one fat cat 😂
@milanjovancevic4063
@milanjovancevic4063 2 жыл бұрын
2:27 hahha this made me laugh :D
@jericho7989
@jericho7989 2 жыл бұрын
Wow you used intellij now
@JelleDeLoecker
@JelleDeLoecker 2 жыл бұрын
But I like snake_case for my variables!
@findlestick
@findlestick 2 жыл бұрын
Have you stopped doing tutorials in Eclipse altogether?
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Yeah, I did that survey and almost twice as many viewers used IntelliJ as Eclipse. So I thought it made sense to switch. For most video topics, like this one, it doesn't really make a difference which is used. But no matter which one I use I get a bunch of comments saying it sucks and I should use the other one 🙃
@findlestick
@findlestick 2 жыл бұрын
@@CodingWithJohn Understandable. Although, it feels like the continuity and flow between your older Eclipse-videos and these videos has been somewhat lost, by suddenly changing IDE. From my own personal beginners-perspective, consistency in the IDE used throughout a series is preferable. Never mind, still great videos. And congrats on reaching 50K subs. 👍
@animeko4u706
@animeko4u706 2 жыл бұрын
It was really helpful. I am a beginner in Java and was asking people I know how to write clean code. Unfortunately, they almost did not say anything even tried to avoid this topic. Really thankful for this video. COULD you also make video about how to write comments for codes? ))))))
@nicklausss
@nicklausss 2 жыл бұрын
I will be almost like this guy in one year.
@somanyteapots
@somanyteapots 2 жыл бұрын
That example variable name is r/oddlyspecific material. What the hell did you do to Fluffy after you ran out of diet coke that Thursday?!
@Lixon236
@Lixon236 2 жыл бұрын
Why not Javadoc? Just art + enter in IDE , i think more javadoc more important than the name.
@germimonte
@germimonte 2 жыл бұрын
or do what everyone does, start with short and fast to type names and refactor before committing
@Chamarel
@Chamarel 2 жыл бұрын
I love it how cats weight 95 KG lol
@beerensaft413
@beerensaft413 2 жыл бұрын
She had a lot of cookie codes
@marcello4258
@marcello4258 2 жыл бұрын
2 years? I already had it sometimes after 3 month.. "hey you wrote this service, could you please change xz to yy?" ..sure give me a minute.. ehm hour.. screw it I start over.
@usabilist
@usabilist Жыл бұрын
So why "clean" or "don't suck" (sic!) are defined as a list of idiosynchratic examples of a tandom guy?
@Aizistral
@Aizistral 2 жыл бұрын
"...what is this code even doing, who wrote this garbage? Oh... it was me." Long story short: been there man
Java Unit Testing with JUnit - Tutorial - How to Create And Use Unit Tests
21:35
Vectors in Java: The 1 Situation You Might Want To Use Them
16:13
Coding with John
Рет қаралды 75 М.
How many pencils can hold me up?
00:40
A4
Рет қаралды 17 МЛН
КАКОЙ ВАШ ЛЮБИМЫЙ ЦВЕТ?😍 #game #shorts
00:17
CAN YOU HELP ME? (ROAD TO 100 MLN!) #shorts
00:26
PANDA BOI
Рет қаралды 36 МЛН
it takes two to tango 💃🏻🕺🏻
00:18
Zach King
Рет қаралды 28 МЛН
Naming Things in Code
7:25
CodeAesthetic
Рет қаралды 1,9 МЛН
Is Coding still worth it in 2024? (as an ex-Google programmer)
13:36
Java Strings are Immutable - Here's What That Actually Means
7:06
Coding with John
Рет қаралды 601 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,5 МЛН
3 Tips To Write Clean Code (from an ex-Google software engineer)
17:12
Clément Mihailescu
Рет қаралды 189 М.
Optionals In Java - Simple Tutorial
15:53
Coding with John
Рет қаралды 194 М.
Abstraction Can Make Your Code Worse
5:13
CodeAesthetic
Рет қаралды 601 М.
Writing cleancode withmodern Java.  Miro Cupak, Dnastack
49:14
How many pencils can hold me up?
00:40
A4
Рет қаралды 17 МЛН