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.
 
  • Locked thread
Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

BattleMaster posted:

i've never programmed anything serious in a language with exceptions

if you mean the general case of "have I ever accidentally dereferenced a null pointer" no one's perfect but i don't think it's a tall order to do your best to find out what a function will return

:rolleyes:

Adbot
ADBOT LOVES YOU

BattleMaster
Aug 14, 2000

lol mistakes happen, why even try am i rite

fritz
Jul 26, 2003

in a more ideal world move semantics + return value optimization + references as output parameters should mean a lot fewer cases of returning pointers from functions

Soricidus
Oct 21, 2010
freedom-hating statist shill
if it was easy to tell whether a function will return null or not, the ide would do it for you and warn you if you forgot to check

for some reason, even ides with sophisticated static analysis features and advanced null inference turn out to rely on humans annotating functions to tell whether they can return null or not

CRIP EATIN BREAD
Jun 24, 2002

Hey stop worrying bout my acting bitch, and worry about your WACK ass music. In the mean time... Eat a hot bowl of Dicks! Ice T



Soiled Meat

BattleMaster posted:

well if the function doesn't ever return null then the problem being discussed wouldn't happen so there's no reason to check it in that case

i think it's up to you to know if the function you're calling can return null if it fails and check in that case

maybe you don't know every in and out of the code base but i think it's also up to you to quickly check the docs or even just use intellisense or whatever before you call a function if you're not sure

realtalk what language even has a feature that prevents you from ever having to check return values for null and what is this feature? (edit: the program stopping the second you try to work with a null doesn't count because that's not functionally any different from a segfault in a lower level language)

Kotlin has this. If the type isnt marked as nullable, the compiler prevents null references from being returned. If it IS a nullable type, it wont compile if you try to use it before checking if its null.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Java code:
//Butt.java
import org.jetbrains.annotations.NotNull;

public interface Butt {
    /**
     * @return Turd, which must not be null
     */
    public @NotNull Turd poop();
}
// Turd.java
public interface Turd {
    public int getFirmnessOnBristolStoolScale();
}

//NoncompliantButt.java
public class NoncompliantButt implements Butt {
    @Override
    public Turd poop() {
        return null;
    }
}

// ButtMain.java
public class ButtMain {
    public static void main(String[] args) {
        Butt b = new NoncompliantButt();
        Turd t = b.poop();
        t.getFirmnessOnBristolStoolScale();
    }
}
am i allowed to call getFirmnessOnBristolStoolScale() without explicitly checking for null if both the documentation and the annotation org.jetbrains.annotations.NotNull say that Turd is never null?

i mean i guess i have to after it bites me, but what is the level of due diligence here when implementing a TurdConsumer for the first time?

CRIP EATIN BREAD
Jun 24, 2002

Hey stop worrying bout my acting bitch, and worry about your WACK ass music. In the mean time... Eat a hot bowl of Dicks! Ice T



Soiled Meat

Wheany posted:

Java code:
//Butt.java
import org.jetbrains.annotations.NotNull;

public interface Butt {
    /**
     * @return Turd, which must not be null
     */
    public @NotNull Turd poop();
}
// Turd.java
public interface Turd {
    public int getFirmnessOnBristolStoolScale();
}

//NoncompliantButt.java
public class NoncompliantButt implements Butt {
    @Override
    public Turd poop() {
        return null;
    }
}

// ButtMain.java
public class ButtMain {
    public static void main(String[] args) {
        Butt b = new NoncompliantButt();
        Turd t = b.poop();
        t.getFirmnessOnBristolStoolScale();
    }
}
am i allowed to call getFirmnessOnBristolStoolScale() without explicitly checking for null if both the documentation and the annotation org.jetbrains.annotations.NotNull say that Turd is never null?

i mean i guess i have to after it bites me, but what is the level of due diligence here when implementing a TurdConsumer for the first time?

intellij will yell at you that a implementor of the interface doesnt also have the @NotNull annotation.

the IDE will yell at you. findbugs will also.

the compiler will not.

BattleMaster
Aug 14, 2000

i feel like this is more a philosophical argument over whether you can trust docs. like... are docs really real, maaaan?

HoboMan
Nov 4, 2010

ok i have a bug in my dev build that is not in the test build even if i push it into test and i don't have the slightest loving clue what the gently caress is going on and aggggggggggggg

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

CRIP EATIN BREAD posted:

intellij will yell at you that a implementor of the interface doesnt also have the @NotNull annotation.

