Truly Awful Code

So, on the back of my previous post about people designing applications for mobile devices having never even used one, I am now working for an employer who would appear to have never read any guides on how to code in Objective C – or any kind of language for that matter.

We’ll start with this simple bit of code that is not only written badly, it also contains 2 errors! ¬†Assuming this person had any schooling, I think they missed the classes on “else if”, “switch statements”, “enumeration” and “top down programming”.

[sourcecode lang=”c”]
if (yourScore < 12) { gameState = 1; } if (yourScore > 12) {
gameState = 2;
}
if (yourScore > 24) {
gameState = 3;
}
if (yourScore > 37) {
gameState = 4;
}
if (yourScore > 49) {
gameState = 5;
}
if (yourScore > 62) {
gameState = 6;
}
if (yourScore > 74) {
gameState = 7;
}
if (yourScore > 87) {
gameState = 8;
}
if (yourScore == 100) {
gameState = 10;
}
[/sourcecode]

The two errors are:

  • How does one achieve game state 9
  • And what if a persons score == 12

This next snippet, from the same company, had me lost for words. As I am working from home and I have never met the employer, I am beginning to wonder whether they aren’t some 12 year old skipping school and getting an early start creating their own business.
[sourcecode lang=”c”]
// if under 10 we prefix a 0
if (Row < 10) { tempRowString = [NSString stringWithFormat:@"%02d",Row]; } else { tempRowString = [NSString stringWithFormat:@"%d",Row]; } [/sourcecode] These aren't one off's. The whole code base is riddled with statements like these. The following snippets, which could be ignored by themselves, create an unnecessary large code base, code that will run slower on the target device or generally complicate what should be straight forward statements. [sourcecode lang="c"] // set the challlneger and challenger name , make sure we dont lose reference NSString *tempChallengerName = [[NSString alloc] initWithFormat:@"%@",[parts objectAtIndex:0]]; self.challengerName = tempChallengerName; [tempChallengerName release],tempChallengerName = nil; [/sourcecode] [sourcecode lang="c"] NSString *WBUserNameString = [NSString stringWithFormat:@"WB UserName:%s\n",messageChallenger->playerName];
WBUserName.text = WBUserNameString;

NSString *GCUserIdString = [NSString stringWithFormat:@”GC UserId:%@”,playerID];
GCUserId.text = GCUserIdString;

GKPlayer *player = [[GameCenterHelper sharedInstance].playersDict objectForKey:playerID];
NSString *GCUserNameString = [NSString stringWithFormat:@”GC UserName:%@”,player.alias];
GCUserName.text = GCUserNameString;
[/sourcecode]
I have spared you the vast swathes of white space between statements, misaligned indentations, and poor logic.

What really pisses me off, is that these inbreds who can barely code, are being paid a small fortune for this via some relatively popular apps on the app store and I am being paid a pittance (in comparison) for going through their code and doing it properly.

Another example of their wonderful style.

[sourcecode lang=”c”]
-(void)setSoundVolume
{
// play background sound if not muted
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
int intMusic = [userDefaults integerForKey:kMusicLevel];
NSLog(@”INT KMUSIC LEVEL %i”,intMusic);
if (intMusic == 1) {
NSLog(@”NOT Playing background music and setting to level 1″);
//[[SoundManager sharedManager] playMusic:@”Jack-Beats-aac” looping:YES];
[[SoundManager sharedManager] setMusicVolume:1];
}
if (intMusic == 2) {
NSLog(@”Playing background music and setting to level 2″);
[[SoundManager sharedManager] playMusic:@”Jack-Beats-aac” looping:YES];
[[SoundManager sharedManager] setMusicVolume:2];
}

if (intMusic == 3) {
NSLog(@”Playing background music and setting to level 3″);

[[SoundManager sharedManager] playMusic:@”Jack-Beats-aac” looping:YES];
[[SoundManager sharedManager] setMusicVolume:3];
}

if (intMusic == 4) {
NSLog(@”Playing background music and setting to level 4″);

[[SoundManager sharedManager] playMusic:@”Jack-Beats-aac” looping:YES];
[[SoundManager sharedManager] setMusicVolume:4];
}

if (!intMusic) {
NSLog(@”Playing background music and setting to level 4 intMusic is nil so not set in defaults”);

[[SoundManager sharedManager] playMusic:@”Jack-Beats-aac” looping:YES];
[[SoundManager sharedManager] setMusicVolume:4];
}

//// set audio level on launch
int intAudio = [userDefaults integerForKey:kAudioLevel];

if (intAudio == 1) {
NSLog(@”Audio setting to level 1″);

[[SoundManager sharedManager] setSoundVolume:1];
}
if (intAudio == 2) {
NSLog(@”Audio setting to level 2″);

[[SoundManager sharedManager] setSoundVolume:2];
}

if (intAudio == 3) {
NSLog(@”Audio setting to level 3″);

[[SoundManager sharedManager] setSoundVolume:3];
}
if (intAudio == 4) {
NSLog(@”Audio setting to level 4″);

[[SoundManager sharedManager] setSoundVolume:4];
}
}
[/sourcecode]

Leave a Reply