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
Doctor w-rw-rw-
Jun 24, 2008

Gordon Cole posted:

If you're really worried about how long it takes to type out stringBuilder, take some typing lessons. And if you're working in an environment without autocomplete you're doing something wrong.
In fact, it's pretty much the first thing that Eclipse suggests. "SBui" followed by autocompletes (I map content assist to esc) can product with very minimal typing: StringBuilder stringBuilder = new StringBuilder(); - it's all muscle memory, and it's perfectly descriptive of what it is. Easy as cake and the proper way to go.

It was a revelation for me that eclipse uses capital letters to search through camel cases, as a shortcut. So to autocomplete SomeFooBarThingActivity, anything unique from SF through SFBTA or even SFBarTA or SFBTAct will match in additional to the normal prefix matching. It can really speed things up without loss of meaning in the class name! Not that I'm advocating for rambling class names, of course.

Adbot
ADBOT LOVES YOU

nielsm
Jun 1, 2009



Wheany posted:

"tmp" is pretty much the worst name for a variable ever.

Every time I've seen a variable named tmp or temp, the surrounding code has been poo poo.

code:
tmp = b;
b = a;
a = tmp;
Suggest a better name.

PENETRATION TESTS
Dec 26, 2011

built upon dope and vice
c

Jewel
May 2, 2009

Wheany posted:

"tmp" is pretty much the worst name for a variable ever.

Every time I've seen a variable named tmp or temp, the surrounding code has been poo poo.

