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
Hughlander
May 11, 2005

loinburger posted:

The "volatile" stuff was a bandage for a database cache that I'm probably going to just wind up getting rid of. It used to consist of about a half dozen of these
code:
private List<Foo> foo;

public void invalidateFoo() {
    foo = null;
}

public List<Foo> getFoo() {
    if(foo == null) {
        foo = // retrieve foo from database
    }
    return foo;
}
which I replaced with
code:
private volatile ImmutableList<Foo> foo;

public void invalidateFoo() {...}

public ImmutableList<Foo> getFoo() {
    ImmutableList<Foo> temp = foo;
    if(temp == null) {
        temp = // retrieve foo from database
        foo = temp;
    }
    return temp;
}
I haven't done any performance testing yet, but I doubt that the cache is doing any good: we're frequently invalidating it, the query isn't very expensive, and I suspect that the database is already caching the query results for us.

Or just use a loadingcache

Adbot
ADBOT LOVES YOU

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that

Blotto Skorzany posted:

GHC did this in the past. It involved a Perl script called the "evil munger" or something like that.

Does GHC still statically link the entire standard library whenever you compile something? I'm pretty sure I remember it doing that.

Athas
Aug 6, 2007

fuck that joker

Pavlov posted:

Does GHC still statically link the entire standard library whenever you compile something? I'm pretty sure I remember it doing that.

By default yes, but you can also dynamically link now. It's probably not a good idea - partly because dynamic linking has the usual problems, partially because it is exacerbated by Haskell programs usually using a large number of small libaries, resulting in a staggering number of dynamic libraries.

And seconding that compiler-generated C - it is very readable. Certainly more readable than the C my own compiler generates.

Knyteguy
Jul 6, 2005

YES to love
NO to shirts


Toilet Rascal
code:
        private Boolean GetBooleanValue(Object field)
        {
            if (field == DBNull.Value || field == null)
            {
                return false;
            }
            else
            {
                if (field.ToString() == "No" || field.ToString().ToLower() == "false")
                {
                    return false;
                }
                else
                {
                    return true;
                }


            }

        }
Edit white space is exactly how it appears.

Knyteguy fucked around with this message at 17:09 on Oct 5, 2015

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

else after return makes me twitch.

senrath
Nov 4, 2009

Look Professor, a destruct switch!


I think my favorite part is that no is true.

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that

Athas posted:

By default yes, but you can also dynamically link now. It's probably not a good idea - partly because dynamic linking has the usual problems, partially because it is exacerbated by Haskell programs usually using a large number of small libaries, resulting in a staggering number of dynamic libraries.

And seconding that compiler-generated C - it is very readable. Certainly more readable than the C my own compiler generates.

I just remember it producing massive Hello World programs last time I checked it out. Made me pretty suspicious of their compilation pipeline.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Subjunctive posted:

else after return makes me twitch.

It's good, in a function like that one, because it makes it visually clear that there's a dichotomy.

However, instead of being structured as "if (a) {x} else {if (b) {y} else {z}}", it should be structured as "if (a) {x} else if (b) {y} else {z}", because the nesting doesn't add anything.

ExcessBLarg!
Sep 1, 2001

Hammerite posted:

because the nesting doesn't add anything.
In this particular case, the ifs don't add anything.

sarehu
Apr 20, 2007

(call/cc call/cc)
The nesting seems reasonable to me. "else if" to me is the sort of thing where, if you dropped the previous if statement, the code ought to be able to work. For example if (foo == "hey") { } else if (foo == "ho") { }. In the horror above, the stuff in the else clause relies on the null checks failing. So you've got to have a "okay, field is not null in this world" situation established for all further branches.

KaneTW
Dec 2, 2011

Pavlov posted:

I just remember it producing massive Hello World programs last time I checked it out. Made me pretty suspicious of their compilation pipeline.

GHC links statically. If you link dynamically (not recommended for various reasons), you get small binaries. If you link C statically you get binaries just as large.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Hammerite posted:

It's good, in a function like that one, because it makes it visually clear that there's a dichotomy.

Except there are more than two paths. Why isn't the rest of the function also in elses?

Athas
Aug 6, 2007

fuck that joker

KaneTW posted:

