Pokazywanie postów oznaczonych etykietą guide. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą guide. Pokaż wszystkie posty

środa, 25 listopada 2015

Things I've learned while building an iOS SDK

There are times in programming when you think "I wish I knew that from the beginning". Building an SDK is quite different from making an app. You have more things to remember about and the distribution is more tricky. It's also a big responsibility, because if you screw something up, you can't just update right away - you might have a lot of users, and you don't control when they update your library and push to the store. This is a list of my i-wish-i-knews when building the Pulsate SDK. 

My SDK is too big!

I've started building the SDK with writing it from scratch, instead of refactoring the old code. One of my goals was to make the SDK smaller, as it was about 20 mb. When I finished an early version I was shocked to find out that the SDK still weighs the same. Only when I've educated myself about how does clang actually work - while writing this post - I realized that it's ok.

There's a big difference between a library and an executable. Library contains a lot of metadata that weighs a lot. Also, you want your static library to contain a lot of slices, including the simulator ones. The final executable strips all unneeded parts, making our demo executables with as much as 1 mb (with all the graphical assets etc.).

Also, Xcode 7 allows you to use bitcode which will reduce the executable on user's device even more. Remember to make sure you support bitcode in your library by using bitcode linker flags - if you don't, inform your users about that!

Watch out for symbol conflicts

Another big difference when building a library is that it's meant to be linked with someone else's code. Not only do you have to be careful to add prefixes to your classes to avoid symbol duplicates, but you have to make sure that you don't ship your dependencies (like AFNetworking) without making sure they're not going to clash with users using them too - It's not a problem if your code is open source and you're using CocoaPods, but it's a problem if you're shipping a compiled library. I've explained it in more detail in my other blog post.

Want to distribute compiled library through CocoaPods?

No problem! I didn't think it was possible, but it is and it's very easy. Our SDK is for commercial use only, so we couldn't open source it obviously. Check out our repo for the .podspec. 

Automate your process

It saves a lot of time to automate the building and distribution process. For a while I was using lipo to create a fat static library, commiting to GitHub and CocoaPods manually. I've written custom scripts that do that automatically now. It takes a couple of seconds to create a new release.

Here's my XCode script that builds both device and simulator, creating a fat library out of them and putting them in the release version folder.


Some things about the .bundle

If you need to ship your .framework with a .bundle (if you have some assets, or a CoreData model) there are some things you need to know.

The first thing that surprised me was that when we tried to publish an app with our SDK, it got rejected. Xcode automatically generates an executable and puts that executable info into the .plist file. You have to remove that executable, and .plist line from the .bundle in order for it to be accepted on iTunes. More here.

You can also distribute the .bundle file with CocoaPods.

Your users might not use some features. Create separate bundles, if you have a lot of assets, so users can choose which assets they need.

Some other tips

• Make sure you always distribute the release version
• Make sure you have a way to see what version of the SDK this is, just by looking into files. It's very useful
• Make sure you add the simulator slices
• If you're using a different server for testing, or any debug features and different for production use C macros and define a DEBUG identifier for debug builds so you make sure it won't get leaked into production like it just did for Vine.



piątek, 26 grudnia 2014

Your code sucks - things you need to know about clean code and architecture

    
 Although the title is an exaggeration, all code I've seen in my (short) professional career did indeed suck. Because it's my first post, it'd be appropriate to talk about something very important to me: code quality. 


I want this post to be something of a quick collection of references for programmers on how can they recognize if their code sucks and how should they try to fix it. I'll avoid getting into details or code examples.

I'd like to say it's targeted to junior programmers, but I know for a fact that there are some senior programmers out there that didn't hear of such basic concepts as Dependency injection or Don't-Repeat-Yourself principle.

Clean up your code, would you ?

I wish I ruled the world and that's just for one selfish reason: force all programmers to read this book. If all developers followed the simple rules described in Clean Code by Robert C. Martin the IT world would be so much nicer.

