Kiire ja põhjalik juhend nullist: mis see on ja kuidas peaksite seda kasutama

Mis on selle tähendus null? Kuidas seda nullrakendatakse? Millal peaksite nulloma lähtekoodi kasutama ja millal mitte ?

Sissejuhatus

nullon paljude programmeerimiskeelte põhimõiste. See on kõikjal levinud kõikides nendes keeltes kirjutatud lähtekoodides. Seega on hädavajalik idee täielikult mõista null. Peame mõistma selle semantikat ja rakendamist ning peame teadma, kuidas seda nulloma lähtekoodis kasutada .

Programmeerijate foorumite kommentaarid näitavad mõnikord natuke segadust null. Mõned programmeerijad üritavad isegi täielikult vältida null. Sest nad arvavad, et see on "miljoni dollari viga", mille mõtles välja leiutaja Tony Hoare null.

Siin on lihtne näide: Oletame, et Alice email_addressosutab null. Mida see tähendab? Kas see tähendab, et Alice'il pole e-posti aadressi? Või et tema e-posti aadress pole teada? Või et see on salajane? Või tähendab see lihtsalt seda, et see email_addresson "määratlemata" või "initsialiseerimata"? Vaatame. Pärast selle artikli lugemist peaksid kõik saama sellistele küsimustele kõhklemata vastata.

Märkus: see artikkel on programmeerimiskeeleneutraalne - nii palju kui võimalik. Selgitused on üldised ega ole seotud kindla keelega. Konkreetsete nõuannete saamiseks lugege palun oma programmeerimiskeele kasutusjuhendeid null. See artikkel sisaldab siiski Java-s näidatud lihtsaid lähtekoodinäiteid. Kuid pole raske neid oma lemmikkeelde tõlkida.

Käitusaja juurutamine

Enne selle tähenduse arutamist nullpeame mõistma, kuidas nullkäitamise ajal mälus rakendatakse.

Märkus. Vaatame programmi tüüpilist rakendust null. Tegelik rakendamine antud keskkonnas sõltub programmeerimiskeelest ja sihtkeskkonnast ning võib erineda siin näidatust.

Oletame, et meil on järgmine lähtekoodi juhis:

String name = "Bob";

Siin deklareerime muutuja tüüpi Stringja identifikaatoriga, namemis osutab stringile "Bob".

"Punktide" ütlemine on selles kontekstis oluline, sest eeldame, et töötame viitetüüpidega (ja mitte väärtustüüpidega ). Lisateavet selle kohta hiljem.

Asjade lihtsuse huvides teeme järgmised eeldused:

  • Ülaltoodud käsk täidetakse 16-bitises protsessoris, millel on 16-bitine aadressiruum.
  • Stringid on kodeeritud kui UTF-16. Need on lõpetatud 0-ga (nagu C või C ++).

Järgmisel pildil on väljavõte mälust pärast ülaltoodud juhise täitmist:

Ülaloleval pildil olevad mäluaadressid valitakse meelevaldselt ja need pole meie arutelu seisukohast asjakohased.

Nagu näeme, on string "Bob"salvestatud aadressile B000 ja hõivab 4 mälurakku.

Muutuja nameasub aadressil A0A1. A0A1 sisu on B000, mis on stringi algmälu asukoht "Bob". Sellepärast ütleme: Muutuja nameosutab"Bob" .

Siiamaani on kõik korras.

Oletame, et pärast ülaltoodud juhise täitmist täidate järgmist:

name = null;

Nüüd nameosutab null.

Ja see on uus olek mälus:

Näeme, et stringi puhul, "Bob"mis on endiselt mälus , pole midagi muutunud .

Märkus. Stringi salvestamiseks vajalik mälu "Bob"võidakse hiljem vabastada, kui seal on prügikoguja ja pole muid viiteid "Bob", kuid see pole meie arutelus asjakohane.

Oluline on see, et A0A1 (mis tähistab muutuja väärtust name) sisu on nüüd 0000. Muutuja nameei viita "Bob"enam sellele. Väärtus 0 (kõik bitid nullil) on tüüpiline väärtus, mida tähistatakse mälus null. See tähendab, et väärtusega pole seotudname . Võite mõelda ka kui andmete puudumisest või lihtsalt andmete puudumisest .

Märkus: tegelik mälu väärtus, mida tähistatakse, nullon rakenduspõhine. Näiteks Java Virtual Machine spetsifikatsioon ütleb jaotise 2.4 lõpus . Viite tüübid ja väärtused:”

Java virtuaalse masina spetsifikatsioon ei kohusta konkreetset väärtuse kodeerimist null.

