4 ms·
One approach that can help is to name things based on what the functions actually do. validateSortDisplayedItems { validation Logic ... sortDispla
by Retric 11y ago
One approach that can help is to name things based on what the functions actually do.
validateSortDisplayedItems
{
validation Logic
...
sortDisplayedItems(); //Actually sorts items.
}
This can be harder to maintain, but really long names end up a useful code smell.
- ergothus 11y agoI find it a bit...incorrect. I mean, your above code LIES. If I call validateSortDisplayedItems, I don't validate, I validate AND sort. Plus, what do you do if you have "validateItems" and "sortItems", and then one function that calls them each in turn? call it "validateAndSortItems"? Yuck.
- Retric 11y agoDepends on the Validation Logic. I like the style that basically works like this: validateSortDisplayedItems { if(!DisplayedItemsValid()) { CorrectDisplayedItems(); if(!DisplayedItemsValid()) { DisplayValidationError(); return; } } sortDisplayedItems(); //Actually sorts items. } AKA validate means try and make valid, not verify that data isValid. So, you can't just do if(isValid) sort; the bonus is unrecoverable errors end up at leaf nodes vs. the happy path. At the high level, your function might be sortClicked, which can then respond to that by calling a wide range of functions. (userCanSort,SortData,UpdateDisplay) PS: I find the validate > correct loop is generally the important and error prone part of code, so I give it priority. The happy path where everything works is more or less an addendum.
- kazinator 11y agoThe problem is that the validateSortDisplayItems is some sort of public function. The caller doesn't care about the validation, only that the items are sorted. All kinds of functions everywhere validate their arguments; that doesn't deserve to be in their name.
- Retric 11y agoThe question is why you need a helper function? IMO, public functions are a special case and your generally better off using different naming schemes for internal and external functions. Granted, if it's pure pass-through then reusing the name is a non issue. Foo(x){foo(x);} is not confusing. Foo(x){junk; foo(x);} is. One option is Foo(x){validateForFoo; ValidedFoo(x);} Alternatively for private functions JunkFoo(x){junk(x); foo(x);} And replacing JunkFoo with a meaningful name if possible. Again though, public function Foo(x) should really call internal function Bar(x) otherwise it's really easy to couple things on both sides of an interface.
- kazinator 11y agoThe computation represented by "foo" could actually not be contributing any new semantics to "foo". It could just be some boiler plate that is needed to extract and isolate various resources out of context "x", so that these pieces can then be passed to the low-level "foo", which knows nothing of the aggregate "x". "junk" could be some locking an dunlocking, or debugging, or preparing the display in some way or whatever. If someone wants to know how to name the low-level helper which does the real work and the interface over it, we just have to take it for granted that the separation is necessary and that it makes sense for either function to have the name "displaySortedItems" or whatever.
- Retric 11y agoIf your stripping boilerplate then your operating on different things. Which seems more readable? SortGuiElement(){junk; SortUserNames(); junk;} SortUserNames(){junk;} vs. SortNames(){junk; sortNames() junk;} sortNames(){junk;} It might seem obvious and the code might be identical, but I see the second case very frequently in other peoples code.
- kazinator 11y agoRe-using an identifier with just a case difference is criminal. You want: SortNames(){junk; SortNamesImpl(); junk;} sortNames(){junk;} or whatever: SortNamesGuts(), InternalSortNames(), DoSortNames(), LowLevelSortNames(). Anything but just flipping the case of a letter or two in the "SortNames" identifier.
- deleted 11y ago[deleted]