|
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?
|
# ? Aug 19, 2017 20:29 |
|
|
# ? May 9, 2024 06:41 |
|
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!
|
# ? Aug 20, 2017 14:22 |
|
Keetron 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.
|
# ? Aug 20, 2017 16:36 |
|
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.
|
# ? Aug 20, 2017 18:07 |
|
Pycharm is an assemblage of plugins and features that are available to the paid version of IntelliJ IDEA.
|
# ? Aug 20, 2017 18:10 |
|
That I already have. Cheers! You saved me 90 a year.
|
# ? Aug 20, 2017 18:23 |
|
Blinkz0rz posted:Pycharm is an assemblage of plugins and features that are available to the paid version of IntelliJ IDEA. Jetbrains released a FAQ on this sort of question: https://confluence.jetbrains.com/display/PYH/PyCharm+vs.+IntelliJ+IDEA+Python+plugin+FAQ
|
# ? Aug 20, 2017 20:25 |
|
The amount of times I see:code:
WHY IS THERE AN ELSE STATEMENT THERE?
|
# ? Aug 21, 2017 16:20 |
|
ratbert90 posted:The amount of times I see: I more get annoyed at that because this situation is exactly what the ternary operator is for. code:
|
# ? Aug 21, 2017 16:25 |
|
Skandranon posted:I more get annoyed at that because this situation is exactly what the ternary operator is for. I see these way too often. return (foo > bar) ? true : false; return (foo != null) ? foo : null;
|
# ? Aug 21, 2017 17:31 |
|
Gounads posted:I see these way too often. That annoys me as well... I suppose it's better then code:
|
# ? Aug 21, 2017 18:02 |
|
how do you feel about this?code:
|
# ? Aug 21, 2017 18:05 |
|
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
|
# ? Aug 21, 2017 18:18 |
|
I usually feel like if you have more than one return in a function, you need to split it up into multiple functions.
|
# ? Aug 21, 2017 18:20 |
|
Yea, that too. Theres a couple different icky things that it smells like.
|
# ? Aug 21, 2017 18:21 |
|
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:
code:
|
# ? Aug 21, 2017 18:32 |
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.
|
|
# ? Aug 21, 2017 18:56 |
|
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.
|
# ? Aug 21, 2017 20:33 |
|
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:
Colonel Taint fucked around with this message at 21:05 on Aug 21, 2017 |
# ? Aug 21, 2017 21:03 |
|
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.
|
# ? Aug 21, 2017 21:05 |
|
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? 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.
|
# ? Aug 21, 2017 21:54 |
|
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.
|
# ? Aug 21, 2017 21:59 |
|
Volguus posted:Then again there are people who wouldn't use goto if their life depended on it. I am one of those.
|
# ? Aug 21, 2017 22:40 |
|
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.
|
# ? Aug 21, 2017 22:42 |
|
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.
|
# ? Aug 21, 2017 22:52 |
|
ratbert90 posted:99% of the time, gotos are bad. Faking exception handling in C is most of that 1% though.
|
# ? Aug 21, 2017 22:56 |
|
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;}?
|
# ? Aug 21, 2017 23:01 |
|
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.
|
# ? Aug 21, 2017 23:07 |
|
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".
|
# ? Aug 21, 2017 23:45 |
|
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.
|
# ? Aug 21, 2017 23:58 |
|
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.
|
# ? Aug 22, 2017 00:57 |
|
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.
|
# ? Aug 22, 2017 02:29 |
|
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.
|
# ? Aug 22, 2017 02:56 |
|
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.
|
# ? Aug 22, 2017 04:11 |
|
"pragmatism before idealism"
|
# ? Aug 22, 2017 06:09 |
|
Volguus posted:There are ways: 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.
|
# ? Aug 22, 2017 13:14 |
|
But should we use tabs or spaces?
|
# ? Aug 22, 2017 15:06 |
|
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.
|
# ? Aug 22, 2017 15:12 |
|
spaces_make_more_money.jpg
|
# ? Aug 22, 2017 15:43 |
|
|
# ? May 9, 2024 06:41 |
|
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.
|
# ? Aug 22, 2017 16:46 |