Is it considered bad practice to write c code under objective-c / cocoa?

I am writing Objective-C code that will be publicly available. The code consists mainly of well-known algorithms that will benefit from optimization. I plan to write most of the code in C to reduce the overhead of creating objects and garbage collection. Is this a bad practice?

+6
objective-c
source share
4 answers

No no. This is done quite often.

As a rule, this is done when it is necessary for the critical time code to work faster (all others are equal, calling the C function is faster than the Objective-C method).

However, in some other cases, it is better to write C functions.

Remember that Objective-C is a superset of C. All that C can do, Objective-C can do, and should not be considered bad, to do anything that C can do. There may be times when some things not encouraged, but in any case.

+11
source share

Premature optimization is the root of all evil

Whether this is good practice or not depends entirely on the application. In most cases, I would say write it in Objective-C first, and then use the profiler to optimize if it is too slow.

However, there are cases when you know that you will need to do optimization, for example, if you are writing a language interpreter or processor emulation. In those cases (and in cases where profiling shows a bottleneck), it is great for writing pure C.

0
source share

No, it is not. Many of Apple's own frameworks mostly have C, like Core Graphics or Accelerate. C is very good for functions that you will use a lot, and they need to be run quickly, for example, mathematical functions.

0
source share

There are many cases where the OO model, especially with a single dispatch, simply does not make sense. For a general function, there may be no clear, preferred โ€œreceiverโ€. A language that supports universal functions for multiple mailing lists will be my preference, since such a language supports what most people think of as OO and much more, and also fully supports simple old C-style functions.

0
source share

All Articles