What Is Metaprogramming? – Part 2/2

1 mainThis is a continuation of my previous blog post on metaprogramming. In the previous post, we saw why we need metaprogramming. In this post, we will see what exactly this whole metaprogramming is all about. In the world of programming, the basic problem is that any general-purpose programming language is that it has its own limitations. If the language doesn’t have a metaprogramming model that is as pleasant and expressive as the language itself, then it will eventually frustrate the user. Nobody can design the perfect language, we get that! The people who manage these languages don’t allow the users to extend the language in generic ways. Not a bad thing altogether! This is done because a lot of people end up extending it in too many different ways and it becomes one big potpourri of nastiness. Coming back to the topic at hand, how exactly do we understand metaprogramming?   Continue reading “What Is Metaprogramming? – Part 2/2”

What Is Metaprogramming? – Part 1/2

1 mainYou know how people talk about creating machines that can create more machines by themselves? Well, we already have machines that can create other machines. The concept of metaprogramming is just the technical side of it. Metaprogramming is the process of writing programs that can create other programs. It is one of the most underused programming techniques. The good thing is that it allows programmers to minimize the number of lines of code to express a solution, or it gives programs greater flexibility to efficiently handle new situations without recompilation. If it’s so good, then why isn’t it used everywhere? Why do we need it? Before we jump into the details of metaprogramming, let’s understand why we would consider it in the first place.   Continue reading “What Is Metaprogramming? – Part 1/2”

Using Multiple CPU Cores With Command Line Tools

command lineAll of you must have heard about how the processors in our laptops have multiple cores. It’s good that the technology is advancing in that direction. When people write programs, they can utilize these cores to increase the speed of computation. But most of the inbuilt commands don’t use these cores unless specified explicitly. If you ever want to add up a very large list, say hundreds of megabytes, or just look through it to find some particular value, you would write a simple program to do it. But going through so much data takes a lot of time if you just use a single thread. The same is true for tools like grep, bzip2, wc, awk, sed, etc. If the last sentence looked like jibber-jabber, then you should probably google those things before you proceed. They are singly-threaded and will just use one CPU core. So how do we use multiple cores in these situations?   Continue reading “Using Multiple CPU Cores With Command Line Tools”

ifdef and ifndef

mainIf you have worked on a large code base before, you would have seen ifdef and ifndef statements a lot of times. In fact, it is a good practice to have them wherever necessary. It just adds more robustness to your code and if you are working in a team, it helps you avoid weird compiler issues. The preprocessor directive #ifdef is used to define conditional groups of code at the preprocessor level. Based on a condition, a piece of code may or may not be included in the program. The body of this directive is usually termed controlled text.   Continue reading “ifdef and ifndef”

CMake vs Make

mainProgrammers have been using CMake and Make for a long time now. When you join a big company or start working on a project with a large codebase, there are all these builds that you need to take care of. You must have seen those “CMakeLists.txt” files floating around. You are supposed to run “cmake” and “make” commands on the terminal. A lot of people just follow the instructions blindly, not really caring about why we need to do things in a certain way. What is this whole build process and why is it structured this way? What are the differences between CMake and Make? Does it matter? Are they interchangeable?   Continue reading “CMake vs Make”

Generating UIButton Programmatically

uibuttonWhen you are working on an iOS app, you invariably end up creating buttons on your view controllers. For some of the apps, you know exactly where everything goes and you place everything beforehand in your storyboard file. But if your app is more complex, you may want to create the buttons dynamically. This means is that creating a button might be interactive and can depend on the actions of the user. How do we create buttons programmatically? Once they are created, how do we associate actions with them?   Continue reading “Generating UIButton Programmatically”

Plus vs Minus In Objective-C

plus-minus-balance 3d MännchenWhenever you are working with an iOS app, you would have seen those functions and how they have a plus or a minus sign at the beginning. Ever wondered what they are? Objective-C requires that the interface and implementation of a class be in separate code blocks. That’s the reason you will see a .h and a .m file as a pair. Interface is the same as class definition in other languages. By convention, we place the interface in a header file and the implementation in a code file. In the header file, we declare functions with these ‘+’ and ‘-‘ signs. Do they make any real difference or do we have them just as part of the Objective-C language protocol? Well, they exist for a reason and there is a real difference between the signs.   Continue reading “Plus vs Minus In Objective-C”

Reading JPEG Into A Byte Array

Let’s say you are workBits_and_bytesing with images for your project. A lot of times, when you have to work across multiple platforms, the encoding doesn’t remain the same. In these scenarios, you cannot process images directly by treating them like 2D matrices. One of the most common image formats you will comes across is JPEG. If you are working on the same platform using a library like OpenCV, you can directly read JPEG files into 2D data structures. If not, you will have to read it into a byte array, process it and then encode it back. How do we do that?   Continue reading “Reading JPEG Into A Byte Array”

Programming Paradigms: Object Oriented vs Data Oriented

mainOver the last couple of decades, different programming paradigms have emerged in an attempt to make software better. Different situations demand different requirements, so it wouldn’t be fair to say that one paradigm is better than the other. Whenever you want to create a software system, you usually write code that attempts to fulfill most of the requirements. Now the difference between these paradigms is the way in which you write your code. On one end of the spectrum, we have object oriented programming. The world revolves around pieces of data here and functionalities are attached to them. On the other end of the spectrum, we have data oriented programming. In this paradigm, everything is just data. How does that work? What exactly is this spectrum and how do we understand it?   Continue reading “Programming Paradigms: Object Oriented vs Data Oriented”

How To Trigger A Segue Programmatically In iOS

mainYou have your storyboard ready and you have created the necessary segues. Great! Now how do we trigger these segues programmatically? There are many different ways to do it. One of the nicer ways to do this is by connecting two scenes in a storyboard using a segue that is not attached to an action. After that, you can programmatically trigger the segue inside your view controller. The way you do this is by control-dragging from the file owner icon at the bottom of the storyboard scene to the destination scene.   Continue reading “How To Trigger A Segue Programmatically In iOS”