the IDE will yell at you. findbugs will also.

the compiler will not.

i know, but i was kind of implying that the non-compliant butt comes from some external source, even though i just implemented and instantiated one myself in the example.

i really didn't feel like creating a di framework just for another yospos turd joke.

CPColin
Sep 9, 2003

Big ol' smile.
^^^ If you defensively check for null anyway, Eclipse will complain about unnecessary code. :shobon:

CRIP EATIN BREAD posted:

intellij will yell at you that a implementor of the interface doesnt also have the @NotNull annotation.

the IDE will yell at you. findbugs will also.

the compiler will not.

I'll never understand why Oracle went, "Yes, these annotations are a good idea. We'll write up a spec for them." then did not add them to the standard library or tools.

CRIP EATIN BREAD
Jun 24, 2002

Hey stop worrying bout my acting bitch, and worry about your WACK ass music. In the mean time... Eat a hot bowl of Dicks! Ice T



Soiled Meat
kotlin sorta owns since you ge tthings like

code:
var a: String = "yospos, bithc"
a = null // wont compile, you're a terrible programmer
but you can do

code:
var a: String? = "yospos, bithc"
a = null // gets here
return a.length // it was nullable, this wont compile!
finally it does some nice inferences

code:
var a: String? = getYosposBithc()
if (a == null) {
  return;
}
// at this point the compiler knows that a can
// never be null because you checked it above
return a.length // this is ok!

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

BattleMaster posted:

i feel like this is more a philosophical argument over whether you can trust docs. like... are docs really real, maaaan?

its really got nothing to do with that. the fundamental issue is that if something can go wrong, given enough time it will. the problem boils down to the fact that you actually want to return one of two values: a valid reference, or a null reference, but the language doesn't encode this information. a similar thing happens in oldschool c functions that return an int but where say "-1" means an error of some sort. they really wanted two types of return values: an integer, or an error code. in both cases the type system doesn't distinguish between the two return types and this can produce difficult to track bugs because these "exceptional" cases don't necessarily come up very often. compare this with languages that have option types and tagged unions where the language forces you to check the result in order to do anything with it.

CRIP EATIN BREAD
Jun 24, 2002

Hey stop worrying bout my acting bitch, and worry about your WACK ass music. In the mean time... Eat a hot bowl of Dicks! Ice T



Soiled Meat
oh yeah and you get cool type cast checks

code:
fun imATerribleProgrammer(a: Any) {
  if (a is String) {
    print(a.length) // no need to cast, it knows its a string because of the check above
  }
  else if (a is Int) {
    print(a + 420) // it knows its an int
  }
}

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

CPColin posted:

^^^ If you defensively check for null anyway, Eclipse will complain about unnecessary code. :shobon:

so does idea


which you can then suppress with
//noinspection ConstantConditions

Progressive JPEG
Feb 19, 2003

CRIP EATIN BREAD posted:

its useful in case you have something like this:

code:
int foo(int x) {
  // do a dumb thing
}

int foo(int *x) {
  // do another dumb thing
}

foo(0); // what gets called?

dont do that

oh no blimp issue
Feb 23, 2011

c#s ? operator is nice

Bloody
Mar 3, 2013

type systems are really good

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Awia posted:

never return anything and just make everything happen through side effects

oh hey i think you work at my company, whats up

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

BattleMaster posted:

i feel like this is more a philosophical argument over whether you can trust docs. like... are docs really real, maaaan?

docs are not real. they aren't required to match the code or even be self-consistent. I'd rather have a test suite

FamDav
Mar 29, 2008

Wheany posted:

are you saying that your code has never had a nullpointerexception?

you treat the solution as fixing whatever emitted the null as opposed to checking for null and letting that infest every bit of your code base.

Jerry Bindle
May 16, 2003

Bloody posted:

type systems are really good

i agree with this poster.

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer
in mumps the empty string "" is null. your basically hosed if you need to return an empty string that doesnt mean null because its all by convention and your caller will almost always just assume "" means something hosed up.

at least 0 as null has historical reasoning behind it. outside of some exceptional architectures the page starting at 0x0 is off-limits typically so if you dereference a pointer with a value of 0 it's likely you've messed up. that this has downstream effects for situations where you have null values that aren't pointers or need to be handled outside the context of dereferencing the pointer is frustrating but at least understandable.

in mumps, "",an uninitialized variable, and global subscript with no value are all equal:
code:

