Live data from Hacker News

What's wrong with this code, really?

cvmountain.com

191–200 of 217 posts

Re: What's wrong with this code, really?

#191

Earlier quoted context omitted.

I blame JavaSchools for this. Computing education should be started in Scheme or Python, without all that accidental complexity-- "public static void main(String[] args)"-- littered around the first program. To absorb all of that requires more than should be expected before people can write a first program. JavaSchools get people used to magic incantations that create a culture where inadequate knowledge is the norm.…

My CS degree program was primarily taught in Java. This was not a problem. Language syntax had little to do with conceptual understanding. I appreciated graduating with a firm grasp of a popular language's syntax and core libraries in additional to an understanding of CS theory. Can you name a school that teaches a student only one CS course expecting them to be a programmer?

> My CS degree program was primarily taught in Java. Same here

> This was not a problem. This was not either, for me. It was for ~80% of the class though.

> Language syntax had little to do with conceptual understanding. A uselessly verbose, initially incantational syntax adds useless friction, which just makes many people minds grind.

My experience in teaching languages is that Java and C# were atrocious grind generators whereas Python, Ruby and C were much smoother. JavaScript sits in the middle.

Re: What's wrong with this code, really?

#192
Consider this:

for ( int i=0 ; i Can this be simplified? If this syntax were used elsewhere in the program, but we didn't want to catch the exception in this particular case, should we copy-paste this, and remove the try block? Might there be a situation where preserving the syntax makes the program clearer?

Re: What's wrong with this code, really?

#193
post #62

Earlier quoted context omitted.

Depends on what you want to accomplish and what your container and iterators support. If your Iterator offers a delete method, I think it's a very elegant and clear way to filter a container. for (Iterator it = container.iterator(); it.hasNext(); ) if (!predicate(it.next())( it.remove(); It's more ugly and error prone if you've got to juggle an index, though.

Even with plain C, you can just iterate backwards: for (i = ctr_size(container); i > 0; i--) if (!predicate(container, i - 1)) ctr_remove(container, i - 1);

  while(container.Size > 0)
    container.Remove(0);
No need for variables.

Re: What's wrong with this code, really?

#195

The article's point about writing code that does what it says it does is fine. But as an interview question, I have trouble imagining that candidates won't figure out that this is a game of "guess the answer I'm looking for" and say that they would rewrite this code. A better question would be, there's tremendous deadline pressure, the company is in imminent danger of losing a giant deal if we don't have working code…

I am not a programmer, so I did not know about the clear() method. I thought it was a rather elegant solution! I guess it's all about perspective.

The problem with this code is that it is quite obfuscated. The "i" variable is essentially always 0, because it is being incremented and decremented over and over. Here is code that does the same thing, but without the useless variable:

   while( this.MyControl.TabPages.Count >0)
   {
      this.MyControl.TabPages.Remove ( this.MyControl.TabPages[0] );
   }

Re: What's wrong with this code, really?

#196
post #193

Earlier quoted context omitted.

Even with plain C, you can just iterate backwards: for (i = ctr_size(container); i > 0; i--) if (!predicate(container, i - 1)) ctr_remove(container, i - 1);

while(container.Size > 0) container.Remove(0); No need for variables.

  container.clear();
No need for looping :)

Re: What's wrong with this code, really?

#197

Earlier quoted context omitted.

I blame JavaSchools for this. Computing education should be started in Scheme or Python, without all that accidental complexity-- "public static void main(String[] args)"-- littered around the first program. To absorb all of that requires more than should be expected before people can write a first program. JavaSchools get people used to magic incantations that create a culture where inadequate knowledge is the norm.…

My CS degree program was primarily taught in Java. This was not a problem. Language syntax had little to do with conceptual understanding. I appreciated graduating with a firm grasp of a popular language's syntax and core libraries in additional to an understanding of CS theory. Can you name a school that teaches a student only one CS course expecting them to be a programmer?

Can you name a school that teaches a student only one CS course expecting them to be a programmer?

This is never explicit, but the reason many schools make Java the default language is because it's more "employable" than Python or Scheme.

If you're a CS major, you've probably been exposed to Lisp or Python in your AI class, C in your OS course, and ML and Prolog in your PL class and the "default language" matters, from a CV perspective, a lot less because people've been exposed to more languages. The default language matters in this way for those who take one or two CS courses and then try out for programming jobs in the future.

Re: What's wrong with this code, really?

#198

Earlier quoted context omitted.

> Perhaps the programmer wanted to run test functions on the objects as he deleted them? Doesn't justify anything that was done. > Or perhaps the data structure is a vector of pointers in C++, in which case using the standard clear method would introduce a massive memory leak. Doesn't justify anything that was done. > There are a lot of reasons why someone doing difficult work with complex objects would use a loop to…

If someone used clear on a vector of pointers AS RECOMMENDED IN THIS ARTICLE they've just introduced a serious, serious problem. readability < does it work

"readability No. In a professional setting, readability ~= does it work.

The code presented works almost by accident. It is clear there is no thought put into it at all. It is confusing, and subsequent modifications could cause errors very easily. It is not acceptable code.

Of course, code needs to work. Of course, if this .Clear method is a memory leak you wouldn't use it. Of course, we need to consider these things. But there is no situation in which this code is acceptable. This code would not even warrant a passing grade in an "Introduction To Programming" class.

Re: What's wrong with this code, really?

#200

Earlier quoted context omitted.

If someone used clear on a vector of pointers AS RECOMMENDED IN THIS ARTICLE they've just introduced a serious, serious problem. readability < does it work

"readability No. In a professional setting, readability ~= does it work. The code presented works almost by accident. It is clear there is no thought put into it at all. It is confusing, and subsequent modifications could cause errors very easily. It is not acceptable code . Of course, code needs to work. Of course, if this .Clear method is a memory leak you wouldn't use it. Of course, we need to consider these thing…

> Of course, if this .Clear method is a memory leak you wouldn't use it.

Yes. Exactly.

Post reply on HN