Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Sep 15 '11 at 5:06

This question came from our site for professional and enthusiast programmers.

3  
You have two completely unrelated topics here. Please split them into separate SO questions! –  Oli Charlesworth Sep 11 '11 at 19:58
1  
"It" says explicit is better? What exactly is "it"? –  Jon Skeet Sep 11 '11 at 19:58
1  
I bet the author meant something along the lines of list comprehensions when he/she used the term "implicit looping". –  Michael Aaron Safyan Sep 11 '11 at 20:03
1  
I think you are asking some interesting things here; if you split them up and make them more specific (more of a "real question") and also clarify that you're referring to python.org/dev/peps/pep-0020 I think you'll get a more positive response. –  Owen Sep 11 '11 at 20:04
1  
@Jon Skeet. I assume he's referring to 'The Zen of Python' –  HodofHod Sep 11 '11 at 20:07
show 2 more comments

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
1  
A performance impact? –  Oli Charlesworth Sep 11 '11 at 20:05
1  
The for each loop can be marginally slower for iterating over an ArrayList than using get() over the range of indexes. Its usually not worth worrying about and I can't think of another example. The best time to use an Iterator explicitly is so you can remove() but this is not for performance reasons. –  Peter Lawrey Sep 11 '11 at 20:12
    
Thanks for the answer. I don't like the code return null or return Noneand I see it often and I don't want to write such code. It happens when I must return something and can't get the object / value. And if explicit is better than implicit, why are there languages where no loops are explicit? –  909 Niklas Sep 11 '11 at 21:01
1  
@Niklas R In Haskell there is no "explicit for" loop in the language -- at all. (And most "looping" is done using higher-order functions.) –  pst Sep 11 '11 at 22:03
    
Now I wonder why it says that explicit is better than implicit. I don't think being explicit about the context is "better" than completely leaving variables to the framework or app server and same with threading - I don't want to be explicit about thread and why should I? An article about J2EE I found even compares implicit and explicit and I think that implicit programming can be preferred "You still have to know about the JNDI Environment Naming Context to do something useful" weblogs.java.net/blog/monsonhaefel/archive/2003/12/… –  909 Niklas Sep 11 '11 at 23:37
add comment

I think the "implicit" looping here isn't very implicit. Compare this with Scala, where you have operations on lists without any hint of looping:

List(1,2,3,4).foreach(println)

List(1,2,3,4).map(x => x*x)

List(1,2,3,4).filter(_ % 3 != 0)

Still, the enhanced for loops (or "for every"... in Python) are better than explicit loops, it's a step in the right direction, abstracting from the low level details. E.g. for most operations it isn't even important that you have a linear data structure, you can implement the three methods above easily for other data structures like trees, and you wouldn't even note the difference.

share|improve this answer
    
Thanks for a very good answer! Looking at a language as APL a loop looks like +\N that iterates over every element in N and that's also more implicit. –  909 Niklas Sep 15 '11 at 9:45
    
Agreed. for and while are loop constructs, and so are explicit. –  Ethan Furman Jan 11 '12 at 20:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.