It would most definitely prevent such classes as Start from being created. It's actually a pretty decent one compared to some other classes on the repo of Polish Electoral Calculator client, which is only... one of the most important programs in the country. 

There are a few basic rules a decent developer should follow:

  • Replace comments with meaningful method/variable names. 
    Code should comment itself
  • Methods should do one thing and one thing only
  • Methods should be very short 
    If a method is longer than 10-30 lines, consider dividing it into separate methods
  • Don't keep dead code - your repo is your history, not commented code
  • Don't use magic strings and magic numbers
    The only exception I use are log and error mesage strings. It would be tiresome to create a constant for every possible log.
  • If a class is very long, consider dividing it
  • Don't use snake expressions
    If one expression is long, divide it into several different expressions
  • Don't repeat yourself
    If you have two blocks of similiar looking code, it means it probably needs to be put in one method
And probably many more I missed. The point here is: if you didn't know about at least one of these rules, that's more the reason to browse through the book.

My two cents

I find comments and macros very useful when it comes to arranging insides of your file. 

I like to keep my private methods and public methods divided by comments/macros.


I also put comments before every method to put method/parameters/return information for documentation (check out automatic documentation generators for IDE you're using, to see what format to use) and put a clear sign that "this method starts here". When you open the DOOM 3 source files you immediately notice when methods start. Actually, DOOM3 source code is pretty good example of clean code, here's a good blog post about that:" The exceptional bueaty of DOOM 3`s source code".


“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Someone

Design Patterns

If you're a developer and you've never heard of design patterns, well then better hurry: click.
Even if you find out that you don't like to use design patterns, you have to know them by heart. Why? It has an undeniable advantage: it's a universal language of software architecture.

You are the architect

Your classes are your blocks, your program is your city. The bigger the city, the better architect you have to be to prevent the city from falling apart.

Architecture is a huge chunk of computer software. It's not something you learn in a short blog post. It's also unique for every programming language or kind of program. But hey, we can always cover some basics, right ?

The most basic thing you need to follow is:

 Keep your program modular (loosely coupled)

It's easier to look at a program that's just simple modules talking to each other, than a growing spaghetti monster. It's also easier to switch stuff around.

Example

Let's say you have a Client class that uses a SQLite Database object to write and read from that database. One day your project manager tells you that SQLite sucks and he wants to to write and read to a file instead.

Interface


How can we switch writing to database to writing to a file ? They seem like two very distant things. Yeah, but they have something in common, they're both writing to some local storage. Let's make an interface/protocol/abstract class LocalStorage. Make SQLiteImplementation, and FileReadWriteImplementation, conform to this interface and make the Client talk to LocalStorage object. It explains a one simple rule:



 Always separate your interface, from your concrete classes if you have more than one implementation.

It's done using a simple design pattern called Bridge, or Strategy. They're the same thing implementation-wise but have different uses. 

IoC

How can we tell the Client which implementation to use ? Doesn't the Client create his own LocalStorage objects ? Well, it does, if the code is tightly coupled. 

In a perfect scenario, something from outside tells the Client which local storage to use. It's called Inversion of Control and the most popular implementation of that principle is Dependency Injection. We inject (tell the Client) the dependencies (Client depends on the database) to the class. Which brings us to our next rule:


 Use Dependency Injection (IoC)


It makes code highly reusable, losely coupled, easy to test, plus it has a great advantage while creating Unit Tests.



A little extra: Using mocks in unit testing


How do you write unit tests for server calls? Calls to databases without actually connecting to one ? Or how do you unit test if you still haven't finished some modules ? It's easy if you're using Dependency Injection.



Let's say we want to test our Client but we don't have access to the database. Just create a class LocalStorageMockImplementation that extends the LocalStorage interface and inject that class into the Client. Now you can test your client properly. Here's a quick tutorial if you want to know more.

Let it sink in


In my opinion, these are the basics that every developer should know, before writing a single line of code. It's not a lot to take in, and by sticking to these rules, you create a readable, highly reusable, changeable code - a miracle I have yet to experience.