ios - should I avoid using new to initialize my objects? -
this question has answer here:
- use of alloc init instead of new 8 answers
recently told apple discouraging use of new, , google's ios coding standards has say:
do not invoke nsobject class method new, nor override in subclass. instead, use alloc , init methods instantiate retained objects. modern objective-c code explicitly calls alloc , init method create , retain object. new class method used, makes reviewing code correct memory management more difficult.
why make reviewing code correct memory management more difficult though?
i expect reference ease of code reviews merely meant human readers of code may not notice word 'new' eyes scan on code looking alloc-init calls.
in objective-c, word 'new' shortcut calling alloc , init. cannot pass arguments; calling no-arg constructor. if later change code in such way want call 1 of other constructors , pass arguments, need change "new" alloc-init. common enough yet anothe reason avoid calling 'new' in first place.
there no advantage calling 'new' on calling alloc-init. 'new' word in objective-c because other languages such java use keyword.
Comments
Post a Comment