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
gonadic io
Feb 16, 2011

>>=

raminasi posted:

see, i like this (but only if you have nested functions)

no he forbids those lol

Adbot
ADBOT LOVES YOU

gonadic io
Feb 16, 2011

>>=
see that style is okay in haskell when you can add a
where removeButts = filterNot isAss . filterNot isButt . filterNot isToushie
to the end of the function

but I really dislike defining nested functions before the main logic of the function which you have to do in most iterative langs

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

gonadic io posted:

ask me about having a coworker that obsessively moves half of the lines of code in any given method into a 1-line 1-use helper function for each one. it's not like they're long or complicated lines but there are so many functions like

code:
private def removeButts(bodyParts: List[BodyPart]): List[BodyPart] =
  bodyParts.filterNot(isButt).filterNot(isAss).filterNot(isTushie)

and it drives me insane

ask me about having a coworker who refactors this:
code:

if (this) {
  doThat();
}
into
code:

if (this) { doThat(); }

he's a massively petulant rear end in a top hat though so it's not unexpected when he submits a pull request that consists of aggressively refactoring only this kind of thing

gonadic io
Feb 16, 2011

>>=

Blinkz0rz posted:

ask me about having a coworker who refactors this:
code:

if (this) {
  doThat();
}
into
code:

if (this) { doThat(); }

he's a massively petulant rear end in a top hat though so it's not unexpected when he submits a pull request that consists of aggressively refactoring only this kind of thing

oh i didn't mean takes perfectly working code and spends company time changing it for no benefit, that's a whole other level of obsessive. mine just always writes it like that and argues in code reviews for it

VikingofRock
Aug 24, 2008




I think

Python code:
def process(foo):
	preprocess(foo)
	do_stuff(foo)
	post_process(foo)
with preprocess(), do_stuff(), and post_process() all defined separately is good if preprocess(), do_stuff(), and post_process() are verbose enough. This is true even if those functions are only used in this one spot. It makes it a little easier to follow when compared with some huge function. It's not great if you have to pass a million parameters to each one, but with luck you can pass those parameters as larger structs instead. So like instead of passing (out_x, out_y, out_z) you pass (out_point).

JawnV6
Jul 4, 2004

So hot ...

VikingofRock posted:

Python code:
def process(foo):
	# Preprocess
        ...
	# Do Stuff
        ...
	# Post Process
        ...
???

Dr Monkeysee
Oct 11, 2002

just a fox like a hundred thousand others
Nap Ghost
comments lie

tef
May 30, 2004

-> some l-system crap ->
also:

code:
def process():
  def step_a():
  def step_b():
  def step_c():

  if step_a():
     step_b()
  else
     step_c()
but uh you could also just have a goddam class and pass the state around via self like a normal person rather than hocking a bunch of args around by hand

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
making a helper class for a complex operation is really a trick that people don't do enough

VikingofRock
Aug 24, 2008





The comments help show what's going on, but with a large enough function it's still hard to follow because there's so much you mentally have to keep track of. Also too much scrolling!

tef posted:

also:

code:
def process():
  def step_a():
  def step_b():
  def step_c():

  if step_a():
     step_b()
  else
     step_c()
but uh you could also just have a goddam class and pass the state around via self like a normal person rather than hocking a bunch of args around by hand

The inline functions are good, but not every language has them and you still gotta scroll past them to get to the main part of the function. Passing the state through self can work, if process() logically lends itself to being a member of Foo or whatever, but that's not always the case.

I'm not arguing that what I posted is always right. Just that it's not that crazy of a thing to do and it can make reading things easier in some circumstances. Just make sure you don't export the helper functions so you don't clutter things up too much.

fritz
Jul 26, 2003

tef posted:

but uh you could also just have a goddam class and pass the state around via self like a normal person rather than hocking a bunch of args around by hand

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Dr Monkeysee posted:

comments lie

so do function names

Sapozhnik
Jan 2, 2005

