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
hey mom its 420
May 12, 2007

dwazegek posted:

code:
public bool HasValidItems(SomeClass[] items)
{
  bool retVal = false;
  foreach(SomeClass item in items)
  {
    retVal = retVal || item.IsValid;
  }
  return retVal;
}
Also what the hell is up with setting a variable to false and then doing var = var || x
Every kid knows that false || x equals to x (in a boolean context)
He could have mine as well written that line as
code:
retVal = ((retVal && false) || (false || ((retVal || item.IsValid) && true))) && item.IsValid;

Adbot
ADBOT LOVES YOU

hey mom its 420
May 12, 2007

Oh yeah, drat, missed that.
That's exactly why you shouldn't try to be clever in your code and just use the most readable and clear code possible (in this case, an if statement)

hey mom its 420
May 12, 2007

Mr. Heavy posted:

Never has a single empty method call made me want to hit something so badly.

code:
$self->maybeTakeUserFromForm();

I see someone has been reading Schrödinger's book on programming.

hey mom its 420
May 12, 2007

I agree with nebby that there's almost always a better name for a variable that is returned by a method or function than result.

Although I disagree that i is a bad name, because it's almost exclusively used for loop counters and has become a sort of a standard for them. When you see some_collection[i] in a loop, you know what that i stands for. That's for languages like C and such, proper higher level languages shouldn't force you to write boilerplate code :v:.

hey mom its 420
May 12, 2007

zootm posted:

Just "apples" would suffice - why bother saying it's a count when it could be nothing else? On the other hand, generally speaking, if "result" is confusing, your method is too complex and you should simplify it.
Well it could be a list of Apple objects, so I'd probably use num_apples or something. I don't know, I guess functions shouldn't be so complicated that you don't know what result gives you but it doesn't hurt to give it a more meaningful name.

hey mom its 420
May 12, 2007

such a nice boy posted:

What's wrong with it? There's no way I'm going to make the mental effort to try to parse out what that regex does.
Hehe, I think that's exactly what's wrong with that code.

hey mom its 420
May 12, 2007

nebby's example looks like it's written in Python and there, the first argument to a method is always the instance itself. So if you have
code:
>>> class Boo:
...   def store_this(self, str):
...     self.my_str = str
>>> b = Boo()
>>> b.store_this('haha')
>>> b.my_str
'haha'
But anyway, the first parameter of instance methods in Python should always be called self. This is a very, very strong convention and Guido would probably have a heart attack if he saw anyone using anything other than self as the first parameter to instance methods.

hey mom its 420
May 12, 2007

Ultimate variable naming scheme
code:
private static int :iamafag: (String :buddy:, int :D) {
            // ... code here
}

hey mom its 420
May 12, 2007

I never found the appeal of those keyboards because I rarely look at my keyboard and when I need to do stuff quickly I rely on vim's command mode or just on keyboard shortcuts in general while in other applications.

hey mom its 420
May 12, 2007

Yeah, that's true. I don't know C# but what's the type of the exception being propagated upwards in that code? Because in Java, for instance, if you just blanket catch exceptions and then just do throw new Exception(), you're losing the info about what kind of exception it was, but if you do something like:
code:
  catch (Exception e)
  {
    transaction.Rollback();
    throw e;
  }
then any exception will be caught but the type of the exception propagated upwards will stay in tact. At least I think so, my Java is pretty rusty.

hey mom its 420
May 12, 2007

edit: doubelpost

Adbot
ADBOT LOVES YOU

hey mom its 420
May 12, 2007

Aha, I see, cool.
Yeah, like I said, I don't really know C#, I just wanted to know if that piece of code kept the information about the exception.

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