If you link C statically you get binaries just as large.

This is not true. GHC generates gigantic code because the optimiser relies heavily on aggressive inlining (also why compilation is very slow), and because code for such a huge number of thunks is generated.

TheresaJayne
Jul 1, 2011

Subjunctive posted:

else after return makes me twitch.

Also using == instead of .equals

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

TheresaJayne posted:

Also using == instead of .equals

Based on the casing of ToString and ToLower, it's c#, not java, so == is perfectly fine, and in my opinion even preferable.

TheresaJayne
Jul 1, 2011

dwazegek posted:

Based on the casing of ToString and ToLower, it's c#, not java, so == is perfectly fine, and in my opinion even preferable.

meh! you are right, being a java dev mainly i didnt notice the DBNull..

Multiple returns are the bane of enterprise devs, - not allowed but you want to fail fast

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

ExcessBLarg! posted:

In this particular case, the ifs don't add anything.

I don't follow what you mean, unless you're saying that it should be rewritten like this:

code:
return field != DBNull.Value &&
       field != null &&
       field.ToString() != "No" &&
       field.ToString().ToLower() != "false"
which although it's defensible stylistically I would disagree with because I think the number of &&'d conditions makes it harder to understand at a glance.

Subjunctive posted:

Except there are more than two paths. Why isn't the rest of the function also in elses?

As to there being more than two paths, yes it's more a trichotomy than a dichotomy and the second sentence of my post addresses that. In general, what I'm saying is that it's good to do this:

code:
if (condition 1)
{
    return value;
}
else
{
    do something else, which might or might not be returning;
}
As to whether "the rest of the function [should be] also in elses", I don't understand what you mean; can you elaborate? If the "do something else" bit in my above pseudocode is several lines, then I would tend to agree that the else should be dispensed with. If a large part of the work done by the function is to discriminate on the basis of the if-condition, then I think it's good for the function to visually reflect that. If it's an up-front check for some failure condition before moving on to the real work of the function, then sure, drop the else.

sarehu posted:

The nesting seems reasonable to me. "else if" to me is the sort of thing where, if you dropped the previous if statement, the code ought to be able to work. For example if (foo == "hey") { } else if (foo == "ho") { }. In the horror above, the stuff in the else clause relies on the null checks failing. So you've got to have a "okay, field is not null in this world" situation established for all further branches.

I had not considered this perspective.

Soricidus
Oct 21, 2010
freedom-hating statist shill

Hammerite posted:

I don't follow what you mean, unless you're saying that it should be rewritten like this:

code:
return field != DBNull.Value &&
       field != null &&
       field.ToString() != "No" &&
       field.ToString().ToLower() != "false"

which although it's defensible stylistically
really it isn't













everyone knows the && goes at the start of the next line

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Soricidus posted:

everyone knows the && goes at the start of the next line

Tangentially related, I was unaware until well after I started programming as an occupation that convention, when one is dedicating a whole line to a comment, is to place the comment on the line before the thing one is commenting about, rather than on the line after. Which makes no sense, but what can you do.

Carbon dioxide
Oct 9, 2012

Hammerite posted:

Tangentially related, I was unaware until well after I started programming as an occupation that convention, when one is dedicating a whole line to a comment, is to place the comment on the line before the thing one is commenting about, rather than on the line after. Which makes no sense, but what can you do.

Hm, to me it does make sense. "I'm gonna do this now" *does this* "so I can next do THAT" *does that*.

Xerophyte
Mar 17, 2008

This space intentionally left blank
Yeah, I'd rather have context I need to understand a complex blob of code provided to me before I read said blob than after I've slowly trudged through it. Like a lot of stylistic stuff it doesn't really matter as long as everyone who has to read it is in agreement on the local convention, I suppose.

qntm
Jun 17, 2009

Carbon dioxide posted:

Hm, to me it does make sense. "I'm gonna do this now" *does this* "so I can next do THAT" *does that*.

Much of the time my comments are just giving worked example values to demonstrate what the code does, so putting "// Now `x` is ['a', 'b', ...]" immediately after I just did something odd to `x` makes far more sense.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Carbon dioxide posted:

Hm, to me it does make sense. "I'm gonna do this now" *does this* "so I can next do THAT" *does that*.