Pidage meeles:

Kui viide viitab null, tähendab see lihtsalt, et onsellega pole seotud mingit väärtust .

Tehniliselt öeldes sisaldab viitele määratud mälupaik väärtust 0 (kõik bitid nullil) või mis tahes muud väärtust, nullmida antud keskkonnas tähistatakse .

Performance

Nagu eelmises lõigus teada saime, on operatsioonid, mis nullon seotud käitamise ajal, äärmiselt kiiresti ja hõlpsasti teostatavad.

Toiminguid on ainult kahte tüüpi:

  • Initsialiseeri või määra viide null(nt name = null): Ainus, mida teha tuleb, on ühe mäluraku sisu muutmine (nt 0-le seadmine).
  • Kontrollige, kas viide viitab null(nt if name == null): Ainus asi, mida tuleb teha, on kontrollida, kas viite mälurakul on väärtus 0.

Pidage meeles:

Operatsioonid nullon ülimalt kiired ja odavad.

Viide vs väärtuse tüübid

Siiani eeldasime töötamist viitetüüpidega . Selle põhjus on lihtne: nullseda pole väärtustüüpide jaoks olemas .

Miks?

Nagu me oleme näinud varem, viide on kursorit oma mälu-aadress, mis salvestab väärtus (nt string, kuupäev, klient, mis iganes). Kui viide osutab null, siis pole sellega seotud mingit väärtust.

Teiselt poolt on väärtus definitsiooni järgi väärtus ise. Sellel pole kursorit. Väärtuse tüüp salvestatakse väärtusena. Seetõttu nullpuudub väärtustüüpide mõiste .

The following picture demonstrates the difference. On the left side you can see again the memory in case of variable name being a reference pointing to "Bob". The right side shows the memory in case of variable name being a value type.

As we can see, in case of a value type, the value itself is directly stored at the address A0A1 which is associated with variable name.

