Skip to content

Instantly share code, notes, and snippets.

@mharju
Created August 27, 2012 06:13
Show Gist options
  • Save mharju/3486133 to your computer and use it in GitHub Desktop.
Save mharju/3486133 to your computer and use it in GitHub Desktop.
Code duplication and complexity
/*
The most trivial implementation of what I need. Introduces code
duplication and can become a hassle to maintain if more and more
data sources are introduced.
What would be the best approach to keep the code organization as
simple as possible but on the other hand keep the clarity of this
two data source example?
When should a programmer "feel the pain" of code duplication?
For example I think in this example I'd keep it like it is
now to make the intention and code structure more clear. I think this
is effectively in its current form just "code unrolling". There are
less function calls and the functionality is more centralized and comprehendable.
An obvious way to optimize this is to introduce functions that extract the common
functionalities and use them in their current places. I think
the end result would become less obvious when it's been looked over
again some time later. And the basic structure of switches would still exist unless there
is another layer that maps view tags to actions.
Another way would be to have a declarative description of
the situation and it would generate this code that would be again
easy to read but probably hard to maintain as-it-is.
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(tableView.tag) {
case kLatestActionsViewTag:
return [[Battle sharedBattle].latestActions count];
case kTopPlayersViewTag:
return [[Battle sharedBattle].topPlayers count];
default:
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
Battle *battle = [Battle sharedBattle];
switch(tableView.tag) {
case kLatestActionsViewTag: {
cell = [tableView dequeueReusableCellWithIdentifier:@"BattleLatestActionCell"];
Action *current = [battle.latestActions objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", current.player, current.type];
cell.detailTextLabel.text = [NSDate relativeTimeStringSinceDate:current.time];
break;
}
case kTopPlayersViewTag: {
cell = [tableView dequeueReusableCellWithIdentifier:@"BattleTopPlayerCell"];
BasePlayer *player = [battle.topPlayers objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%d. %@", player.position, player.name];
cell.detailTextLabel.text = player.team;
break;
}
}
return cell;
}
- (void)battleModel:(Battle*)battle didUpdateField:(BattleModelField)field
{
switch (field) {
case kBattleModelFieldCurrentPlayer:
{
UILabel *label = (UILabel*)[self.view viewWithTag:656];
label.text = [NSString stringWithFormat:@"%@ – %d pistettä", battle.currentPlayer.name, battle.currentPlayer.points];
[self.indicator stopAnimating];
break;
}
case kBattleModelFieldLatestActions:
{
UITableView* latest = (UITableView*)[self.view viewWithTag:kLatestActionsViewTag];
[latest reloadData];
break;
}
case kBattleModelFieldTopPlayers:
{
UITableView* topPlayers = (UITableView*)[self.view viewWithTag:kTopPlayersViewTag];
[topPlayers reloadData];
break;
}
default:
break;
}
}
- (IBAction)segmentChanged:(UISegmentedControl *)sender {
switch(sender.selectedSegmentIndex) {
case 0:
break;
case kLatestActionsViewTag:
[[self.view viewWithTag:kLatestActionsViewTag] setHidden:NO];
[[self.view viewWithTag:kTopPlayersViewTag] setHidden:YES];
break;
case kTopPlayersViewTag:
[[self.view viewWithTag:kLatestActionsViewTag] setHidden:YES];
[[self.view viewWithTag:kTopPlayersViewTag] setHidden:NO];
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment