Archive for the ‘oop’ Tag

How to Use Properties to Better Encapsulate Your Code?   Leave a comment

Download ExampleDifficulty: easy moderate challenging

In this article I will explore the usage of properties in order to simplify, bulletproof, and even better encapsulate your code. These are not merely slogans. By the advanced use of properties [and categories], you’ll see how we save coding time, write less implementation details, make our code less error-prone, and make other classes who use our class more independent.


The standard use of properties: to create fields your and other classes can modify and access was thoroughly explained in my previous post: What are Properties and how do they work?. Instead, in this article, we’ll deal with three distinct types of variable usages, whose names I made up, and standardize their implementation using properties. We’ll start with Internal Variables – variables that are to be used only within our implementation, we’ll continue to Seemingly Read-Only Variables – variables that are read-only to all classes but our implementation – and lastly, we’ll discuss General-Specific Variables – variables that are exposed to other classes as a superclass of what they are, whom merits will be explained. We trouble ourselves with coding these cases using properties in order to exclude any implementation details from our code, for instance, by not using retain or release at all. Plus, since the memory options of properties (retain, assign, and copy) are easily changeable from one to another, it will allow for faster and less-buggy code changes later.

Read the rest of this entry »


What are Properties and how do they work?   2 comments

Difficulty: easy moderate challenging

Properties are a way for programmers to simplify the use – or to perhaps minimize the misuse – of handling instance variables, especially in regard to how their memory management is being done. Although properties support scalar C data variables such as BOOL, int, etc., their strength exhibits while dealing with NSObjects, when memory management becomes an issue. If you ever noticed properties and didn’t quite understand what the heck, than I would start by saying that properties are not so complicated as they seem at first and there is no voodoo happening that makes them work – they are merely a different syntactic notation for what you normally code in Objective-C. Properties modify two aspects of your code: 1) they streamline how users of a class access its variables, 2) they simplify memory management for classes, by minimizing or even omitting implementation details of the variables’ memory management. If the previous sentence or even the entire paragraph were difficult to understand, read it again after finishing the article and then see if it makes sense.

Read the rest of this entry »

%d bloggers like this: