59038

Alternatives to long if statement

Question:

I currently have a long if statement, which looks ugly, and I'm fairly certain isn't proper. It looks something like this.

if self.folderactive.isChecked: folders.createDir('Desktop') elif not self.folderactive.isChecked: folders.deleteDir('Desktop') if self.folderactive_2.isChecked: folders.createDir('Documents') elif not self.folderactive_2.isChecked: folders.deleteDir('Documents') if self.folderactive_3.isChecked: folders.createDir('Downloads') elif not self.folderactive_3.isChecked: folders.deleteDir('Downloads') if self.folderactive_4.isChecked: folders.createDir('Music') elif not self.folderactive_4.isChecked: folders.deleteDir('Music') if self.folderactive_5.isChecked: folders.createDir('Pictures') elif not self.folderactive_5.isChecked: folders.deleteDir('Pictures') if self.folderactive_6.isChecked: folders.createDir('Public') elif not self.folderactive_6.isChecked: folders.deleteDir('Public') if self.folderactive_7.isChecked: folders.createDir('Templates') elif not self.folderactive_7.isChecked: folders.deleteDir('Templates') if self.folderactive_8.isChecked: folders.createDir('Videos') elif not self.folderactive_8.isChecked: folders.deleteDir('Videos')

How would I go about simplifying this long list of conditionals?

Answer1:

First, all elif ... clause can be written as else:.

Multiple if .. else .. can be expressed as for loop:

checkbox_directory_pairs = [ (self.folderactive, 'Desktop'), (self.folderactive_2, 'Documents'), (self.folderactive_3, 'Downloads'), ... ] for checkbox, directory in checkbox_directory_pairs: if checkbox.isChecked: folders.createDir(directory) else: folders.deleteDir(directory)

Answer2:

First of all, you can simplify a statement like this:

if self.folderactive.isChecked: folders.createDir('Desktop') elif not self.folderactive.isChecked: folders.deleteDir('Desktop')

to

if self.folderactive.isChecked: folders.createDir('Desktop') else: folders.deleteDir('Desktop')

Answer3:

You should use associative arrays: checkbox number as index, folder name as value.

Recommend

  • Mongo Query a nested field within an array.
  • Modify contents of Welcome widget in Sonar
  • Asp.Net is looking for the web.config in the wrong place
  • Building assimp 3.2 does not work anymore
  • How to retrieve the Id of a single Firestore document?
  • visual studio fatal error LNK1120:
  • IBM mobile app builder does not see documents in Cloudant database, why?
  • What are aliases in elasticsearch for?
  • 400 error when downloading file from sharepoint rest api
  • Html Anchor text formatting
  • Batch file FOR/f expansion
  • WSO2 SSO always redirects to localhost:9443/samlsso
  • VB.Net using C DLL
  • Gradle failed to build with error Multiple dex files define
  • createFileAtPath & OCUnit & errno blues
  • Powershell - Change windows 7 background to image off a website
  • TextDecorations not being applied to TextBlock during a TreeViewItem Style Trigger
  • Change of MySQL database location not working
  • java.lang.NoClassDefFoundError for org/springframework/aop/framework/AbstractAdvisingBeanPostProcess
  • Opening an image file from java InputStream
  • VBA vlookup with defined range and file from other workbook
  • PDF page count using regex
  • Update recordset without updating database
  • How to personalize a timeline with R?
  • How to use output buffering inside PHPUnit test?
  • Unable to get Emma Coverage for Android [duplicate]
  • How do I manage org and space users in bluemix using cf command line?
  • Installing a python package in a desired folder
  • Which reference source does include Microsoft.VisualBasic?
  • ImportError: cannot import name COMError in python
  • Delphi. Analog of Memo/RichEdit
  • SIP Makefile fail (gnuwin and mingw)
  • Where in the relevant specification is it documented that some comments in a SQL script are, in fact
  • Where to save the local DB created for iphone app?
  • Is it possible to create a self-extracting archive which will run a specific file when unzipped?
  • What is Closure Compiler?
  • Reading XML into Datatable gives incorrect DateTime when the time has Time Zone info
  • 'doc_del_count' bigger than 'doc_count' on CouchDB
  • c# winform DrawToBitmap offscreen
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more