1

I'm working on a command line interface text based game. I'm writing it in C but there are various ways in which I could refactor the code by using Objective-C:

  • using NSDictionary to allow me to store data in Plists (easier to read/write because of the Foundation framework but much larger in size) instead of plain text

  • having classes with methods (not a big thing but increases code readability)

  • eliminating the need of converting between an object's char* name and its int/enum equivalent (using and enum to represent things reduces memory usage and complexity when using plain non-OO C, otherwise I would have to use strncmp everywhere)

What I want to know is whether or not there are disadvantages to mixing OO- and non-OOP. It's still command line, which means most of it will be written in pure non-OO C.

Are there any reasons for which I shouldn't mix these?

  • How do you attend to mix the two different languages ? I don't know much about Objective-C, but do you intend to compile C with Objective-C compiler ? – Arthur Havlicek Sep 14 '15 at 13:05
  • I have gcc which I got from Xcode's command line tools. The only ObjC features I need are listed in my question. The idea is that I will use some of the classes in ObjC to simplify various operations – Arc676 Sep 14 '15 at 13:20
  • 1
    You might look at this SO thread on the topic - stackoverflow.com/questions/801976/… Search for others – Brandin Sep 14 '15 at 13:48
  • 3
    @ArthurHavlicek: see Wikipedia here, Objective-C is a strict superset of C. – Doc Brown Sep 14 '15 at 15:55
4
  1. Object Orientation is not primarily a language feature. It is a programming paradigm/method/style. That is, you can write perfectly object oriented code in pure C, and you can just as well write perfectly non-object oriented code in Java.

  2. Object Orientation is not a virtue in itself. It is helpful most of the time, and thus rightly encouraged heavily, but there are use cases where a non-object oriented solution works just as well or better.

  3. There is no problem with mixing object oriented and non-object oriented code in the same project. Object oriented code can call non-object oriented code and vice-versa.

    The only problem that may come up is mixing code written in different languages. But, as Doc Brown rightly notes in the comments, Objective-C is a strict superset of C, which means that you can just compile any C code as Objective-C. This is very different from trying to compile C code as C++ code, which will force changes in many places. But that is not your problem, you can easily switch from a .c suffix to a .m suffix wherever you need it without breaking the code in the file.

That said, your main focus when refactoring your game should be to simplify your code. If you know object orientation well, simplifying your code will likely mean making it heavily object oriented. If you don't know how to use object orientation well yet, try to find at least two different object oriented solutions for each problem, and try to choose the better one in each case. But don't try to keep object oriented and non-object oriented code separate.


Concerning your enum/char* problem: I would suggest that you read up on string interning and try to implement it in your game. Because, once you have interned strings, you can simply compare your strings by comparing their pointer. That should allow you to reduce the enum usage in a flexible and performant way.

  • Any obvious solution on string interning in C? – Arc676 Sep 15 '15 at 8:29
  • @Arc676 What do you mean? – cmaster Sep 15 '15 at 20:37
  • The Wikipedia link doesn't say that C has a built in function for this. This answer says that string interning is extremely hard in C. Were you suggesting using string interning in ObjC? – Arc676 Sep 16 '15 at 8:24
  • No, C does not have a "built-in" for this. There may be libraries that provide such a functionality, though. But I would disagree that it is "extremely hard" to implement in C (or Objective-C for that matter). It's just a matter of managing a background list (preferably a hash table) of all string objects in existence, ensuring that no two of these objects are equal. Of course, you either need to implement reference counting or live with the fact that interned strings can't be destroyed. I did this fully fledged in C++, and it's just 170 lines there. I guess the C version would be around 220. – cmaster Sep 16 '15 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.