Avoid push_back
Avoid doing this:
std::vector<CKFlexboxComponentChild> children;if (someSetting) {children.push_back({[HeaderComponent newWithModel:model});}if (fooComponent) {children.push_back({fooComponent});}return [super newWithComponent:[CKFlexboxComponentnewWithView:{}size:{}style:{}children:children]];
There are a few reasons why:
- There are good reasons to avoid mutable local variables in general.
nil
children are automatically dropped byCKFlexboxComponent
, so there's no need to check fornil
before adding to the list of children.- Conditionals are best done inline; or, encapsulate the checks in a new
CKCompositeComponent
that renders tonil
if the conditional fails.
Instead, do this:
return [super newWithComponent:[CKFlexboxComponentnewWithView:{}size:{}style:{}children:{{someSetting ? [HeaderComponent newWithModel:model] : nil},{fooComponent},}]];