3

I'm programming in Objective-C. I want to include a camera library, DLCImagePicker, in my project. There is a variable I need to change, but it is private and I have no access to it.

I've thought of a few way to get around this, but am not sure on the best practice way to do it.

  1. I can just copy the code into my own class and alter the variable.

  2. I can alter it directly in the original class. However, I have included this code as a git submodule and I'm thinking that touching the source code is a bad idea in this case, and in general.

  3. Subclassing. I can't imagine how this would work, as the variable is private.

  4. I can perform some action with git that would create my own fork/branch of the project, and alter it there. I have never done this and am very primitive with git.

2
  • 1
    go 4th option. fork project in github just one button click. BTW, you can expose private variable to public by using category.
    – Bryan Chen
    Commented Sep 25, 2013 at 5:08
  • 5
    5. File an issue and provide a rationale of why this variable should be accessible from the outside code. Or maybe even submit a pull request if you think you know the best way to do it.
    – fjarri
    Commented Sep 25, 2013 at 6:32

2 Answers 2

3

You're kind of missing the point of open source, which is that in exchange for your use of software you find helpful, you're encouraged to contribute improvements back for everyone to benefit.

So fork the software on github, then make the changes as you would if you owned it. That means not just exposing a variable to make it easier to hack in external code, but employing proper encapsulation and adding whatever improved methods you need.

Then submit a pull request. Github has good tutorials on how to do this. The author will provide suggestions for improvement, then most likely merge it in, which benefits you by not requiring you to maintain a fork and benefits him by getting useful improvements to the code. In the unlikely event he doesn't accept the change, you have a fork available for your personal use. Also, most open source licences encourage if not outright require publishing changes, and your github fork fulfills that requirement.

4
  • @Philipp. Thanks. Both of your answers made the answer to my question very clear... A follow-up question - I've looked over the library and it looks like I might want to modify very heavily as I work on this component (the camera component of the app). I might end up changing perhaps 50% of the code in the main class... If this ends up being the solution, would forking be the proper way to start on this path? And then if it diverges enough to where it becomes a different project then I eventually just separate it into it's own project?
    – OdieO
    Commented Sep 25, 2013 at 18:14
  • 1
    That heavy of modifications, you probably want to run by the library author first. Either way, a github fork is the way to go. At least if he doesn't accept it, it will be easily found by other users. Commented Sep 25, 2013 at 18:16
  • OK, just to be super clear so I know where I'm heading. I fork it. If I end up making major mods then maybe the author accepts them all and the original project is now significantly altered. If they don't accept it then I either maintain my own fork or spin it off into a separate project. Is this right?
    – OdieO
    Commented Sep 25, 2013 at 18:22
  • Right. Keep in mind, they might accept part of your changes even if the entire thing isn't accepted. The closer you are to the official version, the easier it will be for you to maintain. Commented Sep 25, 2013 at 18:33
4

Doing marginal changes to open source software for your own needs is possible, but it can easily result in a maintenance nightmare.

Whenever the library updates, you have two options:

  1. ignore the update and all the improvements and bugfixes it brings
  2. merge your change into the new version of the new library

This merging can become more and more complicated should the codebase of the library evolve.

Another option would be to do what Karl Bielefeldt suggests and ask upstream to merge your change and put it into the official version. This, however, would only work under the premises that:

  • The change is useful for everyone, not just for you (did you consider to implement a setter-method for the private variable instead of just changing the hardcoded value?)
  • The change follows the conventions and long-term strategy of the project
  • The library is a bazaar-style project which accepts contributions from anyone without involving lots and lots of red tape. Most larger OSS projects unfortunately do not fall into this category.

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