Live data from Hacker News

What's wrong with this code, really?

cvmountain.com

141–150 of 217 posts

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

#141
post #118

Earlier quoted context omitted.

for (i = ctr_size(container); i--; ) if (!predicate(container, i)) ctr_remove(container, i);

filter predicate xs :)

The snippet I posted can easily be made into a generic function that takes a collection and a predicate. For example, the Apache Commons library offers such a function for Java, so where available the code comes down to the fairly similar

  CollectionUtils.filter(collection, predicate);
Of course aside from being a bit more verbose, predicate needs to be an object (often a singleton), because you can't pass around functions.

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

#142
post #27

About the McConnell quote: "Inefficient programmers tend to experiment randomly until they find a combination that seems to work." The essence of this quote is being passed around quite often these days. When you first start programming, you generally have no idea what the hell you are doing. You learn all these strange, abstract concepts best, by experimenting. It's easy to dismiss people "jiggling things around unt…

After they're experienced, they can do this:

A novice was trying to fix a broken Lisp machine by turning the power off and on.

Knight, seeing what the student was doing, spoke sternly: “You cannot fix a machine by just power-cycling it with no understanding of what is going wrong.”

Knight turned the machine off and on.

The machine worked.

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

#143

Earlier quoted context omitted.

for (i = ctr_size(container); i--; ) if (!predicate(container, i)) ctr_remove(container, i);

That's arguably better. Nitpick: start at ctr_size(container) - 1. [Feel free to edit your post, and I'll just delete this one.]

Nope. The decrement is occurring in the test, so it occurs after the initialization and before the first iteration.

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

#144
post #118

Earlier quoted context omitted.

for (i = ctr_size(container); i--; ) if (!predicate(container, i)) ctr_remove(container, i);

filter predicate xs :)

It's not as concise as Haskell, but the addition of FP capabilities to C# make it a lot more pleasant as a practical language than it was in earlier days.

var filteredCollection = collection.Where(x => predicate(x));

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

#145

Earlier quoted context omitted.

I've noticed this "magic" thing too, among the most clueless members of our profession. Even if they have no idea how bad they are, which they usually don't.

You see this a lot (even on this forum) with people referring to Rails "magic", when it can't possibly be anything but a bunch of lines of code being executed according to the rules of a certain interpreter.

To paraphrase Agatha Heterodyne, any insufficiently analyzed technology is indistinguishable from magic. I think their terminology is pretty reasonable.

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

#146
post #118

Earlier quoted context omitted.

filter predicate xs :)

It's not as concise as Haskell, but the addition of FP capabilities to C# make it a lot more pleasant as a practical language than it was in earlier days. var filteredCollection = collection.Where(x => predicate(x));

As far as I remember you can write that as

  var filteredCollection = collection.Where(predicate);
I agree that C# is a fundamentally usable language.

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

#147
post #102
post #82

Earlier quoted context omitted.

That's a good point. And if the project consists of mainly bad code, it probably contributes to the project being abandoned, as it gets harder and harder to fix bugs and add new features in a code base like that.

Most projects are abandoned due to political reasons. Very rarely do they get dismissed because someone didn't code to whatever standard of the moment.

Exactly if you spend 2 years and 10 million developing some new HR software that IBM spends 5million / year supporting for the next 18 years then that's a success even if you spent 90% in support in fact the better the project is the more likely for you to spend more money supporting it after the initial release. The only way to reduce that cost is to spend so long designing your software that it’s never actually released.

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

#148
OK, fairly newbie coder here..

What would be wrong with just setting a variable to the value of 'this.MyControl.TabPages.Count' outside of the for loop and refering to this?? ie;

    var x = this.MyControl.TabPages.Count;
    for ( int i=0 ; i 
as a quick fix, or if someone did not know while loops or clear function??

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

#149

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…

The correct answer is "This code looks ugly, and bad".

Agreed. "What do you think of this code?" is not a hard question.

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

#150

Earlier quoted context omitted.

The code was unacceptable from line 1 onwards, and would not pass code review with me on watch. For one, for loops have invariants . Next up, you are altering the loop bound in the loop. I do not care if the code works, the code is hard to reason about when it goes all non-linear like that. Finally, there is a goddamn Clear method. That is the kind of code you see the next morning and delete; hoping none of your peer…

Perhaps the programmer wanted to run test functions on the objects as he deleted them? 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. There are a lot of reasons why someone doing difficult work with complex objects would use a loop to delete them, and using invariants is only possible if you have immutable data structu…

> 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 delete them

Then use a while loop, and make this explicit. While loops have tests, for loops have invariants. There is a reason both exist, and it's not purely historical.

> and using invariants is only possible if you have immutable data structures.

Incorrect.

Post reply on HN