Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
ashgromnies
Jun 19, 2004

very posted:

Don't you know the virtue of avoid magic numbers in your code?

Are you being sarcastic? Please please please be being sarcastic.

const int THREE = 3;

is just as bad as using 3 everywhere.

It should be

const int NUM_CHOICES = 3;

or whatever 3 represents.

Adbot
ADBOT LOVES YOU

ashgromnies
Jun 19, 2004

such a nice boy posted:

What's wrong with that code? Is it just that
code:
if (_a > _b)
would have been better?

The way I see it - what if _b is == 0? Run-time error.

ashgromnies
Jun 19, 2004

DaTroof posted:

Haha. Somehow that reminded me of an ecommerce site I saw at a development firm that eventually went belly up. I think I've been blocking it from my conscious memory for the past few years.

* The lead developer declared that ASP was insecure, so everything had to be written as CGI executables in Visual Basic.

* He also didn't trust IIS, so the web server was O'Reilly WebSite. The 16-bit free version.

* The order database was written in Access. Every order had its own MDB file. When somebody started an order, one of the CGI programs would copy an empty version of the database and populate it with a single order. Every MDB was named for its order number: 1001.mdb, 1002.mdb, etc.

* The customer's credit card information was stored in plain text in the MDB.

* The web server needed to be restarted several times a day because of a bug that was causing HTTP requests to hang. After a few days, I discovered the problem when I checked the server's monitor. One of the CGI programs was popping a message box with debug information. The process would halt while it waited for someone to click the OK button.

* In response to the above problem, the lead developer sincerely suggested that one of us sit next to the server so we could watch for message boxes.

The site finally died after 1500 orders, because each order was in a separate 200k MDB file and the server's drive ran out of space. Less than twenty of those orders were actually completed. Most of them were in-house tests. The vast majority only had one or two items in the order. Hundreds of 200k databases that each contained less than 400 bytes of actual data.

And of course, after we took the machine offline for an emergency tuneup, it quickly became apparent that someone had compromised it weeks ago.

Eight years later, that poor bastard of a client finally has a functional web site, but they still don't take orders online.

Whenever I feel insecure about my programming skills, there's always a story like this to make me think, "At least I'm not completely retarded."

ashgromnies
Jun 19, 2004

rotor posted:

yep. to my way of thinkin', there are times when code duplication is just fine, considering the alternatives.

So basically he wanted subroutines? I'm not an expert bash scripter(I just use it for automating things I do all the time) but from what I understand, couldn't he just write out separate shell files containing the code block he glommed and just call those? Or does using source give the functions access to lexical variables?

That's actually really, really genius in a way.

ashgromnies
Jun 19, 2004

Triple Tech posted:

These things, as Perl programmer, piss me off:
code:
@stack = ();
for ($i = 0; $i < scalar @elements; $i++) {
  $temporary = do_something_to $elements[$i];

  push @stack, $temporary;
}

See, I wrote

code:

map( do_something_to $_, @elements );

and someone bitched at me and called it unreadable and obfuscated and "an improper use of map".

ashgromnies
Jun 19, 2004

Triple Tech posted:

As written, that is an improper use of map, philosophically.
code:
# reassignment
munge_and_set($_) for @elements;

# side-effect free
my @new_elements = map { munge($_) } @elements;

# side-effect free in void context
map { munge($_) } @elements;

# works but is bad form, equivalent to first example
map { munge_and_set($_) } @elements;
Edit: The philosophical part here is... When you use for, you're addressing a copy of each element and doing something with it in an iterative context. When you use map, you're multiplying each element functionally. The return value could be more than one element, the same element, a different element, or an empty list, for each element. And that gets returned. Traditionally inside of a map or a for, you don't want to be reassigning values. If you want, you can assign the list the value of the same list mapped over.
code:
# be reborn, list!
@list = map { transform($_) } @list;

Hmm, maybe I'm misreading this.

What about

code:
this_returns_an_array($_) for @elements;

map { this_returns_an_array($_) } @elements;
Will the first not work?

ashgromnies
Jun 19, 2004

Triple Tech posted:

If this_returns_an_array contains no side effects to the variables submitted, then both of those do nothing. They're void context. In the first one, the fact that it returns something doesn't even matter. You can't capture the output of a loop when it's written like that. In the second scenario, you're just missing the left side of the assignment. my @array = that expression. It's returning an array for each element in the elements list, and then jamming all of that into one long array, and then that array isn't going anywhere. Again, void context.

For loops never get assigned to something. Maps should always be either assigned to something or chained to another process, like a function's input, or the list input to a sort or grep.