It makes no sense because it's inconsistent with what you do when you put a comment on the same line, which is to put the comment after the code it references.

code:
def my_method():
    # I'm about to do a thing, because reasons. You're about to read the code where I do a thing.
    do_a_thing()

def my_other_method():
    do_a_thing() # This does a thing, because reasons. You've just read the code where I did a thing.

Blinkz0rz
May 27, 2001

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

Hammerite posted:

I don't follow what you mean, unless you're saying that it should be rewritten like this:

code:
return field != DBNull.Value &&
       field != null &&
       field.ToString() != "No" &&
       field.ToString().ToLower() != "false"

Just extract it to an extension method called phpIsFalsy() and be done with it.

Volte
Oct 4, 2004

woosh woosh

Hammerite posted:

It makes no sense because it's inconsistent with what you do when you put a comment on the same line, which is to put the comment after the code it references.

code:
def my_method():
    # I'm about to do a thing, because reasons. You're about to read the code where I do a thing.
    do_a_thing()

def my_other_method():
    do_a_thing() # This does a thing, because reasons. You've just read the code where I did a thing.
The second style is more for labelling things or writing quick asides about particular lines and writing any sort of prose there is going to get annoying fast. They are two different styles for two different purposes. Putting a standalone comment after the block to which it refers is a bit like "The preceding program contained scenes of violence and should not have been viewed by children."

KaneTW
Dec 2, 2011

Athas posted:

This is not true. GHC generates gigantic code because the optimiser relies heavily on aggressive inlining (also why compilation is very slow), and because code for such a huge number of thunks is generated.

I'm talking about the Hello World here.

ExcessBLarg!
Sep 1, 2001

Hammerite posted:

It makes no sense because it's inconsistent with what you do when you put a comment on the same line, which is to put the comment after the code it references.
In many languages you can't put a same-line comment before code, only after. So the only way you can precede a line of code with an explanatory comment is to place it, by itself, on the line above.

SupSuper
Apr 8, 2009

At the Heart of the city is an Alien horror, so vile and so powerful that not even death can claim it.

Volte posted:

The second style is more for labelling things or writing quick asides about particular lines and writing any sort of prose there is going to get annoying fast. They are two different styles for two different purposes. Putting a standalone comment after the block to which it refers is a bit like "The preceding program contained scenes of violence and should not have been viewed by children."
Coding horrors: The preceding program contained scenes of violence and should not have been viewed by children

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Volte posted:

The second style is more for labelling things or writing quick asides about particular lines and writing any sort of prose there is going to get annoying fast. They are two different styles for two different purposes.

That's as may be, but it doesn't have any bearing on whether the comment should go before or after the code to which it applies; you haven't offered here a reason for inconsistency on that matter to exist.

quote:

Putting a standalone comment after the block to which it refers is a bit like "The preceding program contained scenes of violence and should not have been viewed by children."

Then putting a same-line comment after the code is also alike to doing that. Same-line comments and dedicated-line comments are not distinct in that regard.

ExcessBLarg! posted:

In many languages you can't put a same-line comment before code, only after.

All the more reason why it would make the most sense for the convention to be that comments go after what they are in reference to.

Hammerite fucked around with this message at 13:36 on Oct 6, 2015

ExcessBLarg!
Sep 1, 2001

Hammerite posted:

All the more reason why it would make the most sense for the convention to be that comments go after what they are in reference to.
Comment placement isn't about adhering to some arbitrary convention (e.g.,"must go after the relevant code") and avoiding inconsistency therein. It's a balance between placing them in the most useful locations to aid reading comprehension while minimizing interruptions of the code.

When comments are important to explaining the purpose or behavior of a piece of code, most people find it better to precede the code with a (whole-line) comment than succeed it. But in instances where the comment isn't really necessary to comprehension, it's just there as a label or reference, placing it at the end of the line keeps it "out of the way". As others have explained, the inconsistency is, therefore, due to different uses/purposes/degrees of importance.

I wouldn't really have a problem with having a whole-line comment succeeding a line/block of code, but if the comment is important to understand the block I'd much rather read the comment first.

