I'm new to programming, Stack Overflow and Code Review. I was encouraged to post this method here based on a question I posted on StackOverflow here. Let me know if you have tips for improving this post, and I will try to implement them asap.
I'm hoping for a general review of my code. I realize the formatting is going to be less than perfect and my code isn't a gleaming example of best programming practices (i.e., write small functions, use MVC, etc.). While I'd certainly appreciate all comments about formatting and readability of my code, I'm much more interested whether there any of my code is likely to cause significant performance issues / crashing / etc.
A few questions:
Considering I'm building a small app to test an idea, I think that writing the shortest, highest performing code should not be my focus. However, the code needs to work, so any major defects causing crashing or a negative user experience are harmful. Is this line of thinking flawed?
Can the 80/20 rule apply to programming? In other words, what are the 20% changes to implement that would make up 80% of the possible performance increase?
If one were to call two methods back to back in
viewWillAppear
, such as…doSomething() eveningReminderTimes()
… I think
eveningReminderTimes()
won't start executing untildoSomething()
has finished, correct?I am emptying arrays via
arrayName = []
, for example. It feels like it might be better for performance to replace the current values when needed. Is there harm in emptying a small 7-index String/Int array like this vs some kind of replacement alternative?I've received a few comments on Stack Overflow suggesting that you I write smaller functions. I've also heard this on a couple programming podcasts, but I haven't heard much, if any, convincing insight as to why. If one has a connecting set of functionality that must always be performed together, what is the benefit of splitting it up?
Please consider question 5 with the assumption that the programmer/developer intends to build one app only and has no plans to build future apps (intended to combat the code reuse for other app argument).
If the connected code that will always be called together is split up, there is more space separating such connected code (detriment for readability, I would think). And it introduces the possibility that the programmer may forget to call one of the connected methods (potential for a bug). If you need to separate the larger method into smaller methods later because it becomes necessary, why not wait until that later time when it becomes necessary? It seems like you may be reducing readability and increase the possibility for human programming error if you proactively split up larger functions of connected code.
Here's the code, which works in a Playground. I really appreciate any help.
import Foundation
import UIKit
class ViewController: UIViewController {
var settingsArray = [String]()
var sleepHoursArray = [Int]()
var sleepMinutesArray = [Int]()
var sleepTimeArray = [String]()
var bedTimeMinutesArray = [Int]()
var bedTimeHoursArray = [Int]()
var bedTimeAMorPMArray = [String]()
var bedTimeArray = [String]()
var morningAlarmHoursArray = [Int]()
var morningAlarmMinutesArray = [Int]()
var morningAlarmAMorPMArray = [String]()
var totalNiteChoreTimeHoursArray = [Int]()
var totalNiteChoreTimeMinutesArray = [Int]()
var eveningReminderTimeMinutesArray = [Int]()
var eveningReminderTimeHoursArray = [Int]()
var eveningReminderTimeAMorPMArray = [String]()
var sundayEveningArray = [Int]()
var mondayEveningArray = [Int]()
var tuesdayEveningArray = [Int]()
var wednesdayEveningArray = [Int]()
var thursdayEveningArray = [Int]()
var fridayEveningArray = [Int]()
var saturdayEveningArray = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
settingsArray = ["7h 10m", "7h 10m", "7h 10m", "7h 10m", "7h 10m", "7h 10m", "7h 10m"]
morningAlarmMinutesArray = [51, 52, 53, 54, 55, 56, 57]
morningAlarmHoursArray = [5, 5, 5, 5, 5, 5, 5]
sundayEveningArray = [1, 10]
mondayEveningArray = [1, 10]
tuesdayEveningArray = [1, 10]
wednesdayEveningArray = [1, 10]
thursdayEveningArray = [1, 10]
fridayEveningArray = [1, 10]
saturdayEveningArray = [1, 10]
eveningReminderTimes()
}
func eveningReminderTimes() {
//Build the sleep arrays
sleepHoursArray = []
sleepMinutesArray = []
for i in 0...6 {
let sleepHoursAndMinutes = settingsArray[i]
var separateBySpace = sleepHoursAndMinutes.componentsSeparatedByString(" ")
let hours = separateBySpace[0]
let hours2 = String(hours.characters.dropLast())
let minutes = separateBySpace[1]
let minutes2 = String(minutes.characters.dropLast())
let x = Int(hours2)!
let y = Int(minutes2)!
sleepHoursArray.append(x)
sleepMinutesArray.append(y)
}
//Build the bed time array
var morningAlarmHourLessSleepHour = 0
bedTimeHoursArray = []
for hours in 0...6 {
var morningHours = hours + 1
if morningHours == 7 {
morningHours = 0
}
morningAlarmHourLessSleepHour = Int(morningAlarmHoursArray[morningHours]) - Int(sleepHoursArray[hours])
//TO CODE REVIEW: While writing this question, I realized the "Int(...)" isn't needed above. Could this cause crashing for some users?
bedTimeHoursArray.append(morningAlarmHourLessSleepHour)
}
var morningAlarmMinutesLessSleepMinutes = 0
bedTimeMinutesArray = []
bedTimeAMorPMArray = []
for minutes in 0...6 {
var bedTimeAMorPM = "AM"
//needed since you use the next day's morning alarm to calculate the previous day's bedtime
var morningMinutes = minutes + 1
if morningMinutes == 7 {
morningMinutes = 0
}
morningAlarmMinutesLessSleepMinutes = Int(morningAlarmMinutesArray[morningMinutes]) - Int(sleepMinutesArray[minutes])
//TO CODE REVIEW: While writing this question, I realized the "Int(...)" isn't needed above. Could this cause crashing for some users?
if morningAlarmMinutesLessSleepMinutes < 0 {
morningAlarmMinutesLessSleepMinutes = morningAlarmMinutesLessSleepMinutes + 60
bedTimeHoursArray[minutes] = bedTimeHoursArray[minutes] - 1
}
switch bedTimeHoursArray[minutes] {
case 0:
bedTimeHoursArray[minutes] = bedTimeHoursArray[minutes] + 12
break
case -11...(-1):
bedTimeHoursArray[minutes] = bedTimeHoursArray[minutes] + 12
bedTimeAMorPM = "PM"
break
case -24...(-12):
bedTimeHoursArray[minutes] = bedTimeHoursArray[minutes] + 24
bedTimeAMorPM = "AM"
break
default:
break
}
bedTimeMinutesArray.append(morningAlarmMinutesLessSleepMinutes)
bedTimeAMorPMArray.append(bedTimeAMorPM)
}
//Build the Total Evening Chore Hours and Minutes Arrays
totalNiteChoreTimeHoursArray = []
totalNiteChoreTimeMinutesArray = []
buildTotalEveningHoursAndMinuteArrays(sundayEveningArray)
buildTotalEveningHoursAndMinuteArrays(mondayEveningArray)
buildTotalEveningHoursAndMinuteArrays(tuesdayEveningArray)
buildTotalEveningHoursAndMinuteArrays(wednesdayEveningArray)
buildTotalEveningHoursAndMinuteArrays(thursdayEveningArray)
buildTotalEveningHoursAndMinuteArrays(fridayEveningArray)
buildTotalEveningHoursAndMinuteArrays(saturdayEveningArray)
//Build the evening reminder time array
var bedTimeHourLessEveningChoreHour = 0
eveningReminderTimeHoursArray = []
for hours in 0...6 {
bedTimeHourLessEveningChoreHour = Int(bedTimeHoursArray[hours]) - Int(totalNiteChoreTimeHoursArray[hours])
//TO CODE REVIEW: While writing this question, I realized the "Int(...)" isn't needed above. Could this cause crashing for some users?
eveningReminderTimeHoursArray.append(bedTimeHourLessEveningChoreHour)
}
var bedTimeMinutesLessEveningChoreMinutes = 0
eveningReminderTimeMinutesArray = []
eveningReminderTimeAMorPMArray = []
for minutes in 0...6 {
var eveningReminderTimeAMorPM = bedTimeAMorPMArray[minutes]
bedTimeMinutesLessEveningChoreMinutes = Int(bedTimeMinutesArray[minutes]) - Int(totalNiteChoreTimeMinutesArray[minutes])
if bedTimeMinutesLessEveningChoreMinutes < 0 {
bedTimeMinutesLessEveningChoreMinutes = bedTimeMinutesLessEveningChoreMinutes + 60
eveningReminderTimeHoursArray[minutes] = eveningReminderTimeHoursArray[minutes] - 1
}
switch eveningReminderTimeHoursArray[minutes] {
case 1...12:
if bedTimeAMorPMArray[minutes] == "AM" {
if bedTimeHoursArray[minutes] < eveningReminderTimeHoursArray[minutes]{
eveningReminderTimeAMorPM = "PM"
}
if bedTimeHoursArray[minutes] == 12 && eveningReminderTimeHoursArray[minutes] < 12 {
eveningReminderTimeAMorPM = "PM"
}
}
break
case 0:
eveningReminderTimeHoursArray[minutes] = eveningReminderTimeHoursArray[minutes] + 12
break
case -11...(-1):
eveningReminderTimeHoursArray[minutes] = eveningReminderTimeHoursArray[minutes] + 12
if eveningReminderTimeAMorPM == "AM" {
eveningReminderTimeAMorPM = "PM"
} else {
eveningReminderTimeAMorPM = "AM"
}
break
case -24...(-12):
eveningReminderTimeHoursArray[minutes] = eveningReminderTimeHoursArray[minutes] + 24
if eveningReminderTimeAMorPM == "AM" {
eveningReminderTimeAMorPM = "AM"
} else {
eveningReminderTimeAMorPM = "PM"
}
break
default:
break
}
eveningReminderTimeMinutesArray.append(bedTimeMinutesLessEveningChoreMinutes)
eveningReminderTimeAMorPMArray.append(eveningReminderTimeAMorPM)
}
print(eveningReminderTimeHoursArray)
print(eveningReminderTimeMinutesArray)
print(eveningReminderTimeAMorPMArray)
}
func buildTotalEveningHoursAndMinuteArrays(eveningArray: Array<Int>) {
var totalNiteChoreTimeSum = 0
var totalNiteChoreTimeHours = 0
var totalNiteChoreTimeMinutes = 0
for itemTime in 1...eveningArray.count {
var totalNiteChoreTime = 0
let index = itemTime - 1
totalNiteChoreTime = eveningArray[index]
totalNiteChoreTimeSum += totalNiteChoreTime
totalNiteChoreTimeHours = totalNiteChoreTimeSum / 60
totalNiteChoreTimeMinutes = totalNiteChoreTimeSum % 60
}
totalNiteChoreTimeHoursArray.append(totalNiteChoreTimeHours)
totalNiteChoreTimeMinutesArray.append(totalNiteChoreTimeMinutes)
}
}