Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Playlist Browser with Objective-C Building a Music Library Model Creating a Music Library

Shaun Kelly
Shaun Kelly
5,648 Points

Hard coded?

What does the term Hard coded mean ? mentioned at 9:26...ish

3 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Shaun,

Hard coded as he is using it is I believe the reference to strings or data (ie numbers) being used inside the body of your code that are not 'dynamic' in the sense that you can define in one place and use in many. A good practice is to define your strings and numbers into variables that describe what they are.

A good example of this with numbers is also referred to as 'magic numbers' for when numbers appear in your code that you may or may not know what they are.

#define TAG_FIRST_NAME  2

NSString *logInButtonText = @"SIGN UP";

In your code, it is much better to have :

UILabel *firstnameLabel = (UILabel*)[cell.contentView viewWithTag:TAG_FIRST_NAME];
firstnameLabel.text = logInButtonText;
// over say
UILabel *firstnameLabel = (UILabel*)[cell.contentView viewWithTag:2];
firstnameLabel.text = @"SIGN UP";
Shaun Kelly
Shaun Kelly
5,648 Points

Okay i think i understand... So when we hard code them we are manually setting a value for each object or whatever it may be. But when we are not hard coding we are setting the value with a variable that we can use over and over again. ?

//HARD CODE
self.view.backgroundColor = [UIColor greenColor];
self.firstButton.tintColor = [UIColor greenColor];
self.secondButton.tintColor = [UIColor greenColor];


//NOT HARD CODE
UIColor *greenColor = [UIColor greenColor];
self.view.backgroundColor = greenColor;
self.firstButton.tintColor = greenColor;
self.secondButton.tintColor = greenColor;
Damien Watson
Damien Watson
27,419 Points

You got it, I think this matches their 'DRY' policy of not repeating yourself, also making the code easier to read in most circumstances. The other thing being that you can set up your 'defaults' at the top of the page and change them in one place.

Shaun Kelly
Shaun Kelly
5,648 Points

What does it mean when your making a method dynamic? I understand that when you call the method you can set the value of the string but can you explain what does it mean when you make the method "Dynamic". And also what are parameters ?

-(void) createLabelWithString:(NSString)text {

NSString *string = text; }

Damien Watson
Damien Watson
27,419 Points

Hey Shaun, it would be best to break the above into a new question in the forum as questions can only have one correct answer, and answering these helps other students. That said, I will have a crack at answering it.

Making a method dynamic mostly refers to what it does or returns. If you have a method that does the same thing, or returns the same thing then it's not dynamic. If you can pass in something and get a result returned then this would be dynamic.

A property is a variable that you want the class to store. This can be available to other classes or locked to just be available inside the class.

// A property of class 'ExampleClass' is 'randomNum'.
// Dynamic method 'getRandomFromZeroTo' takes a user defined variable and returns a result.

@interface ExampleClass ()
@property int randomNum;
@end;

@implementation ExampleClass
-(void)viewDidLoad {
  self.randomNum = [self.getRandomFromZeroTo:100];
}

-(int)getRandomFromZeroTo:(int)maxNum {
  return arc4random() % maxNum;
}