Horror

Coding Horrors from Stackoverflow

Statement length to the extreme — a statement 443 character long!

    NSMutableArray *newArray = [[[NSMutableArray alloc] initWithArray:[NSJSONSerialization JSONObjectWithData:[[[appDelegate.Matches objectAtIndex:(NSUInteger)indexPath] objectForKey:@”chat”] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil]] addObject:[NSDictionary dictionaryWithObjectsAndKeys:message, @"message", [NSString stringWithFormat:@"%llu", appDelegate.userId], @”id”, nil]];

Formatted more sanely:

The intermediate variable names are unfortunately arbitrary, this is an example where intent is lost by not using good (or any) names.

    NSString       *string1       = [NSString stringWithFormat:@"%llu", appDelegate.userId];

    NSDictionary   *dictionary1   = [NSDictionary dictionaryWithObjectsAndKeys:message, @"message", string1, @"id", nil];

    NSDictionary   *dictionary2   = [appDelegate.Matches objectAtIndex:(NSUInteger)indexPath];

    NSString       *string2       = [dictionary2 objectForKey:@"chat"];

    NSData         *data1         = [string2 dataUsingEncoding:NSUTF8StringEncoding];

    NSArray        *array1        = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];

    NSMutableArray *mutableArray1 = [[NSMutableArray alloc] initWithArray:array1];

    NSMutableArray *newArray      = [mutableArray1 addObject:dictionary1];

Statement length to the extreme

self.allLessonsArray = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:@"Title", @"Description", @"LessonID", @"LessonSuffix", @"LibraryImage", @"Price", @"IsFree", nil] forKeys: [NSArray arrayWithObjects: @"First Lesson", @"This is a test description of our first lesson, this lesson is just a copy of Sample StoryApp which we will replace with our first Color Lesson for Very Small Kids. Design of that lesson is still to be discussed with our designer.", 1, @"Less1", @"LibLess1Image.jpg", 0, 1, nil]], [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:@"Title", @"Description", @"LessonID", @"LessonSuffix", @"LibraryImage", @"Price", @"IsFree", nil] forKeys: [NSArray arrayWithObjects: @"Second Lesson", @"I want to give some description to this title but its so time consuming and boring to write. So here i go - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", 2, @"Less2", @"LibLess2Image.png", 0.99, 0, nil]], nil];

Formatted more sanely:

  • Added NSNumber conversions as needed
  • The elimination of the repeat of the lessonKeys NSArray
  • Corrected the order of the keys and values
  • The above errors were difficult to notice the the original one-liner

NSArray *lessonKeys = [NSArray arrayWithObjects:

                       @"Title",

                       @"Description",

                       @"LessonID",

                       @"LessonSuffix",

                       @"LibraryImage",

                       @"Price",

                       @"IsFree",

                       nil];

NSArray *lesson1Values = [NSArray arrayWithObjects:

                          @"First Lesson",

                          @"This is a test description of our first lesson, this lesson is just a copy of Sample StoryApp which we will replace with our first Color Lesson for Very Small Kids. Design of that lesson is still to be discussed with our designer.",

                          [NSNumber numberWithInt:0],

                          @”Less1″,

                          @”LibLess1Image.jpg”,

                          [NSNumber numberWithInt:0],

                          [NSNumber numberWithInt:1],

                          nil];

NSArray *lesson2Values = [NSArray arrayWithObjects:

                          @"Second Lesson",

                          @"I want to give some description to this title but its so time consuming and boring to write. So here i go - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",

                          [NSNumber numberWithInt:2],

                          @”Less2″,

                          @”LibLess2Image.png”,

                          [NSNumber numberWithFloat:0.99 ],

                          [NSNumber numberWithInt:0],

                          nil];

NSDictionary *lesson1 = [NSDictionary dictionaryWithObjects: lesson1Values forKeys: lessonKeys];

NSDictionary *lesson2 = [NSDictionary dictionaryWithObjects: lesson2Values forKeys: lessonKeys];

self.allLessonsArray = [NSArray arrayWithObjects: lesson1, lesson2, nil];

With llvm 4.0 new literal syntax things just get better:

NSArray *lessonKeys = @[ @"Title",

                         @"Description",

                         @"LessonID",

                         @"LessonSuffix",

                         @"LibraryImage",

                         @"Price",

                         @"IsFree" ];

NSArray *lesson1Values = @[ @"First Lesson",

                            @"This is a test description of our first lesson, this lesson is just a copy of Sample StoryApp which we will replace with our first Color Lesson for Very Small Kids. Design of that lesson is still to be discussed with our designer.",

                            @0,

                            @"Less1",

                            @"LibLess1Image.jpg",

                            @0,

                            @1 ];

NSArray *lesson2Values = @[ @"Second Lesson",

                            @"I want to give some description to this title but its so time consuming and boring to write. So here i go - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",

                            @2,

                            @"Less2",

                            @"LibLess2Image.png",

                            @0.99 ,

                            @0 ];

NSDictionary *lesson1 = @{ lessonKeys : lesson1Values };

NSDictionary *lesson2 = @{ lessonKeys : lesson2Values };

self.allLessonsArray = @[ lesson1, lesson2 ];

Still this might be better in a plist file that is read. That would allow making changes and adding lessons without requiring code changes.

All to often

NSMutableDictionary *fileDic = [[[NSMutableDictionary alloc] init] autorelease];

fileDic = [file readFile];

The second line assigns the result of [file readFile] making the allocation of fileDic in the first line irrelevant.

Amazement

One can only view with amazement and contemplate the horror a maintainer will experience. Oh BTW, it does cause a crash under some levels of compiler optimization.

unsigned char* _pos = …;

double result;

*((int*)&result) = *((int*)_pos);

Let’s see: taking bytes of int size and moving them to a double.

    BOOL fail = [[NSUserDefaults standardUserDefaults] synchronize];

    

    if (!fail){

        NSLog(@”success.”);

    } else {

        NSLog(@”fail.”);

    }

    

    BOOL status = [[NSUserDefaults standardUserDefaults] synchronize];

    

    if (status == YES){

        NSLog(@”success.”);

    } else {

        NSLog(@”fail.”);

    }

Leave a Reply