ExcessBLarg! fucked around with this message at 14:21 on Oct 6, 2015

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
The difference is that an end-of-line comment is quite clearly associated with a single line of code. A block comment is associated with an entire section of one or more lines. Striving for "consistency" between those two things in place of actual readability is hella foolish.

I mean, (unless you're suggesting something seriously pants-on-head like putting function documentation at the end of the function instead of at the start), you have to "break consistency" at some point. Having block comments for small bits of code be consistent with block comments for slightly bigger bits of code is far more important than having block comments for small bits of code read like end-of-line comments.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Hammerite posted:

It makes no sense because it's inconsistent with what you do when you put a comment on the same line, which is to put the comment after the code it references.

code:
def my_method():
    # I'm about to do a thing, because reasons. You're about to read the code where I do a thing.
    do_a_thing()

def my_other_method():
    do_a_thing() # This does a thing, because reasons. You've just read the code where I did a thing.
A foolish consistency is the hobgoblin of little minds. - Kurt Cobain, probably

linusBorlaug
Aug 1, 2013
Maybe I'm splitting hairs here, but I generally consider same line comments to be "with" the code on that line rather than "after" it.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Thanks for the thoughts on that. I like to understand what's important in presentation of code and how to make it easier to work with. (In case anybody got the wrong idea, I don't write code where comments come after the code... any more.)

To contribute to the thread, I don't remember this coming up recently (trigger warning: PHP)

http://www.i-programmer.info/news/98-languages/6758-the-reason-for-the-weird-php-function-names.html

Rasmus had a "hash function" on function names that was just string length, and came up with idiosyncratic function names in such a way as to produce a good distribution of name lengths. That's why PHP functions aren't named consistently.

Pavlov
Oct 21, 2012

I've long been fascinated with how the alt-right develops elaborate and obscure dog whistles to try to communicate their meaning without having to say it out loud
Stepan Andreyevich Bandera being the most prominent example of that

KaneTW posted:

I'm talking about the Hello World here.

Ran a few tests using
code:
#include <stdio.h>
int main() { printf("Dongs\n"); }
code:
 main = printStrLn "Dongs"
Statically linked gcc gave me a 719KB executable, and GHC gave me a 944KB one. The default GHC executable is still dynamically linked though, it's just that it only dynamically links the C libraries it also uses. Couldn't get GHC to statically link the used C libraries, so no idea how big it would be if it was fully static. So the GHC one is still kind of bloated (depending on how heavily it leans on those C libraries), but not as much as I remember. Could have sworn it was something like a 2MB Hello World file when I checked a few years ago. I guess they've optimized some since then?

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker

Hammerite posted:

Thanks for the thoughts on that. I like to understand what's important in presentation of code and how to make it easier to work with. (In case anybody got the wrong idea, I don't write code where comments come after the code... any more.)

To contribute to the thread, I don't remember this coming up recently (trigger warning: PHP)

http://www.i-programmer.info/news/98-languages/6758-the-reason-for-the-weird-php-function-names.html

Rasmus had a "hash function" on function names that was just string length, and came up with idiosyncratic function names in such a way as to produce a good distribution of name lengths. That's why PHP functions aren't named consistently.

The real horror is php people not having the balls to remove old poo poo like this.

Magissima
Apr 15, 2013

I'd like to introduce you to some of the most special of our rocks and minerals.
Soiled Meat
A while ago I wrote a bash script to print mpd's currently playing song and status. For most songs it works fine, but today I played a song with a * in it and the script printed the contents of my home directory. I don't know if bash is the horror or if I'm the horror for using it even for lovely one-off scripts. Probably both.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Quote more.

Magissima
Apr 15, 2013

I'd like to introduce you to some of the most special of our rocks and minerals.
Soiled Meat
Yep, that's what I did. I guess I was spoiled by languages with the concept of a string :v:.

Adbot
ADBOT LOVES YOU

xzzy
Mar 5, 2009

Shell has always been a minefield of quote and escape errors.

One I came across a ways back was with a find command. In the script this appeared:

find /somedir -name *.tgz

Worked great when there was nothing in /somedir that matched the pattern. As soon as /somedir/file.tgz showed up it would only return that one file. Or if there was two tarballs you'd start getting invalid argument errors.

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