#How to name method that MIGHT return an array

8 messages · Page 1 of 1 (latest)

spring lotus
#

getEditableOrEditableArray(id: string) ?

splitting it up into two methods seems wrong, but so does that method name

#

it's a method that reads this object: private dynContentDictionary: { [name: string]: DynamicContentDTO | DynamicContentDTO[]; };
and returns a refined one with the correct text localization

spring breach
#

getEditable<T>(id: string) and inside type guard

glass sequoia
#

splitting it up into two methods is the only thing that makes sense 🤣

lunar lynx
#

splitting it up into two methods seems wrong

https://en.wikipedia.org/wiki/Single-responsibility_principle

The single-responsibility principle (SRP) is a computer programming principle that states that "A module should be responsible to one, and only one, actor." The term actor refers to a group (consisting of one or more stakeholders or users) that requires a change in the module.
Robert C. Martin, the originator of the term, expresses the principle...

glass sequoia
#

Having to guess what the return type is a of a function is how we get horrors like JavaScript.

#

Functions are best when they can only return a single type.
Functions are acceptable when they return a single type or undefined (null if you feeling JS-y)

#

It is unclear to me what exactly that method is doing. But if there is a case where you are checking a single field or an array it could be either:

/** A method that checks a single case. */
const isEditable(id: string): boolean => this.thingy[id].isEditable;
/** A method that maps the single case to an array. */
const mapEditable(ids: string[]): boolean[] => ids.map((id: string): boolean => isEditable(id));

or

/** A method that always deals with arrays, and at worst you have a single item array. No Big Deal */
const isEditable(id: string[]): boolean[] => ids.map((id: string): boolean => this.thingy[id].isEditable);