- Just because a variable name is legal doesn't mean it's a good variable name
- Consider the following code
d = s * t
- Can you guess what this is calculating?
- How about this
distance = speed * time
- Both expressions use legal variable names but the second is more informative than the first
- It is more readable
- The code you write makes sense to you right now
- But will it still make sense a year from now?
- The name of a variable should always describe the value it holds
- So speed is a good variable name
- But xena_the_warrior_princess is not
- Sometimes you need more than one word to describe the value contained in a variable
- When this happens, you should use the underscore character,
_ to separate words like this
course_id
student_name
first_name
- Another way to combine multiple words in a variable name is to capitalize the first letter
of each new word
- Like this
courseId
studentName
firstName
- This technique is called
camel case
- I prefer to use underscores since I think they are more readable
- But you are free to use either technique
- You must do something to make the start of each word in name stand out
- I don't want to see
courseid
studentname
firstname
- They are not easy to read
- This is much better
course_id
student_name
first_name