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 trialMichael Williams
Courses Plus Student 8,059 PointsGetting better at reading code.
Does anyone have any resources on how to get better at reading code? For example, I completed the lessons on Structs and Classes in Swift, and ended up with the following code. Overall, I understand what it's supposed to do, but if I go line by line, I get lost and confused and sometimes flat out don't know what's referring to what. Maybe I'm being too hard on myself, but I feel that if I've come this far, I should be able to understand the code more easily than I do:
struct Point { //We're creating a blueprint from which we can create many different points
let x: Int //Here, we are creating two stored properties
let y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
/// Returns the surrounding points in range of
/// the current point.
func points(inRange range: Int = 1) -> [Point] {
var results = [Point]()
let lowerBoundOfXRange = x - range
let upperBoundOfXRange = x + range
let lowerBoundOfYRange = y - range
let upperBoundOfYRange = y + range
for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange {
for yCoordinate in lowerBoundOfYRange...upperBoundOfYRange {
let coordinatePoint = Point(x: xCoordinate, y: yCoordinate)
results.append(coordinatePoint)
}
}
print(results)
return results
}
}
let coordinatePoint = Point(x: 0, y: 0) //This creates an instance of the struct.
//coordinatePoint.x
//coordinatePoint.points()
// Enemies
class Enemy {
var life: Int = 2
let position: Point
init(x: Int, y: Int) {
self.position = Point(x: x, y: y)
}
func decreaseLife(by factor: Int) {
life -= factor
}
}
class SuperEnemy: Enemy {
let isSuper: Bool = true
override init(x: Int, y: Int) {
super.init(x: x, y: y)
self.life = 50
}
}
// Towers
class Tower {
let position: Point
var range: Int = 100
var strength: Int = 1
init(x: Int, y: Int) {
self.position = Point(x: x, y: y)
}
func fire(at enemy: Enemy) {
if isInRange(of: enemy) {
enemy.decreaseLife(by: strength)
print("Gotcha")
} else {
print("Darn! Out of range")
}
}
func isInRange(of enemy: Enemy) -> Bool {
let availablePositions = position.points(inRange: range)
for point in availablePositions {
if point.x == enemy.position.x && point.y == enemy.position.y {
return true
}
}
return false
}
}
class LaserTower: Tower {
override init(x: Int, y: Int) {
super.init(x: x, y: y)
self.range = 100
self.strength = 100
}
override func fire(at enemy: Enemy) {
while enemy.life >= 0 {
enemy.decreaseLife(by: strength)
}
print("Enemy destroyed!")
}
}
let tower = Tower(x: 0, y: 0)
let enemy = Enemy(x: 1, y: 1)
let superEnemy = SuperEnemy(x: 1, y: 1)
let laserTower = LaserTower(x: 2, y: 2)
tower.fire(at: enemy)
tower.fire(at: superEnemy)
laserTower.fire(at: enemy)
laserTower.fire(at: superEnemy)
3 Answers
Chris Stromberg
Courses Plus Student 13,389 PointsHere are a few suggestions.
Start a simple app project of your own making. This will force you learn some new code and apply what you are learning.
TeamTreeHouse is good at explaining somethings, but sometimes having something explained from a different perspective can make things easier to understand. Check out these other websites Lynda.com, Raywenderlich.com, Stackoverflow.com. for other perspectives.
When presented with code you are not familiar with, it can help to "Google" the code and see how others are using it in other projects or with code examples.
Make sure your understanding of the basics (Classes, Struts, Properties, Functions, Initializers, Extensions, Inheritance, etc.). is solid! Without this knowledge it can be very difficult to follow or understand what someone has coded.
It can also help to answer questions on this forum from time to time, teaching others is a great way to learn.
Some good books to check out, "Swift programing, The big nerd ranch guide.", Apress "Swift for absolute beginners", "iOS 10 programming fundamentals with Swift: Swift, Xcode, and Cocoa Basics, Matt Neuburg.
Don't give up!
Best of luck!
Jesus Mendoza
23,289 PointsFrom my own experience, I'd say that you get better at reading code by reading code. I started working as a developer almost 1 year ago and at first I always got lost and bored of reading code, but with the time I started understanding the code I was reading and realized it was not that boring after all!
That's why writing clean code is so important!
Michael Williams
Courses Plus Student 8,059 PointsThank you both for your thoughts! Much appreciated.