This is a question that lives in my mind from a long time.
Does the use of multiple nested conditional statements impact the performance of a taken code? So far I know that programmers have created a precise term to describe this situation, the in-famous "Pyramid of doom" [Wikipedia page], but a side from that how much can this degrade performances?
Let's take this situation as an example: I have a JSON
coming from a server response containing a few objects that represent the user purchases in an application, before taking the needed data from it I need to check various conditions:
- Is the response we are looking for?
- Does response has
resultCode: OK
? - Are the any contents to process?
At this point if everything is true
a "processing process" will begin, parsing and editing the server response, in this case for changing the date format from US to the standard european format. The process consist in two nested for-loop
s and an if statement
.
This is the example code in Objective-C
, since I'm an iOS Developer:
if ([remoteManager.operationId isEqualToString:NEEDED_OPERATION_ID]) {
if ([response[RESULT_CODE_KEY] isEqualToString:@"OK"]) {
if ([response[RESULT_OBJ_KEY][@"CONTENT_LIST"] count] > 0) {
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
for (NSMutableDictionary *productsAvailable in response[RESULT_OBJ_KEY][@"CONTENT_LIST"]) {
for (NSString *key in productsAvailable) {
if ([key isEqualToString:@"NEEDED_KEY"]) {
/* We change the date format here */
}
}
}
}
}
}
In my opining this code can't really have an impact on the performance of an application because those checks are not so complicated for the system to do. But does this can be a problem if we need to have the maximum speed and performances? In other words: should nested conditionals be avoided or not? And also does putting multiple conditions at the same time reduces the performance problems (if any)?