Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming complicated methods

I have a method for pulling data from a database, and I want it to get this:

Limit of Five entries, Item type is Newsletter, Needs to be active (PublishDate < DateTime.Now)

So I'm thinking of naming it GetFiveActiveNewslettersByCreatedDate()

This seems a little long to me. I looked on the site for a good way to name things like this, how would you handle it?

like image 382
Wesley Avatar asked Mar 12 '12 19:03

Wesley


People also ask

What are complex substituents?

When two chains of equal size are present. The chain with more number of side chains forms a part of the longest carbon chain while the other one is considered the complex substituent. On the complex substituent, numbering is done from the end attached to the principal chain.


2 Answers

How about something like this instead?

public IEnumerable<Newsletter> GetActiveNewsletters(int maxRecords = 5) 
{ 
    // ...
}

Top 5 is still the default, but it's not overly specific anymore.

like image 116
Ed S. Avatar answered Oct 06 '22 01:10

Ed S.


The reason I would avoid baking "five" into the name, personally, is what it might mean down the line.

For example, what if later, there were some demand for 10 newsletters in certain scenarios instead of 5? Well, you'd create an additional method GetTenActiveNewslettersByCreatedDate(). And now, you have a 'design pattern' that subsequent developers will follow when 20, 50, 100 newsletters are needed. This is a design that will rot, and you can stave it off now by parameterizing the five.

Of course, this might be YAGNI/speculative generality. If 5 really is some kind of magic, hard-fast, will never change rule, then you might cautiously bake it in. I just find that I've regretted doing and seeing things like that far, far more often than not.

like image 33
Erik Dietrich Avatar answered Oct 06 '22 00:10

Erik Dietrich