Hacker News new | past | comments | ask | show | jobs | submit login

I disagree with this. I think something should be a seperate function only if it is (or likely will be) called from several callsites.

If you try to not always think about contrived cases while writing code, then this rule can sometimes lead to quite long functions simply from executing steps in sequence ("script").

One can split what is really a long function into several small ones for readability, but if one does so I think one should declare that function within the calling function (in languages that support this), and whether one does that or not is really about as insignificant as tab vs spaces or where one likes to have curly braces.

My point is, if a function only has one callsite, and is not likely to get more (incl. test code), then whether to inline that function or write it seperately is a style/formatting issue, not a maintainability issue.




> I disagree with this. I think something should be a seperate function only if it is (or likely will be) called from several callsites.

I disagree. I split out anything that I can describe/test separately. If I need to take a list and

1. Filter it based on custom logic 2. Sort it based on custom logic

Then I will generally break the filter and sort out into their own functions even if they're not used anywhere else. Each function can have it's own description and tests.


I understand that many prefer this. My point wasn't really so much mine or your preference -- my point was that it is a question of stylistic preference, not about spaghetti vs not.

As you simply describe your own stylistic preference I am not sure if you agree with me about that or not?

If you actually write a seperate test for the function then you have two callsites and of course it must be a seperate function.


Even if you have a few callsites and just copy-paste the same function it can be readable - I've heard that called un-cooked spaghetti code. Everything is still nicely layed out and can be refactored without too much trouble if you so chose. The problem starts with cooked spaghetti code where everything is all mangled up and references are all over the place.


I don't know if I'm buying it or not but I like this cooked/uncooked spaghetti code distinction




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: