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
Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
Beginning, end, instead of a break inside a for loop, whatever, just don't jam your returns in the middle of some insane bit of impossible-to-follow logic. How big does a method have to be where you can't find a return at the beginning of it?

Adbot
ADBOT LOVES YOU

Keetron
Sep 26, 2008

Check out my enormous testicles in my TFLC log!

Background: when writing functionality tests I use either java and/or the dsl for the framework in use. A potential client would like me to start with them, they use python instead of java and a framework I am not familiar with. Python is top of my list for technology to add to my backpack.

Question: the framework is fine, I can pick thatvup in a matter of hours. What can I expect on the transition from java to python?

Aside: IntelliJ has a Python plugin, yay!

Virigoth
Apr 28, 2009

Corona rules everything around me
C.R.E.A.M. get the virus
In the ICU y'all......



Keetron posted:


Aside: IntelliJ has a Python plugin, yay!

Pycharm is by the same company that makes IntelliJ and you should use that instead of cobbling a plugin in if you get serious.

Keetron
Sep 26, 2008

Check out my enormous testicles in my TFLC log!

Virigoth posted:

Pycharm is by the same company that makes IntelliJ and you should use that instead of cobbling a plugin in if you get serious.

The plugin is also by jetbrains, the overlap is massive. When we get serious, I will see whatever setup is best.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
Pycharm is an assemblage of plugins and features that are available to the paid version of IntelliJ IDEA.

Keetron
Sep 26, 2008

Check out my enormous testicles in my TFLC log!

That I already have. Cheers! You saved me 90 a year.

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

Blinkz0rz posted:

Pycharm is an assemblage of plugins and features that are available to the paid version of IntelliJ IDEA.
Pycharm does feature a bit of simplification in its menus for its focus on Python, but easy enough to get used to IntelliJ's way of doing things.

Jetbrains released a FAQ on this sort of question: https://confluence.jetbrains.com/display/PYH/PyCharm+vs.+IntelliJ+IDEA+Python+plugin+FAQ

FlapYoJacks
Feb 12, 2009
The amount of times I see:

code:

if (foo > bar){
	return butts;
} else {
	return other_butts;
}

Is going to drive me insane.

WHY IS THERE AN ELSE STATEMENT THERE?

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

ratbert90 posted:

The amount of times I see:

code:

if (foo > bar){
	return butts;
} else {
	return other_butts;
}

Is going to drive me insane.

WHY IS THERE AN ELSE STATEMENT THERE?

I more get annoyed at that because this situation is exactly what the ternary operator is for.

code:
return (foo > bar) ? butts : other_butts;

Gounads
Mar 13, 2013

Where am I?
How did I get here?

Skandranon posted:

I more get annoyed at that because this situation is exactly what the ternary operator is for.

code:
return (foo > bar) ? butts : other_butts;

I see these way too often.

return (foo > bar) ? true : false;
return (foo != null) ? foo : null;

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Gounads posted:

I see these way too often.

return (foo > bar) ? true : false;
return (foo != null) ? foo : null;

That annoys me as well... I suppose it's better then

code:
if (foo > bar){
	return true;
} else {
	return false;
}
but still indicates a complete lack of understanding of what booleans are.

The Fool
Oct 16, 2003


how do you feel about this?

code:
if (foo > bar) {
  return false;
} else {
  return true;
}

KoRMaK
Jul 31, 2012



Early returns make the code hard to read in my opinion. I've had more issues later on when using early returns than when dealing with methods that don't. It kind of leads to building a sloppy context I guess. It's nicer to have 1 linear path through a function to what gets returned

The Fool
Oct 16, 2003


I usually feel like if you have more than one return in a function, you need to split it up into multiple functions.

KoRMaK
Jul 31, 2012



Yea, that too. Theres a couple different icky things that it smells like.

MisterZimbu
Mar 13, 2006
If/elses instead of ternaries really never bothered me that much. The if/else is easier for me to parse at a glance than the ternary, but I use both pretty often.

KoRMaK posted:

Early returns make the code hard to read in my opinion. I've had more issues later on when using early returns than when dealing with methods that don't. It kind of leads to building a sloppy context I guess. It's nicer to have 1 linear path through a function to what gets returned

It really depends...