Nap Ghost
Gnu c lets you define nested functions

Except the emitted code writes and jumps to executable code on the stack, which is the computer security equivalent of trying to board an aircraft with a gallon of nitroglycerin in your carry on

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
i generally prefer the single large function, although i wish more languages supported anonymous blocks with their own scope (or that more people used them). the only problem i really have with the one large function is that many variables have lifetimes exceeding their use. putting them in blocks helps clarify lifetime, and serves as a good way to group sections of code.

tef
May 30, 2004

-> some l-system crap ->
when it comes down to splitting a function out by length, odds are it isn't worth it. doing that trick inevitably ends up with functions with 26 arguments, or methods with 26 inner variables. the hard part is not the lines of code in the function, the hard part is the amount of local state and how it interacts. length of function is a good proxy, but number of local variables or arguments may be more helpful.

like, i enjoy reading code that's narratively structured: the design and layout of features considers someone reading the file from top to bottom


sure, lifting into an object can be worth it if you need to express the logic but not the control flow in the same way: result = AHardThing(setup).step_one(options).step_three(),
but your consumers gain nothing from a bunch of helper functions.

i still use them, but it's often because the amount of error handling or boilerplate is sufficient that i want to hide it away from the top-level control flow/process, on the other hand i've written monoliths of functions, but it turns out bootstrap code is always going to be ugly so make it read from top to bottom.

you aren't always really cleaning things up by using objects or closures, sometimes you're making poo poo more awkward to engineer. breaking things out into functions is good when you can read that function and it has very little shared state, and when you call it, you don't need to refer to the implementation.

and some functions are just going to be long. i've also seen arguments about it being easier to test but i'm not convinced tightly coupling your tests into implementation specifics works out in the long term

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
helper functions in compilers often need to be recursive, and they often need to maintain at least 2-3 pieces of context, and they often accumulate extra parameters and results. extracting out to a class is usually the right thing to do, especially once you find yourself writing mutually-recursive functions

but here i will disobey the iron law of programming punditry and admit that my experience may not be representative

sarehu
Apr 20, 2007

(call/cc call/cc)
Or how about a struct that carries the parameters? No need to get object orientated.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Blinkz0rz posted:

ask me about having a coworker who refactors this:
code:
if (this) {
  doThat();
}
into
code:
if (this) { doThat(); }
he's a massively petulant rear end in a top hat though so it's not unexpected when he submits a pull request that consists of aggressively refactoring only this kind of thing

reformatting code isn't refactoring

Cybernetic Vermin
Apr 18, 2005

also you are both equally bad for caring at all about the difference

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

i think blinkzorz doesnt care so much about the difference but that the other guy cares so much that he submits pull requests changing just that

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

Thermopyle posted:

i think blinkzorz doesnt care so much about the difference but that the other guy cares so much that he submits pull requests changing just that

this

comedyblissoption
Mar 15, 2006

rjmccall posted:

making a helper class for a complex operation is really a trick that people don't do enough
an object is a poor man's closure

a closure is a poor man's object

nested functions w/ closures are generally the right abstraction when you want to modularize heavily related code inside a function. unfortunately most languages provide poor support for this.

unless you have a bunch of necessarily intertwined, mutable local state, a function exceeding 20 statements/lines/whatever is probably (not always) a bad function

it's probably hard to have that much necessary intertwined mutable local state inside a single function unless you're creating a performant data structure or something like that

comedyblissoption fucked around with this message at 23:44 on Jun 30, 2016

sarehu
Apr 20, 2007

(call/cc call/cc)

comedyblissoption posted:

an object is a poor man's closure

Objects are only closures when they have virtual functions.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

tef posted:

and some functions are just going to be long.

loving this.

also if you test the equality of two things that have 10 member variable then yes, the function is going to have a high cyclomatic complexity number. it doesn't make the function complex to understand.

b0lt
Apr 29, 2005

