Linked Questions

0
votes
0answers
104 views

If null is bad, what other approaches exist that could be implemented? [duplicate]

Null is considered bad, because of nullcheck. My question is, what other way there is, that would have replaced this null problem? How could that have been avoided?
85
votes
10answers
49k views

If null is bad, why do modern languages implement it? [closed]

I'm sure designers of languages like Java or C# knew issues related to existence of null references (see Are null references really a bad thing?). Also implementing an option type isn't really much ...
40
votes
8answers
9k views

Is a new Boolean field better than a null reference when a value can be meaningfully absent?

For example, suppose I have a class, Member, which has a lastChangePasswordTime: class Member{ . . . constructor(){ this.lastChangePasswordTime=null, } } whose lastChangePasswordTime ...
26
votes
8answers
3k views

If nulls are evil, what should be used when a value can be meaningfully absent?

This is one of the rules that are beaing repeated over and over and that perplex me. Nulls are evil and should be avoided whenever possible. But, but - from my naivety, let me scream - sometimes a ...
28
votes
7answers
15k views

null pointers vs. Null Object Pattern

Attribution: This grew out of a related P.SE question My background is in C / C++, but I have worked a fair amount in Java and am currently coding C#. Because of my C background, checking passed and ...
1
vote
3answers
16k views

Java: difference between String a = null and String a = new String()

It's a pretty naive question to ask but I got this doubt while programming for Android. I use to initialize my new strings like String a=null, so in my code there remains a probability to get ...
1
vote
4answers
5k views

How to ideally calculate profit margin with zero sales

How do you handle the following scenario without null? You have this problem (In Java): BigDecimal sales = ...; BigDecimal cost = ...; BigDecimal profit = sales.subtract(cost); // Profit Margin = ...
4
votes
6answers
2k views

Is it bad practice to resolve null arguments to default static variables?

First, let me show you an example (written in ActionScript 3.0): class GameObject { public static function MakeFromName( pName:String, pAtlas:TextureAtlas ...
6
votes
3answers
5k views

What is a good design for a method that can return several logically different results?

The question title is probably too abstract, so let me provide a particular example of what I have in mind: There is a webservice that encapsulates a process of changing passwords for users of a ...
5
votes
3answers
6k views

Should a Java Boolean be used for ternary (3-state) logic?

I'm trying to make a basic cache of a boolean value, and I did it like such: private Boolean _valueCache = null; private boolean getValue() { try { if (_valueCache == null) { // if cache ...
4
votes
3answers
2k views

A very basic question about whether I should check for null and throw NPE? [duplicate]

Consider the below method- public void operationOnList(List<String> list) { list.add(1); } It is obvious that if list is null this method will throw a NullPointerException. My question is ...
2
votes
6answers
1k views

When do the benefits of nullable value types outweigh the cost of null pointers?

The question is in the title. Here is the context: Some people think that the null pointer is a big mistake. Tony Hoare famously apologized for inventing it. Since version 2.0 C# has had nullable ...
2
votes
3answers
287 views

How to use Option Type Pattern in a language that doesn't support generics?

I've been interested to start using the Null Object / Option Type approach for replacing my old habits of null-checking multiple parts of my code. But, from the many examples I saw out there, it seems ...
0
votes
3answers
108 views

Should I use an Exception in a case like this? [duplicate]

I have a Windows service with a fluent interface like this: aRequest = Repository.getRequest() .createProcess() .validate(); Sometimes getRequest() could ...