;this tag returns null
returnNull()
     q ""

;this tag creates some data and shows its all treated the same
thisShitsEqual() n var,arr
     s arr("subwithdata")=1
     s ^glo("othersubwithdata")=5
     q $$returnNull()=""&(var="")&(arr=var)&(^glo=arr) ;this line will return 1, which is True in mumps
did you catch that? even that array and global, which have data in them, are null if you dereference a node that exists but only has children and no data of its own. so now you not only have to ""-check things, but also use $d on them to make sure they're not arrays with data in them (because arrays and variables are indistinguishable from each other except by one having descendants).

The MUMPSorceress fucked around with this message at 16:31 on Mar 25, 2016

oh no blimp issue
Feb 23, 2011

fart simpson posted:

oh hey i think you work at my company, whats up

lol if you ever error check or ensure that what you said would happen does happen

Soricidus
Oct 21, 2010
freedom-hating statist shill

hackbunny posted:

docs are not real. they aren't required to match the code or even be self-consistent. I'd rather have a test suite

the kind of code that has bad or no docs is unlikely to have great tests

oh no blimp issue
Feb 23, 2011

i saw
code:
do
{
    doingstuff();
} while( false );
in the code base of one of our products a few days ago
id never seen it before

Bloody
Mar 3, 2013

0x0000 is cpu register r0

or reserved

or the head of data memory

or the head of flash memory

(this is all on one architecture)

Bloody
Mar 3, 2013

Awia posted:

i saw
code:
do
{
    doingstuff();
} while( false );
in the code base of one of our products a few days ago
id never seen it before

what the gently caress

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Bloody posted:

what the gently caress

you can exit the do-block early using break

code:
do {
   if(somethingIsFucked) {
         break;
   } 
   actuallyDoTheThing();

}while(false);

oh no blimp issue
Feb 23, 2011

well, more properly it was:

code:
do
{
    if( !doingstuff() )
    {
        break;
    }
    if( !doingotherstuff() )
    {
        break;
    }
} while( false );

oh no blimp issue
Feb 23, 2011

Wheany posted:

you can exit the do-block early using break

code:
do {
   if(somethingIsFucked) {
         break;
    }

}while(false);

what he said

Bloody
Mar 3, 2013

Wheany posted:

you can exit the do-block early using break

code:
do {
   if(somethingIsFucked) {
         break;
   } 
   actuallyDoTheThing();

}while(false);

if(!somethingIsFucked) actuallyDoTheThing();

CRIP EATIN BREAD
Jun 24, 2002

Hey stop worrying bout my acting bitch, and worry about your WACK ass music. In the mean time... Eat a hot bowl of Dicks! Ice T



Soiled Meat
thats a pretty common pattern when you dont have goto.

then again i really only use goto when writing kernel device drivers.

oh no blimp issue
Feb 23, 2011

Bloody posted:

if(!somethingIsFucked) actuallyDoTheThing();

what if you dont want the thing to happen if it doesn't happen first time?

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

CRIP EATIN BREAD posted:

thats a pretty common pattern when you dont have goto.

then again i really only use goto when writing kernel device drivers.

also if you have goto, but have heard the phrase "Go To Statement Considered Harmful" from the year 1968

Zemyla
Aug 6, 2008

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

Awia posted:

i saw
code:
do
{
    doingstuff();
} while( false );
in the code base of one of our products a few days ago
id never seen it before

It could also be an expanded macro. In C, if a macro has to do more than one thing at once, it generally goes

code:
#define BUTTS(x) do { fart(); fart(); poo poo(x) } while (false)
so that it can have a semicolon after it, whereas if it were defined as

code:
#define BUTTS(x) { fart(); fart(); poo poo(x) }
then a semicolon after it would be a syntax error.

AWWNAW
Dec 30, 2008

people who write X Considered Harmful blog posts should be shot into space

Bloody
Mar 3, 2013

Awia posted:

what if you dont want the thing to happen if it doesn't happen first time?

what?

oh no blimp issue
Feb 23, 2011


what if it doesnt' work and you dont want to try again?

Adbot
ADBOT LOVES YOU

oh no blimp issue
Feb 23, 2011

Wheany posted:

also if you have goto, but have heard the phrase "Go To Statement Considered Harmful" from the year 1968

id probably not use a goto just because at least with break you know it goes to the end of the block instead of somewhere else in the file

ignoring the fact that ctrl+f exists

  • Locked thread