How To Create Variables That Don't Suck - Writing Clean Java Code
Summary
TLDRThis video script emphasizes the importance of using meaningful and descriptive names in programming to enhance code readability and maintainability. It discusses best practices for naming variables, classes, methods, and packages, drawing inspiration from 'Clean Code' by Robert C. Martin. Key takeaways include using context to inform variable names, keeping names concise yet meaningful, and adhering to naming conventions like camelCase for variables and classes. The script also stresses the value of pronounceable names for team communication and the significance of method names that clearly convey their purpose.
Takeaways
- π **Meaningful Names**: Use variable names that clearly indicate what is stored and the purpose behind it.
- π **Context Matters**: The context in which a variable is used affects its naming; for instance, 'catWeightInKilograms' makes sense within a Cat class but not in isolation.
- π **Keep It Short**: Variable names should be as short as possible while remaining meaningful and clear.
- π£οΈ **Pronounceability**: Names should be easy to pronounce to facilitate communication among team members.
- π« **Avoid Type Information**: Generally, don't include type information in variable names unless necessary for clarity.
- π€ **Naming Conventions**: Follow Java naming conventions such as camelCase for variables and classes, and UPPERCASE with underscores for constants.
- π **Constants**: Use all caps with underscores for constants, following the convention for enums as well.
- π **Readability**: Good method names should be verbs or verb phrases that clearly describe what the method does without needing to look into the code.
- π οΈ **Single Responsibility**: Methods should do one thing, making naming straightforward and reducing the chance of miscommunication.
- β±οΈ **Time Investment**: Investing time in choosing good names pays off in the long run by making code more maintainable and understandable.
Q & A
Why are good names important in programming?
-Good names are crucial in programming because they make the code more readable and maintainable, reducing the time and effort required for other programmers or future self to understand the code.
What does a meaningful variable name convey?
-A meaningful variable name conveys both what is being stored in the variable and the purpose behind that variable, making it clear and understandable without needing to look at the code.
Why is it important to include context in variable names?
-Including context in variable names is important because it clarifies what the variable represents, especially when it's not immediately clear from the surrounding code, such as when the variable is not part of a class or a well-defined scope.
What is the recommended length for variable names?
-Variable names should be kept as short as possible while still being meaningful, avoiding overly long or verbose names that do not add clarity.
Why should variable names be pronounceable?
-Variable names should be pronounceable to facilitate communication among team members during discussions, code reviews, or when explaining the code to others.
Why should type information generally be avoided in variable names?
-Type information should generally be avoided in variable names because it can be redundant, as the programming language itself already defines the type, and it can clutter the name, making it less readable.
What is the naming convention for variables in Java?
-In Java, variables should use camelCase, with the first letter of each word capitalized except for the first letter of the variable name, which should be lowercase.
How should constants be named in Java?
-Constants in Java should be named using all uppercase letters with words separated by underscores, following the convention for enums and other constants.
What is the difference between class names and variable names in terms of naming conventions?
-Class names should be nouns and use camelCase with the first letter capitalized, while variable names should also use camelCase but start with a lowercase letter.
Why should method names be verbs or verb phrases?
-Method names should be verbs or verb phrases because they describe actions, which is what methods typically perform, making it clear what the method does when called.
How can you tell if a method name is good?
-A good method name is one that clearly describes what the method does without needing to look inside the code, and it matches the functionality when you do look at the code.
Outlines
π Effective Naming Conventions in Programming
This paragraph emphasizes the importance of using meaningful and descriptive names in programming. It discusses how poor naming can lead to unreadable code and suggests that variable names should clearly indicate what they represent and their purpose. The paragraph provides examples to illustrate the point, such as improving the name 'float num' to 'weightInKilograms'. It also touches on the context of where a variable is used, suggesting that additional context might be needed in certain situations to clarify what the variable represents. The length of variable names is addressed, advocating for conciseness while maintaining clarity. Pronounceability is highlighted as a factor, especially in team environments where code is discussed verbally. The paragraph also advises against using type information in variable names and recommends using single-character variable names only when following established conventions, such as using 'i' for loop iterators. Java-specific naming conventions are briefly mentioned, including camelCase for variables and classes, all lowercase with dots for package names, and all caps with underscores for constants.
π The Power of Clear Method Naming
The second paragraph focuses on the significance of method names in code readability. It asserts that a well-named method should clearly indicate its functionality without the need to inspect the code. The paragraph contrasts a vague method name 'calculate' with a more descriptive one 'calculateAverage', highlighting how the latter immediately conveys the method's purpose. It stresses the principle of single responsibility for methods, suggesting that if a method name is complex, the method might be doing too much and should be simplified. The paragraph also advises on the length of method names, advocating for clarity over brevity, and encourages programmers to ensure that the method's behavior matches its name to avoid confusion. The importance of choosing good names is reiterated, with a humorous note on the potential for future confusion if names are not chosen carefully. The paragraph concludes with a call to action for viewers to engage with the content and follow the channel for more Java tutorials.
Mindmap
Keywords
π‘Naming Conventions
π‘Meaningful Names
π‘Variable Context
π‘Pronounceability
π‘Avoiding Type Information
π‘Single Character Variable Names
π‘CamelCase
π‘Constants
π‘Verbs in Method Names
π‘Single Responsibility Principle
Highlights
Names are crucial in programming as they define classes, variables, methods, and packages.
Using poor names can lead to unreadable code and frustration among team members.
Good variable names should clearly convey what is stored and the purpose of the variable.
Avoid vague names like 'float num'; instead, use descriptive names like 'weightInKilograms'.
Variable names should include units when the context is not clear, such as 'catWeightInKilograms'.
Within a class, you can use shorter names as the context is already established.
Keep variable names short but meaningful; avoid overly long or nonsensical names.
Variable names should be pronounceable for easy communication among team members.
Avoid using type information in variable names unless necessary for clarity.
Single character variable names are acceptable in loops following standard conventions.
Follow naming conventions such as camelCase for variables and classes, and all caps for constants.
Class names should be nouns, while method names should be verbs or verb phrases that describe their actions.
Method names should be clear and concise, telling you what the method does without looking at the code.
If a method is doing too much, it may be difficult to name, indicating a need to refactor.
Good method names should match expectations and not surprise the reader with their functionality.
Choose meaningful method names even if they are longer, as long as they are clear and descriptive.
Taking the time to choose great names improves code readability and maintainability.
The video encourages viewers to leave a like and subscribe for more Java tutorials.
Transcripts
names are the building blocks of every
single thing we program classes
variables methods packages that means
using bad names is a great starting
point for writing impossible to read
code that makes other programmers regret
being on your team in this video we'll
talk about how to create names that will
make your code a pleasure for other
programmers or your future self to read
and maintain i also have a full java
course available in a link down in the
description so go check it out a lot of
these ideas are heavily inspired by one
of my favorite programming books a
classic in the programming world called
clean code by robert c martin you can
find it at the link down in the
description let's start with naming
variables you probably know you're
supposed to use meaningful names but
what does that actually mean to me a
good variable name will tell you exactly
what it is being stored in the variable
and will also convey the purpose behind
that variable so what if you're looking
at someone's code and you see something
like float num equals 94.5
but this doesn't tell you anything about
what this variable actually represents
so maybe you know that this is supposed
to be a weight so a little bit better
name would be
weight but if something weighs 94.5
that's not really enough information is
that pounds kilograms milligrams let's
say you find out this is supposed to be
a weight in kilograms so probably a
better name would be weight in kilograms
the context of where a variable appears
is also important to how you're going to
name it right now since this is a
variable just floating in our main
method we don't know exactly what this
is a weight of is it a rock or a planet
or a cheeseburger so in this context it
might make sense to add to the variable
name to clarify what it's a weight of so
if it's a weight of a cat we could say
cat weight in kilograms however if we
were going to create a weight variable
as a property of a cat class here it's
fine to just use weight in kilograms as
the variable name because we're already
within the cat context so it can be
assumed here that the name age and
weight are all attributes of the cat
without having to spell it out in the
variable names for the length of your
variable names you want to keep them as
short as you can while still keeping
them meaningful so here cat weight in
kilograms tells you everything you need
to know without being overly long
something like fluffy's weight in
kilograms before accounting for the
rotational velocity of the earth on the
second thursday of the month after i ran
out of diet coke that one time this
doesn't help anyone make it long enough
to be meaningful and no longer you
should also make your names
pronounceable if you're going to work on
a team of developers and anyone who's
going to do this professionally will be
you're going to have a lot of occasions
where you need to talk to somebody else
about a piece of code maybe in a code
review or if they have a question about
it whatever when you do you'll want to
be able to talk about those variables in
plain english like update timestamp
instead of something like update mdms
you can maybe also pronounce this update
month month day day year year year year
hours hours minutes minutes second
seconds but you'll also want to be able
to say the variable names without
passing out from lack of oxygen you
should generally avoid using type
information in your variable names for
example for this cat weight in kilograms
you probably don't need to say float cat
weight in kilograms one exception to
that could be if you're doing some kind
of type conversion for example if you
need to take in this weight as a string
and convert it to a float you might have
one variable that's called string cat
weight in kilograms and another called
float just to differentiate the two in
that instance you should also generally
avoid using single character variable
names unless that single character name
is a convention like in a for loop the
convention in a standard for loop is to
use i for your iterator so everybody
will know what the purpose of this i is
speaking of conventions if there's some
convention for naming what you're naming
use it here's a few quick java naming
conventions for variables you should use
what's called camelcase which means that
the first letter of every word in the
variable name is going to be capitalized
except for the first letter which should
be lowercase class names should also be
camelcase but with the first letter of
the class name capitalized package names
should be all lowercase with different
sections separated by dots if you're
naming a variable that's intended to be
a constant like this private static
final int grid size the convention is to
use all caps with the words separated by
underscores for enums it's the same
thing all caps with multiple words
separated by underscores which makes
sense because enums are just another
kind of constant you should generally
avoid using underscores in other types
of variable names that aren't constants
class names and variable names should be
nouns like cat or update timestamp or
max hit points method names however
should usually be a verb or a verb
phrase and the name should tell you
exactly what the method does and if it
makes sense what it does that too for
example this save cat method saves a cat
to a database you already know what this
method is going to do
just by looking at the name and there's
no surprises or anything with what the
method actually does in this case we've
called this method save cat but we could
probably even get rid of the cat from
the name because it's implied that this
method is going to save whatever it is
that's being passed in which in this
case is a cat a great method name tells
you what it does without having to look
inside the code you shouldn't have to
look through the code to figure out what
it's doing here's an example of a bad
method name so it's taking in a list of
doubles and it's just called calculate
but what exactly is it calculating well
you can't know in this case without
reading through the method to find out
what it's actually doing so then you
have to take the time to read through it
okay what is it doing oh it's
calculating the average of this list of
numbers well a better name would be
calculate average now you know exactly
what this method is doing just by
looking at the name methods that you
make should only be doing one thing so
that should make naming them pretty
straightforward if you're having trouble
giving your method a simple name it
might mean that your method is doing too
much and needs to be broken up also if
you read the name of a method and then
you're surprised by something that
happens in that method that probably
means either the name is bad or the
method is bad and doing something that
it shouldn't as before you want to keep
method names as short as you can while
still keeping them meaningful sometimes
to make a method name meaningful might
mean a little bit longer of a name but
i'd rather read a long method name that
tells me exactly what it does then see a
short method name that leaves out
important details about the method's
functionality a good way to know if a
method name is good is if you read the
method name and then look at a method
and you just go yup does pretty much
what i thought it would do take the time
to choose great names when you're
programming otherwise you'll find
yourself two years down the line looking
at somebody's code going what what is
this code even doing who wrote this
garbage oh it was me if you enjoyed this
video or learned something please let me
know by leaving a like and be sure to
subscribe so you don't miss each new
java tutorial as always thank you so
much for watching i really do appreciate
you being here with me and i'll see you
next time
5.0 / 5 (0 votes)