Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Kotlin approach to perform an operation after any of condition blocks are executed

Getting to know Kotlin!

var flag = false;
if(condition) {
    //do one thing
    flag = true;
}
if(condition2) {
    //do another thing
    flag = true;
}
if(flag)
    save();

What is the right way to do this in Kotlin avoiding the flag approach?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I don't see any clever way to do it concisely in Kotlin, but in most cases I would probably prefer assigning conditions to variables over the flag approach:

val isBlue = ... some condition ...
val hasWings = ... another condition ...

if (isBlue) {
   ...
}
if (hasWings) {
   ...
}
if (isBlue || hasWings) {
   ...
}

Of course, it is a matter of personal taste.

Also, in some cases flag makes more sense. There is save() in your example, so if this flag is something like shouldSaveData and it depends on several conditions where for example the data is mutated, then flag approach makes perfect sense.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!