There would be much more to say about reference versus value types, but this is out of the scope of this article. Please note also that some programming languages support only reference types, others support only value types, and some (e.g. C# and Java) support both of them.

Remember:

The concept of null exists only for reference types. It doesn't exist for value types.

Meaning

Suppose we have a type person with a field emailAddress. Suppose also that, for a given person which we will call Alice, emailAddress points to null.

What does this mean? Does it mean that Alice doesn’t have an email address? Not necessarily.

As we have seen already, what we can assert is that no value is associated with emailAddress.

But why is there no value? What is the reason of emailAddress pointing to null? If we don't know the context and history, then we can only speculate. The reason for nullcould be:

Alice doesn’t have an email address. Or…

Alice has an email address, but:

  • it has not yet been entered in the database
  • it is secret (unrevealed for security reasons)
  • there is a bug in a routine that creates a person object without setting field emailAddress
  • and so on.

In practice we often know the application and context. We intuitively associate a precise meaning to null. In a simple and flawless world, null would simply mean that Alice actually doesn't have an email address.

When we write code, the reason why a reference points to null is often irrelevant. We just check for null and take appropriate actions. For example, suppose that we have to write a loop that sends emails for a list of persons. The code (in Java) could look like this:

for ( Person person: persons ) { if ( person.getEmailAddress() != null ) { // code to send email } else { logger.warning("No email address for " + person.getName()); }}

In the above loop we don’t care about the reason for null. We just acknowledge the fact that there is no email address, log a warning, and continue.

Remember:

If a reference points to null then it always means that there isno value associated with it.

In most cases, null has a more specific meaning that depends on the context.

Why is it null?

Sometimes it is important to know why a reference points to null.

Consider the following function signature in a medical application:

List getAllergiesOfPatient ( String patientId )

In this case, returning null (or an empty list) is ambiguous. Does it mean that the patient doesn't have allergies, or does it mean that an allergy test has not yet been performed? These are two semantically very different cases that must be handled differently. Or else the outcome might be life-threatening.

Just suppose that the patient has allergies, but an allergy test has not yet been done and the software tells the doctor that 'there are no allergies'. Hence we need additional information. We need to know why the function returns null.

It would be tempting to say: Well, to differentiate, we return null if an allergy test has not yet been performed, and we return an empty list if there are no allergies.

DON’T DO THIS!

This is bad data design for multiple reasons.

The different semantics for returning null versus returning an empty list would need to be well documented. And as we all know, comments can be wrong (i.e. inconsistent with the code), outdated, or they might even be inaccessible.

There is no protection for misuses in client code that calls the function. For example, the following code is wrong, but it compiles without errors. Moreover, the error is difficult to spot for a human reader. We can’t see the error by just looking at the code without considering the comment of getAllergiesOfPatient:

List allergies = getAllergiesOfPatient ( "123" ); if ( allergies == null ) { System.out.println ( "No allergies" ); // <-- WRONG!} else if ( allergies.isEmpty() ) { System.out.println ( "Test not done yet" ); // <-- WRONG!} else { System.out.println ( "There are allergies" );}

The following code would be wrong too:

List allergies = getAllergiesOfPatient ( "123" );if ( allergies == null || allergies.isEmpty() ) { System.out.println ( "No allergies" ); // <-- WRONG!} else { System.out.println ( "There are allergies" );}

If the null/empty-logic of getAllergiesOfPatient changes in the future, then the comment needs to be updated, as well as all client code. And there is no protection against forgetting any one of these changes.

If, later on, there is another case to be distinguished (e.g. an allergy test is pending — the results are not yet available), or if we want to add specific data for each case, then we are stuck.

So the function needs to return more information than just a list.

There are different ways to do this, depending on the programming language we use. Let’s have a look at a possible solution in Java.

In order to differentiate the cases, we define a parent type AllergyTestResult, as well as three sub-types that represent the three cases (NotDone, Pending, and Done):

interface AllergyTestResult {}
interface NotDoneAllergyTestResult extends AllergyTestResult {}
interface PendingAllergyTestResult extends AllergyTestResult { public Date getDateStarted();}
interface DoneAllergyTestResult extends AllergyTestResult { public Date getDateDone(); public List getAllergies(); // null if no allergies // non-empty if there are // allergies}

As we can see, for each case we can have specific data associated with it.

Instead of simply returning a list, getAllergiesOfPatient now returns an AllergyTestResult object:

AllergyTestResult getAllergiesOfPatient ( String patientId )

Client code is now less error-prone and looks like this:

AllergyTestResult allergyTestResult = getAllergiesOfPatient("123");
if (allergyTestResult instanceof NotDoneAllergyTestResult) { System.out.println ( "Test not done yet" ); } else if (allergyTestResult instanceof PendingAllergyTestResult) { System.out.println ( "Test pending" ); } else if (allergyTestResult instanceof DoneAllergyTestResult) { List list = ((DoneAllergyTestResult) allergyTestResult).getAllergies(); if (list == null) { System.out.println ( "No allergies" ); } else if (list.isEmpty()) { assert false; } else { System.out.println ( "There are allergies" ); }} else { assert false;}

Note: If you think that the above code is quite verbose and a bit hard to write, then you are not alone. Some modern languages allow us to write conceptually similar code much more succinctly. And null-safe languages distinguish between nullable and non-nullable values in a reliable way at compile-time — there is no need to comment the nullability of a reference or to check whether a reference declared to be non-null has accidentally been set to null.

Remember:

If we need to know why there is no value associated with a reference, then additional data must be provided to differentiate the possible cases.

Initialization

Consider the following instructions:

String s1 = "foo";String s2 = null;String s3;

The first instruction declares a String variable s1 and assigns it the value "foo".

The second instruction assigns null to s2.

The more interesting instruction is the last one. No value is explicitly assigned to s3. Hence, it is reasonable to ask: What is the state of s3 after its declaration? What will happen if we write s3 to the OS output device?

It turns out that the state of a variable (or class field) declared without assigning a value depends on the programming language. Moreover, each programming language might have specific rules for different cases. For example, different rules apply for reference types and value types, static and non-static members of a class, global and local variables, and so on.

As far as I know, the following rules are typical variations encountered:

  • It is illegal to declare a variable without also assigning a value
  • There is an arbitrary value stored in s3, depending on the memory content at the time of execution - there is no default value
  • A default value is automatically assigned to s3. In case of a reference type, the default value is null. In case of a value type, the default value depends on the variable’s type. For example 0 for integer numbers, false for a boolean, and so on.
  • the state of s3 is 'undefined'
  • the state of s3 is 'uninitialized', and any attempt to use s3 results in a compile-time error.

The best option is the last one. All other options are error-prone and/or impractical — for reasons we will not discuss here, because this article focuses on null.

As an example, Java applies the last option for local variables. Hence, the following code results in a compile-time error at the second line:

String s3;System.out.println ( s3 );

Compiler output:

error: variable s3 might not have been initialized

Remember:

If a variable is declared, but no explicit value is assigned to it, then it’s state depends on several factors which are different in different programming languages.

In some languages, null is the default value for reference types.

When to Use null (And When Not to Use It)

The basic rule is simple: null should only be allowed when it makes sense for an object reference to have 'no value associated with it'. (Note: an object reference can be a variable, constant, property (class field), input/output argument, and so on.)

For example, suppose type person with fields name and dateOfFirstMarriage:

interface Person { public String getName(); public Date getDateOfFirstMarriage();}

Every person has a name. Hence it doesn’t make sense for field name to have 'no value associated with it'. Field name is non-nullable. It is illegal to assign null to it.

On the other hand, field dateOfFirstMarriage doesn't represent a required value. Not everyone is married. Hence it makes sense for dateOfFirstMarriage to have 'no value associated with it'. Therefore dateOfFirstMarriage is a nullable field. If a person's dateOfFirstMarriage field points to null then it simply means that this person has never been married.

Note: Unfortunately most popular programming languages don’t distinguish between nullable and non-nullable types. There is no way to reliably state that null can never be assigned to a given object reference. In some languages it is possible to use annotations, such as the non-standard annotations @Nullable and @NonNullable in Java. Here is an example:

interface Person { public @Nonnull String getName(); public @Nullable Date getDateOfFirstMarriage();}

However, such annotations are not used by the compiler to ensure null-safety. Still, they are useful for the human reader, and they can be used by IDEs and tools such as static code analyzers.

It is important to note that null should not be used to denote error conditions.

Consider a function that reads configuration data from a file. If the file doesn’t exist or is empty, then a default configuration should be returned. Here is the function’s signature:

public Config readConfigFromFile ( File file )

What should happen in case of a file read error?

Simply return null?

NO!

Each language has it’s own standard way to signal error conditions and provide data about the error, such as a description, type, stack trace, and so on. Many languages (C#, Java, etc.) use an exception mechanism, and exceptions should be used in these languages to signal run-time errors. readConfigFromFile should not return null to denote an error. Instead, the function's signature should be changed in order to make it clear that the function might fail:

public Config readConfigFromFile ( File file ) throws IOException

Remember:

Allow null only if it makes sense for an object reference to have 'no value associated with it'.

Don’t use null to signal error conditions.

Null-safety

Consider the following code:

String name = null;int l = name.length();

Käivitusajal põhjustab ülaltoodud kood kurikuulsa nullkursori tõrke , kuna proovime käivitada viite meetodi, millele viidatakse null. Näiteks C # NullReferenceException-s visatakse a, Java-s a NullPointerException.

Nullkursori viga on vastik.

See on paljude tarkvararakenduste kõige sagedasem viga ja on tarkvaraarenduse ajaloos põhjustanud lugematuid probleeme. Selle leiutaja Tony Hoare nullnimetab seda "miljardi dollari veaks".

Kuid Tony Hoare (Turingi auhinna võitja 1980 ja Quicksorti algoritmi leiutaja) annab oma kõnes ka vihje lahendusele:

Uuemad programmeerimiskeeled ... on kasutusele võtnud deklaratsioonid null-viidete jaoks. See on lahendus, mille lükkasin 1965. aastal tagasi.

Contrary to some common belief, the culprit is not null per se. The problem is the lack of support for null handling in many programming languages. For example, at the time of writing (May 2018), none of the top ten languages in the Tiobe index natively differentiates between nullable and non-nullable types.

Therefore, some new languages provide compile-time null-safety and specific syntax for conveniently handling null in source code. In these languages, the above code would result in a compile-time error. Software quality and reliability increases considerably, because the null pointer error delightfully disappears.

Null-safety is a fascinating topic that deserves its own article.

Remember:

Whenever possible, use a language that supports compile-time null-safety.

Note: Some programming languages (mostly functional programming languages like Haskell) don’t support the concept of null. Instead, they use the Maybe/Optional Patternto represent the ‘absence of a value’. The compiler ensures that the ‘no value’ case is handled explicitly. Hence, null pointer errors cannot occur.

Summary

Here is a summary of key points to remember:

  • If a reference points to null, it always means that there is no value associated with it.
  • In most cases, null has a more specific meaning that depends on the context.
  • If we need to know why there is no value associated with a reference, then additional data must be provided to differentiate the possible cases.
  • Luba nullainult siis, kui on otstarbekas, et objektiviidel pole „sellega mingit väärtust seotud”.
  • Ärge kasutage nullveatingimuste signaalimiseks.
  • Mõiste nulleksisteerib ainult viitetüüpide jaoks. Seda pole väärtustüüpide jaoks olemas.
  • Mõnes keeles nullon viidetüüpide vaikeväärtus.
  • null toimingud on ülimalt kiired ja odavad.
  • Kui vähegi võimalik, kasutage keelt, mis toetab kompileerimise aega-null-ohutust.