Wheany posted:

loving this.

also if you test the equality of two things that have 10 member variable then yes, the function is going to have a high cyclomatic complexity number. it doesn't make the function complex to understand.

get a better language? ¯\_(ツ)_/¯
code:
struct foo {
  int a; int b; int c; int d; int f; int g; int h; int i;
  auto tie() const {
    return std::tie(a, b, c, d, e, f, g, h, i);
  }
  bool operator==(const foo& rhs) const {
    return tie() == rhs.tie();
  }
}

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

b0lt posted:

get a better language? ¯\_(ツ)_/¯
code:
struct foo {
  int a; int b; int c; int d; int f; int g; int h; int i;
  auto tie() const {
    return std::tie(a, b, c, d, e, f, g, h, i);
  }
  bool operator==(const foo& rhs) const {
    return tie() == rhs.tie();
  }
}

:downsowned:
Java code:
import org.apache.commons.lang.builder.EqualsBuilder;

public boolean equals(Object obj) {
   return EqualsBuilder.reflectionEquals(this, obj);
 }

VikingofRock
Aug 24, 2008




Aren't there a bunch of ifs ands and buts about using std::tie? Like it doesn't work with references or something?

VikingofRock
Aug 24, 2008




Oh wait I think it just doesn't work with r-values. Not sure why you would want to do that anyways though.

Zemyla
Aug 6, 2008

I'll take her off your hands. Pleasure doing business with you!

Wheany posted:

:downsowned:
Java code:
import org.apache.commons.lang.builder.EqualsBuilder;

public boolean equals(Object obj) {
   return EqualsBuilder.reflectionEquals(this, obj);
 }

Get an even better language?

code:
deriving instance Eq Foo

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
i apologize for ever using branches.

Max Facetime
Apr 18, 2009

Python code:

def process(foo):
	# Preprocess
        ...
	# Do Stuff
        ...
	# Post Process
        ...


this too is perfectly acceptable if you have a sufficiently smart ... operator

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
i found http://blog.rust-lang.org/2016/06/30/State-of-Rust-Survey-2016.html and it has this part "Another big piece of the Rust development experience is using the Cargo tool."

so i googled "rust cargo" and found https://crates.io/

"The Rust community’s crate host"
"Instantly publish your crates and install them. Use the API to interact and find out more information about available crates. Become a contributor and enhance the site with your work."

i still have no idea what it is. (i assume it's a package/dependency manager of some sort and crates are the packages/libraries)
i loving hate websites for tools and frameworks that acty coy or try to be funny about what they are (it's like monkey cheese bacon for your narwhal to all the code ninjas out there!!!! LOL)

comedyblissoption
Mar 15, 2006

crates are libraries

cargo is the dependency/package manager for crates. it also provides a framework for building projects and running tests from a CLI.

https://doc.rust-lang.org/book/crates-and-modules.html

you are right that it is dumb they decided to invent the new term crate

i suggest looking past it and forgiving the rust community b/c rust is a very good language

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
they can call them bags if they want to, just tell me what your web page is about.

compare that to https://www.rust-lang.org/

"Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety." with a list of features underneath.

that is close to perfect.

comedyblissoption
Mar 15, 2006

to be fair the rust docs explain what cargo is and how to use it right in their getting started page

https://doc.rust-lang.org/book/getting-started.html#hello-cargo

but yah that website could use an explanation somewhere and how to use cargo youre right

even if it's literally just a link to the above document

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
hey guys containers are hot right now lets get in on that somehow

CPColin
Sep 9, 2003

Big ol' smile.
Nice of them to reuse half of the term "cargo cult," though.

gonadic io
Feb 16, 2011

>>=

MALE SHOEGAZE posted:

hey guys containers are hot right now lets get in on that somehow

what's rust's time to crate?

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison

gonadic io posted:

what's rust's time to crate?

heh

Adbot
ADBOT LOVES YOU

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker

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