Yea, I wrote it wrong :( I meant that they did have side effects on the variable being passed in.

ashgromnies
Jun 19, 2004

heeen posted:

I'd guess the latter involves a division which is a rather lengthy operation, whereas the former is just bit juggling.
Isn't (num & 1)==0 even simpler? I'm not sure the former is doing the right thing at all.

(num & 1)==0 is correct.

What he posted in his example is incorrect.

code:
[b]First method:[/b]

Let n = 0111 (7 in decimal)

0110 &
0111
----
0110

Not equal to 0, therefore 7 is odd

Let n = 1010 (10 in decimal)

1001 &
1010
----
1000

Not equal to 0, therefore 10 is odd... WAITAMINUTE


[b]Second method:[/b]
Let n = 0111 (7 in decimal)

0111 &
0001
----
0001

Not equal to 0 therefore 7 is odd

Let n = 1010 (10 in decimal)

1010 &
0001
----
0000

Equal to 0 therefore 10 is even

ashgromnies
Jun 19, 2004

IcePotato posted:

from the forum bug reporting thread


I was going to give it as pass as just someone who thought they knew more than they did, then I noticed that in this guy's profile it said "code monkey".

oh jeez.

The amount of downtime was pretty ridiculous. We have sites that get 30,000 requests per minute and don't allow that to happen. Then again, this is just a pretty small website ran by a handful of people and not a company.

ashgromnies
Jun 19, 2004
Who would have thought that this code would have resulted in issues?

code:
my $user = getUser();

if ($user) {
  doStuff();
} else {
  $user = getUser();
  return 0 if ( !$user );
}
and most of it is equally unreadable... blargh.

ashgromnies
Jun 19, 2004

MrMoo posted:

It leans more towards retarded than unreadable, quite a few people skip using unless but it's a bit sill considering the post-conditional check.

Well it makes it unreadable because I pared it down a little. There are big ol' chunks of code inside those if-else blocks that you have to read to... then you realize the code in the else block won't do anything because the second line is "return if the condition that got me here is still false".

If not user.. try harder to get the user! :downs:

ashgromnies
Jun 19, 2004

Randomosity posted:

I want to kill the people whose code I now maintain. And wtf is an 'appaplexy'

An application-induced apoplexy.

ashgromnies
Jun 19, 2004

Zaasakokwaan posted:

When I was a Junior Programmer, this was always my biggest pet peeve. The former is so natural I could never understand why it wasn't supported more.

I've been beaten down by code for so long I've almost forgotten how wonderful those early days were.

Wouldn't that kill the English readability of something like:

code:
if ($user->is_valid() && $user->has_enough_money()){
though? You'd need to make it

code:
if ($user->is_valid() == 1 && $user->has_enough_money() == 1){
which I don't like! It kills the readability of boolean expressions, and I'm also not sure of how it would work in dynamic languages where if($var) and if($var == 1) are completely different.

ashgromnies
Jun 19, 2004

KaeseEs posted:

Done making GBS threads on the thread, here's a classic from php land, quoted from elsewhere:

I'm curious, just because I'm not a good C coder - what's the proper way to check for overflow?

ashgromnies
Jun 19, 2004
Just ran across this while adding on some functionality to a project...

code:
my $id        = $req->param($unique_id);
That $id is an identifier given to us by one of our third-party integration partners. We also have different identifiers for users on our side and we map the two to eachother.

So that line grabs the user's third-party ID from the CGI request. Nothing weird, but then 20 lines below that in the same method...

code:
if ($id) {
           my $id   = $user->get_user_id();
           my $user = $self->get_user( $id, 1 );
It reassigns $id to $user->get_user_id() which is OUR identifier for users - and it called it on an instance of $user which it reassigns in the next line with the $id it just retrieved - what the gently caress I'm going to cry.

ashgromnies
Jun 19, 2004

Jethro posted:

I don't know much Perl, but doesn't the my in front of $id and $user hide those variables within the scope of the if statement? So if you were commenting this, it might go something like:
code:
if ($id) { #If the 3rd party id is set
           my $id   = $user->get_user_id(); #get our id for the current user,
                                            #and then hide the 3rd party id so we don't have to worry about it
           my $user = $self->get_user( $id, 1 );#get a copy of the current user
Still disgusting, but perhaps "sensical?"

Yeah, it does redefine $id lexically but it's still ridiculous and unmaintainable because at one point $id refers to the third-party ID then in the same method they change it lexically to point to our ID. And they already have an instance of the user - they call get_user_id() on the currently instantiated user, then grab him again.

ashgromnies
Jun 19, 2004

Roseo posted:

I'm not a great programmer, but at least I know enough to not do this:

code:
$n++;
$odd_test = $n/2;
if (preg_match("/\.5/",$odd_test))
{
	$color = "bgcolor=\"#FFCC99\"";
}
else
{
	$color = "bgcolor=\"#FFFFFF\"";
}

code:
$color = 'bgcolor="#F' . ($n++ % 2 ? "C9" : "") . '"';
:c00l:

Who wants to go golfing? :)

Adbot
ADBOT LOVES YOU

ashgromnies
Jun 19, 2004

da keebsta knicca posted:

Also if any of you have never experienced Coldfusion it is best language if you really enjoy typing.

who the gently caress in the history of computing thought, "ah, yes, xml is a decent markup language for organizing data... let's make code out of it"

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply