is it considered bad practice to write c code in objective-c / cocoa framework?
I am writing some Objective-C code which will be publicl开发者_如何学Cy available. The code mainly consists of well known algorithms which would benefit from optimization. I am planning on writing most of the code in C to reduce the overhead on creating objects and garbage collection. Is this considered bad practice?
No it's not. This is done quite frequently actually.
Generally one does this when one needs time critical code to run quicker (everything else equal, calling a C function is quicker than an Objective-C method).
However, it may also be nicer in some other cases to write C functions instead.
Remember though that Objective-C is a superset of C. Everything C can do, Objective-C can do, and it should not be considered bad to do anything C can do. There may be cases where doing some things is discouraged, but anyway.
Premature optimisation is the root of all evil
Whether it is good practice or not depends entirely upon the application. In most cases, I would say write it in Objective-C first then use a profiler for optimisation if it is too slow.
However, there are cases where you know you'll be needing to do optimisation, for example if you are writing a language interpreter or a CPU emulation. In those cases (and in cases where profiling shows a bottleneck) it's perfectly fine to write pure C.
No its not, Many of Apple's own frameworks are mostly C like Core Graphics or Accelerate. C is very good for functions that you will use alot and need to run fast, like math functions.
There are slews of cases where an OO model, especially with single dispatch, just does not make sense. For a general function there may be no clear preferred "receiver". A language that supports multi-dispatch generic functions would be my preference as such a language supports both what most people think of as OO and so much more as well as fully supporting just plain old C style functions.
精彩评论