DEV Community

Writing Good Method & Variable Names

Rachel Soderberg on May 03, 2019

What's In A Name? In Object Oriented Programming it's considered to be good practice to write methods that only do one major job. One strategy you...
Collapse
 
programazing profile image
Christopher C. Johnson

Loved this, thank you!

I've been ranting about a method I found in one of our old libraries for the last couple of weeks.

GetIdById(), really? What the heck is that supposed to even do?

After digging into it and figuring out what it did I eventually renamed it to GetPatientIdByChartId()

Collapse
 
fpuffer profile image
Frank Puffer • Edited

You write that it is a method, not a standalone function. So it belongs to an object, probably an object of type Patient. So what about calling it GetIdByChartId()? It makes no sense to put information into a name that is obvious from the context.

Collapse
 
programazing profile image
Christopher C. Johnson

Because it was a standalone function. I tend to use method and function interchangeably when I really shouldn't.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

Your rename makes a lot more sense than the original. "GetIdById" sounds like the code equivalent of trying to divide something by zero if you ask me!

Collapse
 
timothymcgrath profile image
Timothy McGrath

Ha. I wish it had just contained

return id;

Collapse
 
ledinscak profile image
ledo

I once implemented model class Room, with supported classes RoomView and of course RoomService :-)
Nice article by the way, only thing I do not like in examples here is to use "list" or any other data typ word in names. I prefer simply to use plural nouns. So instead of getListOfProducts simply getProducts.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

True, you're right. Using the variable will show clearly that it's a list - it's probably a bit redunant. Thank you!

Collapse
 
dbanty profile image
Dylan Anthony

I can see using the data type in the name for some weakly typed languages. Like if you’re not on the latest Python and type annotations are a pain, it’s nice to be able to see what you’re doing.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

I have no excuse, I almost exclusively write in C#. sheepish grin

Collapse
 
darrylhodgins profile image
Darryl Hodgins

There's the industry-standard Javascript hack…

var that = this;

Collapse
 
anwar_nairi profile image
Anwar • Edited

I sometimes encounter var self = this; too, kind of annoying. I prefer to use myFunction() { /* much stuff, very algorithm */}.bind(this) if I am in an event handler for example, much natural to work with this even if the bind is a bit ugly!

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

haha "much stuff, very algorithm" :)

Thankfully I don't need to deal with this very often at this point, but I agree with you. I'll keep this style in mind and try to remember to use it whenever I encounter it again!

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

I think, at my current level, I'd probably just sit there and cry (or laugh maniacally) for a few minutes if I came across this.

Collapse
 
scotthannen profile image
Scott Hannen • Edited

Thank you!

One of my favorites is when we spread a single giant method across multiple methods so that the names are UpdateCustomer, ExecuteUpdateCustomer, DoExecuteUpdateCustomer, and even PrepareToUpdateCustomer.

Collapse
 
shubhamforu profile image
Shubham

Great article. Curious about one thing. Have one another question. For example : getUserNearestLocation(user) by going through it we can say it this function is going to return user location based on user information. If suppose if I have to add another parameter in function let's say hospital getUserNearestLocation(user,hospital) now context has change name should be getUserNearestHospitalLocation . My question is that if tomorrow I have 10 parameters. Then this function name might be very big if I keep adding parameter. Eventually function is return userLocation based on parameter. In those cases what are good naming convention?

Collapse
 
laranikal profile image
Carlos Kassab • Edited

Hello everyone,

Do you think that we should keep variable names the same in the method that is calling and in the method that is getting the value?

Let's say we have a main method in Scala:

@main def create_etl_project = {
println("Please enter the project name, avoid using blank spaces:")
val project_Name = readLine().replaceAll("\s", "")

Create_Project_Directories(project_Name)
}

def Create_Project_Directories(project_Name: String) = {

}

Could you please let me know what do you think would be a best practice?

Thank you in advance.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

I saw a great one in a meeting today and had to share:

very.long.name.to.work.around.bug();

Collapse
 
pavlosisaris profile image
Paul Isaris

Totally agree with your point. Taking a step back and putting some thought on how to name a variable or a method can help your app have more SOLID foundations and save you hours of refactoring.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

I feel like it saves me a lot of time when I do small refactoring as I go instead of waiting until I have a 6000 line method to break apart all at once. (not to mention the time saved debugging when the only clue you have is "thing broke in main()"

Collapse
 
pavlosisaris profile image
Paul Isaris

I am not talking only about breking long methods to smaller ones, but conceptual refactoring when trying to name a variable/method. Often times I find myself struggling with coming up with a name, only to end up deciding that my method needs to be a part of a better designed class (or to be extracted to a new class), even before start writing it 😃

Collapse
 
timothymcgrath profile image
Timothy McGrath

Naming is so hard.
I'm also much better at pointing out problems in other developer's names in a PR than I am at naming things myself. 😀

Collapse
 
itachiuchiha profile image
Itachi Uchiha

What if the class name would be ”Customer” and its method name would be ”create”.

Is it good,

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

I think a case like this would actually make sense, because I assume you'd do something like Customer customer = new Customer(); to instantiate your class... then you would be doing customer.create() whenever you called that create method. I hadn't considered the combination of classes and their methods - you made a really good point! Thank you!