code:
function bob() {
  if (a) return "a";
  if (b) return "b";
  if (c) return "c";

  // actual stuff that does things
}
is far more maintainable and easy to read to me than

code:
function bob() {
  if (!a) {
    if (!b) {
      if (!c) {
        // stuff
      }
      else return "c"
    }
    else return "b"
  }
  else return "a"
}
but anything with different logic to determine the return values? Yeah, that either needs a single return point or be broken down into better functions.

ChickenWing
Jul 22, 2010

:v:

As with the rest of the world, the answer lies somewhere in between.


Dogmatically yelling "single point of return" over and over forever is The Wrong Answer


Smiling smugly at the above person as you return from four different indentation levels obscured within horrendously complex nesting is also The Wrong Answer.


Personally, I try to stay away from multiple returns except for in stuff like searching a collection (return immediately if you find The Thing) or in the occasional case where I can return early to skip some DB/external calls.

AskYourself
May 23, 2005
Donut is for Homer as Asking yourself is to ...
My personal preference is a single point of return but I also try to minimize the level of indentations.

But honestly I've seen so much poo poo code that early return statement do not bother me that much anymore.

Colonel Taint
Mar 14, 2004


I mean yeah if you're writing something a bit more algorithmic in nature then yeah, definitely I'd try to keep early returns out of the main body of a function.

But sometimes we have to essentially script a bunch of IO calls in C, any of which could possibly return with an error and force us to stop

I mean my coworker checked in this gem of a function on Friday that ends like so:

C code:

								}
							}
						}
					}
				}
			}
		}
	}

	if( status == STATUS_OK )
	{
		// return handle
		*hpComp = pObj;
	}
	else
	{
		if( pObj )
		{
			free(pObj);
		}
	}
	
	return status;

He's actually been requesting that we all make sure all of our code is checked in for review and I'm definitely going to call him out on this when we actually review the checked in code.

Colonel Taint fucked around with this message at 21:05 on Aug 21, 2017

reversefungi
Nov 27, 2003

Master of the high hat!
Would it make sense to return early if you have some guarding clauses at the top of the function? Aka if the wrong type of variable is provided, missing argument, etc?

I generally have never taught too much about early returns, but it seems like that's something to look out for that I'll try to integrate in my code.

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

The Dark Wind posted:

Would it make sense to return early if you have some guarding clauses at the top of the function? Aka if the wrong type of variable is provided, missing argument, etc?

I generally have never taught too much about early returns, but it seems like that's something to look out for that I'll try to integrate in my code.

It all really depends. If it makes it easier to read/think about by having some early returns, use them. If it makes it easier to read/think about having a single return, do that.

Volguus
Mar 3, 2009
Another popular approach in C code is to use goto. It does make the code more readable. Then again there are people who wouldn't use goto if their life depended on it.

The Fool
Oct 16, 2003


Volguus posted:

Then again there are people who wouldn't use goto if their life depended on it.

I am one of those.

FlapYoJacks
Feb 12, 2009

Volguus posted:

Another popular approach in C code is to use goto. It does make the code more readable. Then again there are people who wouldn't use goto if their life depended on it.

99% of the time, gotos are bad.

Che Delilas
Nov 23, 2009
FREE TIBET WEED
My company's main product relies upon thousands of lines of SQL stored procedures with dozens of conditional blocks that all (mostly) terminate with a GOTO FINALIZE_TRANSACTION. Getting in there to modify or add (never remove!) logic always feels a little Lovecraftian.

raminasi
Jan 25, 2005

a last drink with no ice

ratbert90 posted:

99% of the time, gotos are bad.

Faking exception handling in C is most of that 1% though.

JawnV6
Jul 4, 2004

So hot ...

ratbert90 posted:

99% of the time, gotos are bad.

For this exact problem? Like what's the solution: early return, indention hell, goto: cleanup, or....

Does "you need another function" mean you make do_thing a shim around if(check_args()) {do_qualified_thing()} else {return false;}?

Destroyenator
Dec 27, 2004

Don't ask me lady, I live in beer
Single point of return doesn't really help though if you're declaring a local "ret_val" variable at the top and conditionally setting it in a bunch of branches. You still have to trace through the logic to see which assignment happens in each case and you don't even get the assurance that it's the final value and nothing else will mess with it.