In python I tend to use this trick to make 2D arrays a lot (well, until I discovered object.__getitem__(self, key) allows tuples, so it's beneficial to make a 2DArray class that handles them so you can access members using [x, y] instead of [y][x] like a normal 2D array):

Python code:
width = 50
height = 20

DEFAULT_VALUE = 0

game_map = []

for y in range(height):
	tmp = []
	for x in range(width):
		tmp.append(DEFAULT_VALUE)
	game_map.append(tmp)

Goat Bastard
Oct 20, 2004

Wheany posted:

"tmp" is pretty much the worst name for a variable ever.

Every time I've seen a variable named tmp or temp, the surrounding code has been poo poo.

I see you and raise you "test"

Nippashish
Nov 2, 2005

Let me see you dance!

Jewel posted:

In python I tend to use this trick to make 2D arrays a lot (well, until I discovered object.__getitem__(self, key) allows tuples, so it's beneficial to make a 2DArray class that handles them so you can access members using [x, y] instead of [y][x] like a normal 2D array):

row is a better name here and you might as well just use numpy for this since they've done all the nice fancy ndarray things already.

KaneTW
Dec 2, 2011

It's hard to find any code that doesn't use 'tmp' or 'temp'. Just 'find|xargs grep' through my code folder and cURL, SQLite, LAME, Boost, Sourcemod, HL1 SDK, Metamode:Source all use it and with the exception of HL1 SDK I can't say that these projects have poo poo code.

The Gripper
Sep 14, 2004
i am winner
Sometimes you just need a variable to store poo poo while you're building something, what more descriptive name can you use? Is thing_tmp any better? Jewel's example of building a [complex item] to append to an existing list of [complex item]s is the most common, any name you give it is going to be equivalent to tmp or at worst very easily understandable in context.

The Gripper fucked around with this message at 11:45 on Nov 23, 2012

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

nielsm posted:

code:
tmp = b;
b = a;
a = tmp;
Suggest a better name.
this is probably the single 100% acceptable use case for naming something tmp, when you're swapping two things

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

nielsm posted:

code:
tmp = b;
b = a;
a = tmp;
Suggest a better name.

For all the times my customers ask me to implement "swap."

Gordon Cole posted:

sb is not nearly as bad as tmp, but it's still bad. It might not be that confusing in this particular instance, but if you ever have an abbreviated name that ends up getting used outside the immediate context of where it's declared, people aren't going to know what it is. If I just saw sb by itself I would have no idea what it means because it has no inherent meaning. In contrast I know exactly what stringBuilder means. I'll probably know what builder is too, or at the very least I'll know that it builds something.
If your functions are taller than about ten (twenty is pushing it) lines that's the problem.

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker
php:
<?
$_
?>

Zombywuf
Mar 29, 2008

Someone somewhere thought this was a good idea: https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Version+2+Tutorial#JIRARESTAPIVersion2Tutorial-CAPTCHAs

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


Cocoa Crispies posted:

For all the times my customers ask me to implement "swap."

...in a language that doesn't have modern assignment.

code:
a, b = b, a

hobbesmaster
Jan 28, 2008

Cocoa Crispies posted:

For all the times my customers ask me to implement "swap."

Some people do! Lets see what they did:
glibcxx
code:
  template<typename _Tp>
    inline void
    swap(_Tp& __a, _Tp& __b)
    {
      // concept requirements
      __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)

      _Tp __tmp = __a;
      __a = __b;
      __b = __tmp;
    }
huh.

Volte
Oct 4, 2004

woosh woosh

Cocoa Crispies posted:

If your functions are taller than about ten (twenty is pushing it) lines that's the problem.
This is complete cargo cult bullshit.

fritz
Jul 26, 2003

Cocoa Crispies posted:

For all the times my customers ask me to implement "swap."

This seems like a remarkably bizarre objection.

Doctor w-rw-rw-
Jun 24, 2008

Cocoa Crispies posted:

If your functions are taller than about ten (twenty is pushing it) lines that's the problem.
Complexity, readability, and maintainability don't matter people, it's all about line count! :smug:

Nah. I think it's perfectly fine to have long functions so long as the blocks within it are isolated from each other and have clear purpose. To a degree, the longer it gets, the more you're playing with fire and it can go into horror territory easily, but if you have one-time-use blocks or don't want to recalculate things it can be more straightforward than factoring out methods - until the point where the reuse that comes from making it into a method is a net positive (which IMO is a fairly low threshold to meet).

Zombywuf
Mar 29, 2008

Doctor w-rw-rw- posted:

Complexity, readability, and maintainability don't matter people, it's all about line count! :smug:

Nah. I think it's perfectly fine to have long functions so long as the blocks within it are isolated from each other and have clear purpose. To a degree, the longer it gets, the more you're playing with fire and it can go into horror territory easily, but if you have one-time-use blocks or don't want to recalculate things it can be more straightforward than factoring out methods - until the point where the reuse that comes from making it into a method is a net positive (which IMO is a fairly low threshold to meet).

Factoring it out into a method makes you name it so the next guy has a clue what the gently caress you were thinking. Of course, now you have a different problem.

Volte
Oct 4, 2004

woosh woosh
If you take one-time-only code and turn it into a nullary function, all you're doing is making it hard to follow for no reason. Just put a comment for gently caress sakes.

Zombywuf
Mar 29, 2008

Volte posted:

If you take one-time-only code and turn it into a nullary function, all you're doing is making it hard to follow for no reason. Just put a comment for gently caress sakes.

One line comments are the devil and if the function is clearly named I don't need to know its inner workings to follow the code.

Bunny Cuddlin
Dec 12, 2004

Zombywuf posted:

One line comments are the devil and if the function is clearly named I don't need to know its inner workings to follow the code.

lol

The Gripper
Sep 14, 2004
i am winner
Good grief, I've been coding wrong all this time. Do people actually believe that if you have a function do_a_thing that might require some explaining in how it's doing a thing, it should be broken down into multiple small functions so that their names document it? At what point do you stop?

That can't be more readable and maintainable than just commenting parts of interest.

In a perfect world with perfect code maybe that would make sense, but why would you even be looking at the code in that case?

The Gripper fucked around with this message at 19:27 on Nov 23, 2012

Zombywuf
Mar 29, 2008

The Gripper posted:

Good grief, I've been coding wrong all this time. Do people actually believe that if you have a function do_a_thing that might require some explaining in how it's doing a thing, it should be broken down into multiple small functions so that their names document it? At what point do you stop?

At the point where your functions are all less than 10 lines long obviously.

quote:

In a perfect world with perfect code maybe that would make sense, but why would you even be looking at the code in that case?

Perhaps you should be writing more perfect code.

zergstain
Dec 15, 2005

I never believed in these line limit rules. If I make something a function, it's because I expect it to be used in more than one place.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

zergstain posted:

I never believed in these line limit rules. If I make something a function, it's because I expect it to be used in more than one place.

My guideline is that if you describe the method's purpose by saying "it does X and Y then Z", it should be 3 methods: One that does X, one that does Y, and one that does Z. The more lines of code you have, the more things it's probably doing. Following that guideline generally keeps my methods short, but I don't follow a hard-and-fast "if it's more than 10 lines, it should be two methods!" rule.

[edit]
And I'd consider a variable named "tmp" to be highly suspect. We live in 2012 where autocomplete is pretty much a standard IDE tool. I'd rather name a variable "swapLocation" than "tmp". I wouldn't fail it in a code review, but I'd definitely recommend giving it a more descriptive name prior to committing the code.

New Yorp New Yorp fucked around with this message at 19:51 on Nov 23, 2012

Zombywuf
Mar 29, 2008

Ithaqua posted:

My guideline is that if you describe the method's purpose by saying "it does X and Y then Z", it should be 3 methods: One that does X, one that does Y, and one that does Z. The more lines of code you have, the more things it's probably doing.

People look at me weird when I say function names should not include the word "and". Sometimes they have the decency to look embarrassed though.

quote:

Following that guideline generally keeps my methods short, but I don't follow a hard-and-fast "if it's more than 10 lines, it should be two methods!" rule.

No-one does. When I open a file and find it full of functions that don't fit in a single screen I know I'm in for a long day though.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Why are we talking about phrases like "no function must be longer than X lines" as if they are strict rules across the entire spectrum of programming as a whole?

Insisting a strict set of laws about writing code that hold true is as bad as having no guidelines at all.

"tmp" isn't the best name, but it's not the worst name.

Working on complex math problems, I often see it there because you have to give a variable some name, and there's really no purpose for what it is, outside of the fact that it's a common computation that's used in a lot of places. The purpose and meaning is nothing more than "a temporary variable". It's messy code, but complex math embedded in programming is extremely messy anyway.

In a string builder context, constructing some SQL query or something? Probably not the best idea I've had, but that's the least of my worries.

Any variable starting with "my" has much bigger problems. :clint:

Thern
Aug 12, 2006

Say Hello To My Little Friend

nielsm posted:

code:
tmp = b;
b = a;
a = tmp;
Suggest a better name.

code:
a = a xor b;
b = a xor b;
a = a xor b;
Now you guys can spend the next several pages complaining about how some people use overcomplicated algorithms to solve simple problems.

Sedro
Dec 31, 2008
code:
a ^= b ^= a ^= b;

KaneTW
Dec 2, 2011

XOR swap is dangerous as hell.

dizzywhip
Dec 23, 2005

nielsm posted:

code:
tmp = b;
b = a;
a = tmp;
Suggest a better name.

oldB

KaneTW posted:

It's hard to find any code that doesn't use 'tmp' or 'temp'. Just 'find|xargs grep' through my code folder and cURL, SQLite, LAME, Boost, Sourcemod, HL1 SDK, Metamode:Source all use it and with the exception of HL1 SDK I can't say that these projects have poo poo code.

Overall they're probably not poo poo code, but any big project like those is going to have some bad parts. Just because other people use tmp doesn't mean it's good code.

The Gripper posted:

Sometimes you just need a variable to store poo poo while you're building something, what more descriptive name can you use? Is thing_tmp any better? Jewel's example of building a [complex item] to append to an existing list of [complex item]s is the most common, any name you give it is going to be equivalent to tmp or at worst very easily understandable in context.

There is always a better name than tmp, 100% of the time. As someone pointed out, row would be good, or even better, something like tiles (assuming this is a tilemap for a game), because that describes the data that is actually in the list.

Zombywuf posted:

One line comments are the devil

Uhhhhhhhhhh

Suspicious Dish posted:

"tmp" isn't the best name, but it's not the worst name.

Working on complex math problems, I often see it there because you have to give a variable some name, and there's really no purpose for what it is, outside of the fact that it's a common computation that's used in a lot of places. The purpose and meaning is nothing more than "a temporary variable". It's messy code, but complex math embedded in programming is extremely messy anyway.

In complex math calculations that use a lot of abstract symbols, naming gets more difficult but you can still do better than tmp. Instead of c = a + tmp you can have c = a + squareRootOfB, which you can actually understand without needing to look around to find out how tmp is calculated.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

nielsm posted:

code:
tmp = b;
b = a;
a = tmp;
Suggest a better name.
d

KaneTW posted:

XOR swap is dangerous as hell.
Which makes it a good fit for this thread! Do new coders still ever use it? I've never seen it anywhere.

MrMoo
Sep 14, 2000

I tried it in some Galois math heavy code, unsurprisingly on modern hardware it is significantly slower than using an intermediate.

Zombywuf
Mar 29, 2008

Gordon Cole posted:

In complex math calculations that use a lot of abstract symbols, naming gets more difficult but you can still do better than tmp. Instead of c = a + tmp you can have c = a + squareRootOfB, which you can actually understand without needing to look around to find out how tmp is calculated.

Camel case is also the Devil.

Zombywuf
Mar 29, 2008

MrMoo posted:

I tried it in some Galois math heavy code, unsurprisingly on modern hardware it is significantly slower than using an intermediate.

A good compiler will optimise it out.

Thern
Aug 12, 2006

Say Hello To My Little Friend

Aleksei Vasiliev posted:

d
Which makes it a good fit for this thread! Do new coders still ever use it? I've never seen it anywhere.

I don't think most people know about it. I only knew because I read it on a gamedev article years ago. It seems like one of those things that solves a problem you never had.

raminasi
Jan 25, 2005

a last drink with no ice

Thern posted:

I don't think most people know about it. I only knew because I read it on a gamedev article years ago. It seems like one of those things that solves a problem you never had.

"There's no modern language that compiles to native code and isn't C++" is definitely a problem I've had before.

hobbesmaster
Jan 28, 2008

Thern posted:

I don't think most people know about it. I only knew because I read it on a gamedev article years ago. It seems like one of those things that solves a problem you never had.

Was that ever a good idea? Its not like it actually saves more than one instruction, unlike some other weird looking tricks like inverse square root.

Just to emphasize, heres a great example of some helpful one line comments:
code:
float Q_rsqrt( float number )
{
        long i;
        float x2, y;
        const float threehalfs = 1.5F;
 
        x2 = number * 0.5F;
        y  = number;
        i  = * ( long * ) &y;                       // evil floating point bit level hacking
        i  = 0x5f3759df - ( i >> 1 );               // what the gently caress?
        y  = * ( float * ) &i;
        y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//      y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
 
        return y;
}

hobbesmaster fucked around with this message at 22:29 on Nov 23, 2012

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

hobbesmaster posted:

Was that ever a good idea? Its not like it actually saves more than one instruction, unlike some other weird looking tricks like inverse square root.

Just to emphasize, heres a great example of some helpful one line comments:
code:
float Q_rsqrt( float number )
{
        long i;
        float x2, y;
        const float threehalfs = 1.5F;
 
        x2 = number * 0.5F;
        y  = number;
        i  = * ( long * ) &y;                       // evil floating point bit level hacking
        i  = 0x5f3759df - ( i >> 1 );               // what the gently caress?
        y  = * ( float * ) &i;
        y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//      y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
 
        return y;
}

That seems like a great example of code that can easily be refactored to be a lot more descriptive about what the gently caress it's actually doing.

Adbot
ADBOT LOVES YOU

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

GrumpyDoctor posted:

"There's no modern language that compiles to native code and isn't C++" is definitely a problem I've had before.

I have high hopes for Rust

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