Java, Python – Explicit vs Implicit Looping

javapython

Why or when is "explicit" preferred when implicit solutions often are more readable and less code? It says "explicit is better than implicit" when for example an implicit loop is better than an explicit loop isn't it?

for every in all:
  every.updated = True

The above looks implicit to me and better than an explicit counter since a counter is another thing that can go wrong and why not leave to the compiler or other environment instead of requirement an explicit counter variable:

  for(int i=0, i<a.length, i++)
...

And there are languages where all looping is "implicit" – what does it mean and when is it preferred? And not explicitly setting a value to nullor None or likewise seems better to me and I want to rewrite code when I "solve" a requirement by explicitly setting return null or String name = null since I think being explicit about what should be null and None should have a more clear solution such as only use a variable if it's needed and if I write return null somewhere I'm probably doing it wrong. When I was writing Java I found myself writing return null and declaring values as null at some places and I didn't like it and could rewrite it even though the case where I put return null was reached only in case of error.

I asked the guru at work whether we ever should declare a variable to null, isn't defaults much better?

I'd be glad to know your thoughts about this. An example where it is better to be explicit I can think of is declaring a list and explicitly declaring what type is in the list is better than just a list of objects e.g. it's preferred to declare ArrayList<ArrayList> instead of just an ArrayList if we know that it's a list of lists.

Best Answer

In Java the enhanced for loop, for each syntax, has a performance impact. Typically returning a null value is a poor practice at best. You generally want to return an object created using the default constructor instead. This prevents run time exceptions involving null references. There are instances where you may want to return null though, for example when trying to create a file.

Related Topic