Reputation: 268
I got used to using Boolean flags that they let some part of my code to have it only once(but not only at the beginning), then change condition to opposed value and never do it again.
For example:
bool isInited = false
var amount: Int = 0
func thatFunctionRunsManyTimes() {
if !isInited && amount > 3 {
isInited = true
// some additional code
}
// some code
amount += 1
}
My question is: is this a bad solution and code smell? If the answer is yes, could you tell me how to avoid using it?
Upvotes: 1
Views: 618
Reputation: 11
check this solution once. bool isInited = false var amount: Int = 0
func thatFunctionRunsManyTimes() {
// This if line condtion never be going true, bcz isInited set false first and check with amount with 'And' condition.
first need to check isInited false with amount contion.
if isInited && amount > 3 {
isInited = true
// some additional code
}
// some code
amount += 1
}
Upvotes: 0
Reputation: 3040
What is the particular problem, you are trying to solve? If you have code to be executed once and code to be executed repeatedly, why not encapsulate the code in several functions?
func once() {
// Do stuff once
}
func repeatedly() {
// Do stuff repeatedly
}
func do() {
once()
while(condition) {
repeatedly()
}
}
Upvotes: 1