1

I have array, which I want to validate, 2nd item of that array. There are 2 ways which comes to my mind

  1. Check for array length

    if len(array) > 1:
        # Process for array[1]
    
  2. Catch IndexError and process in else block.

    try:
        array[1]
    except IndexError:
        pass
    else:
        # Process for array[1]
    

Which one is better?

If you have any other option, I am ready to learn :)

martineau
  • 119,623
  • 25
  • 170
  • 301
Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • A big thing about decisions like this in python is readability. The first option looks more readable to me. – Jeff Mandell Aug 03 '16 at 15:50
  • 1
    This is a bad question for Stack Overflow, but you have enough rep to join us in the [chat room](http://chat.stackoverflow.com/rooms/6/python), where we'll be glad to discuss this – Wayne Werner Aug 03 '16 at 15:53
  • I ask same question to `codereview` they ask me to move this to `stackoverflow`, now `stackoverflow` ask me to go to `chatroom`, `charroom` rules says "Do not link your recent (< 1-2 days) questions in the room. The main site is the dedicated space for posting questions, and having them answered.", where should I ask this ? – Nilesh Aug 03 '16 at 15:56
  • Very similar to question [Determine whether a key is present in a dictionary](http://stackoverflow.com/questions/3733992/determine-whether-a-key-is-present-in-a-dictionary). – martineau Aug 03 '16 at 16:16

2 Answers2

5

Python encourages EAFP coding style:

EAFP
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

This means the try / except is perfectly fine, but you do not have to use the else clause, simply do:

try:
    # Process for array[1]
except IndexError:
    pass
Delgan
  • 18,571
  • 11
  • 90
  • 141
  • Processing in the try imply that he will not have to catch another IndexError during the processing or he will have to do a try specifically for it and if he doesn't do it, could lead to strange bugs because the catch was not done on the right IndexError. Especially when "Process" get bigger and bigger. Making the way he wrote it 'better' but heavy if he need to ask a lot of forgiveness that way... – Cabu Aug 03 '16 at 16:02
  • Is there any draw back for using `try / catch / else` block ? – Nilesh Aug 03 '16 at 16:07
  • @Cabu your suggession is to use `if` condtion instead of `IndexError`? and what is the meaning of `catch was not done on the right IndexError`? – Nilesh Aug 03 '16 at 16:08
  • There is technically no drawback, but using `try / catch / else` instead of a simple `try / catch` is an improper use of theses statements, which leads to less readable and less maintainable code. – Delgan Aug 03 '16 at 16:10
  • @lafada I mean that if in "process" there is "another_array[out_of_range_index]" the except will catch it could be an error and should not catch it. – Cabu Aug 03 '16 at 16:15
1

If your array should have at least 2 items, I would do an assert:

assert len(array) > 1, "Array should have at least 2 items"

If your array could have 2 items I would use the first form:

if len(array) > 1:
    # Process for array[1]

For me the try form can be less readable in the long run especially if you need to catch exceptions in the "Process for array[1]" part...

Cabu
  • 514
  • 2
  • 5
  • 15