geeves
Sep 16, 2004

Destroyenator posted:

Single point of return doesn't really help though if you're declaring a local "ret_val" variable at the top and conditionally setting it in a bunch of branches. You still have to trace through the logic to see which assignment happens in each case and you don't even get the assurance that it's the final value and nothing else will mess with it.

But at least you can click on the variable and it will highlight itself vs hunting in hilariously nested if / else statements to find return "foo".

metztli
Mar 19, 2006
Which lead to the obvious photoshop, making me suspect that their ad agencies or creative types must be aware of what goes on at SA
Use whatever makes the code more readable and maintainable.

If using guard clauses with early returns makes the code more readable and maintainable, do that.

If using single return makes the code more readable and maintainable, do that.

If your coding standards are such that they don't allow for the judicious breaking of the rules in service of readability and maintainability, they are bad standards.

If your code reviews don't catch violations of the standards that are obviously unnecessary, they are bad code reviews.

Personally, I tend to like guard clauses and early returns, but that's mostly because usually it's pretty easy to read and the time spent looking for a more readable single return option isn't going to be so much cleaner that it justifies the extra time. I'm lazy.

Gounads
Mar 13, 2013

Where am I?
How did I get here?

geeves posted:

But at least you can click on the variable and it will highlight itself vs hunting in hilariously nested if / else statements to find return "foo".

So is this whole single return thing just a way of avoiding fixing hilariously nested code? It seems to me like you should just avoid that.

geeves
Sep 16, 2004

Gounads posted:

So is this whole single return thing just a way of avoiding fixing hilariously nested code? It seems to me like you should just avoid that.

I'm more talking about legacy code that can't be refactored (at this time). We have a ton of bullshit code like this unfortunately. Sometimes with equally hosed up names like (actual method name from our codebase):

updateUserAccountTree_DontTouchThisMethodBecauseItJustWorksInTheYearOfOurLord2005

Sure enough, the method hasn't been updated since 2005 and every time something goes through that method that I have to fix I pray it's that method so I can justify re-writing it. It's 500 lines long and at one point has 6 nested if/else statements each returning a slightly different version of one object.

Phobeste
Apr 9, 2006

never, like, count out Touchdown Tom, man
Linux kernel style goto error handling is the best for those sorts of workflows in c where you have multiple function calls which depend on things the previous function call did and if any one of them fails you need to wind up all the stuff you did before it. Hell I'm not even sure how else you'd do it without RAII-style code.

Volguus
Mar 3, 2009

Phobeste posted:

Linux kernel style goto error handling is the best for those sorts of workflows in c where you have multiple function calls which depend on things the previous function call did and if any one of them fails you need to wind up all the stuff you did before it. Hell I'm not even sure how else you'd do it without RAII-style code.

There are ways:

Nested if/else to hell and back
Return checks that will free whatever has been initialized until then (and pray you don't forget something)

Or you can choose to keep your sanity, don't adopt blanket rules (such as never use goto) , and just use the thing and not behave like a child.

Mr Shiny Pants
Nov 12, 2012
"pragmatism before idealism"

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Volguus posted:

There are ways:

Nested if/else to hell and back
Return checks that will free whatever has been initialized until then (and pray you don't forget something)

Or you can choose to keep your sanity, don't adopt blanket rules (such as never use goto) , and just use the thing and not behave like a child.

I think the rule really should be "don't use goto to jump to a label in a function that is before the goto statement." I've only found goto really terrible when someone uses goto instead of for loops or do-while loops.

fantastic in plastic
Jun 15, 2007

The Socialist Workers Party's newspaper proved to be a tough sell to downtown businessmen.
But should we use tabs or spaces?

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

fantastic in plastic posted:

But should we use tabs or spaces?

I use tabs with early returns but spaces with single points of return. Of course, if I'm using vi, that changes it up a bit.

KoRMaK
Jul 31, 2012



spaces_make_more_money.jpg

Adbot
ADBOT LOVES YOU

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

fantastic in plastic posted:

But should we use tabs or spaces?

Tabs to indent length; spaces for alignment.

Except I've never been on a project that consistently got that to work (outside my personal hobby garbage) because there's always someone that doesn't care and/or doesn't have visible white space enabled.

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