Questions tagged [null]
This tag should be used for questions where you are handling null variables or values.
157
questions
50
votes
5answers
206k views
String.IsNullOrEmpty in JavaScript
I am aware of it being frowned upon to do something like write C# in JavaScript. (see this if you don't know what I'm talking about)
But as a judgement call, I think we could stand to have a ...
32
votes
3answers
10k views
Using default None values in Python when assigning split() to a tuple
Let's say I want to parse audio track information into two variables like this:
'2/15' -> track = 2, num_tracks = 15
...
30
votes
4answers
2k views
Just a lowly counter that turned out to be surprisingly complicated
While writing this review, I saw a need for a Counter object similar to Python's. It seemed like it would be easy to write such a class, but it turned out to be ...
21
votes
2answers
15k views
Filtering out empty Optionals
I want to convert my list of optional strings to a list of strings by getting rid of the empty Optionals.
Is there a shorter version for achieving this than the ...
15
votes
2answers
4k views
Using finally with return statement or not
I was wondering if it is the proper way to always put the return statement of a function with a try-catch clause in the finally ...
14
votes
3answers
60k views
14
votes
6answers
2k views
C# consecutive and redundant null checks
I am working on a C# application and I want to support multiple themes, so I wrote a ThemeManager class that can check, load and apply a specific theme. A theme is ...
14
votes
8answers
21k views
IsNullOrWhiteSpace check for Java
I am needing to write my own IsNullOrWhiteSpace check for Java but am wondering if this is the best method to do so
...
14
votes
3answers
28k views
Constructing a query string using StringBuilder
I speedily wrote a bunch of crappy checks, to check whether strings are empty, in order to build a query string.
...
13
votes
6answers
134k views
Generic Null/Empty check for each property of a class
I have created a method to check for null/empty values of class properties and if any null property is found I'm stopping the checking process and returning the result as true. I've used a solution ...
11
votes
5answers
24k views
Returning null or what?
I have a C# function like below, and in the else block, the function must return something. I don't know whether returning null is a good practice or not.
...
11
votes
4answers
2k views
Writing null-safe code to set form values
I will ask my question using the following example:
HTML
...
11
votes
2answers
101k views
Check if any of class properties is not null/empty/was assigned
I have a class that I use to display on a web page fieldset. However, all of the properties of this class are optionally filled in by the user, which means all of them could be null/default. In this ...
11
votes
2answers
1k views
Nullable<T> Implementation for VB6/VBA
Because I was spoiled with C# and the .NET framework, whenever I have to work with VB6 I feel like something's missing in the language. A little while ago I implemented a ...
10
votes
3answers
3k views
“Suspicious comparison of integer references” while checking for a monotonically increasing sequence
I have the following code which iterates on an Integer[] and makes sure that the the values in the array are in ascending order and that there are no neighbouring ...
10
votes
4answers
22k views
Inserting a DBNull value into a database
I have code which works pretty well. I just need some of your opinions on how to write it better with fewer line of code. I want to use a ternary operator but I couldn't make it work so I did an ...
10
votes
1answer
226 views
Organization data class
I believe I have made the following data class quite decently, and I'd like a thorough review on it.
The code is built on Java 8 and uses the Builder and Serialization Proxy pattern, it is used to ...
10
votes
2answers
752 views
Monad transformers in C# for use in validation
I am working on using monad transformers in C#. I would like to know if the following code I present, shows that I have understood this. I am fairly new to this so any feedback / comments are really ...
9
votes
3answers
3k views
Comparing two objects having two nullable date fields
ExpiryDates has two properties PSL_ExpiryDate and MNL_ExpiryDate of type nullable date. I am ...
8
votes
4answers
23k views
null/undefined checking for checking seats
This looks pretty messy (need to reduce nesting I feel). I need a check an input for a seat, but I can't guarantee it has a value (one may not be chosen for example). If the seats aren't in a certain ...
8
votes
3answers
15k views
Writing a generic performance efficient isEmpty method which can check for null or emptiness
I am writing a utility method which can check for empty and null string, or collection or an object or any general types -
...
8
votes
3answers
601 views
Error solution: Uncaught TypeError
Uncaught TypeError: document.getElementById(...) is null
I have a single JavaScript file that is connected to multiple pages. The below snippet is a function that is used for a single page. The above ...
8
votes
2answers
355 views
Making multiple copies of a pattern
Here's a function for making multiple copies of a block of memory in another block of memory. It handles the case when the size of the target is smaller than or not a perfect multiple of the source ...
8
votes
2answers
5k views
Handling nil: Trying to avoid #try
Yesterday, I introduced a bug into our codebase by calling #titleize on a string that could possibly come in as nil:
...
8
votes
2answers
632 views
PHP isset over use? Good or bad?
First, I'm in no way an experienced PHP coder. This is my 5th time working with PHP, so if you see anything that can bee improve, please point them out for me.
I have the code checking for the ...
8
votes
1answer
511 views
Getting ready for Nullable Reference Types
Looking forward for C# Nullable Reference Types in C# 8.0. I do not like to use the preview implementation in a production code though, so just went with these simple helper classes intending them to ...
8
votes
2answers
844 views
Idiomatic Scala try option code block
Basically, this function returns a list of flights for an airline. But if the airline doesn't exist, I want to throw some custom runtime exception. If that airline does exist, I want to return a list ...
7
votes
3answers
238 views
Creating check parameter class instead of repetitive null checks
What I'm trying to accomplish is to have a error checking class mostly to check if parameters that are pass null but also data structures within class are not null. So my thought is a clean static ...
7
votes
4answers
3k views
Avoid printing attributes of the nil object in Rails
Currently, when I'm wanting to print a value for my class I do the following...
<%= @person.team.name if @person.team.present? %>
This seems really ...
7
votes
3answers
280 views
Too many null checks in an XML-to-XML transformation
I'm writing a conversion mechanism in C#. I deserialize a big XML document of one type and return a big XML document of another type (via serialization of another object).
While I'm doing the ...
7
votes
3answers
258 views
Enumerating new articles whose publication start date has passed
(cn.PublishEnd == null || cn.PublishEnd < DateTime.Now)
vs
(cn.PublishEnd ?? DateTime.MinValue < DateTime.Now)
Which ...
7
votes
2answers
663 views
Use different contructors of disposable class
I have a very simple helper class to validate credentials. Sometimes the domain has a value, sometimes not. Depending on this, a different constructor for ...
7
votes
2answers
432 views
Handling optional parameters in a servlet
I have a form that submits to a servlet that has optional parameters. If they are empty they are just ignored. This is the template I am using:
...
7
votes
1answer
2k views
Getting matching items in lists based on nullable properties
I'm iterating through two lists to find matching objects. The objects have two string properties, Id1 and Id2 below. If ...
7
votes
1answer
301 views
NullGuard for throwing more informative exceptions
This small utility class is my solution for a more convenient way for checking against null. I also wanted to have more informative NullReferenceExceptions but I ...
6
votes
3answers
2k views
REST API to list books, where null means “everything”
My company has a REST API, and one design issue that has come up a few times is that we use null to indicate "get everything".
For example /api/books?genre=romance ...
6
votes
3answers
55k views
Cast an object to Decimal? or Int? etc but leave it as null if it is null
In a case like this:
object A1 = null;
Decimal? B = Convert.ToDecimal(A1);
object A2 = null;
int? C = Convert.ToInt32(A2);
Note that in case where ...
6
votes
3answers
2k views
Checking whether textboxes have data
I always end up creating a quick return statement above a code block if the simple condition of textbox null fields are found. Naturally, these tend to build up the more controls are added. Now I'm ...
6
votes
3answers
77k views
Check for null/empty in dictionary
I am doing the following about 10 times with other strings, so the code is duplicated. How can I refactor this?
queryWhere is a ...
6
votes
4answers
878 views
Handling Null values (and equivalents) routinely in Python
I've found the following code invaluable in helping me 'handle' None values including "whitespace" characters that should be treated as None based on the situation. I have been using this code for ...
6
votes
5answers
340 views
Simplify & Reduce steps in the IsDistinct function
Because I often have to deal with two Variants that may or may not be Null, we need a null-safe equality test, so I came up with ...
6
votes
2answers
175 views
A possibly-terrible alternative to conditionals full of null checks
I am always super annoyed when I have to write a bunch of if statements whenever I have to traverse an object graph of possibly null objects. As there is not yet a <...
6
votes
2answers
555 views
How do I better structure Java code to avoid dereferencing null? [closed]
I have the situation where as long as the user gives valid input, the StringBuilder object will not be null.
...
6
votes
2answers
10k views
Extension method to return a default value for any null value
I'm creating an extension method that can be used on any type. The idea is that if this method is called, it checks if value is null. If null, it needs to return a default instance of the specified ...
6
votes
2answers
2k views
Smart pointer that cannot be nullptr
Sometimes I need a reference counting smart pointer that should not be nullptr. For example, I want to share a very heavy object without copying. And it's more convenient to place it on the heap. So I ...
6
votes
2answers
618 views
An attempt to make a linked list iterator “safe”
I was practicing my C++ routine and decided to code up this simple linked list data structure. Furthermore, I made an attempt to make the list iterator "safe" in the sense that it throws an exception ...
6
votes
1answer
992 views
Detecting gyroscope data using promises
In a plugin that I am currently authoring, I am using a function to check whether the device provides gyroscopic data. Although intuitively this can be easily resolved by checking the value of ...
6
votes
2answers
270 views
6
votes
1answer
132 views
Tomorrow: rainy with a chance of null objects
In an attempt to hide from the sun outside, I gave the community challenge "Rainfall" a try.
Code here, specific questions below.
...
5
votes
3answers
5k views
User registration Servlet
I have a servlet that processes user registration to a website. It takes inputs from an HTML form like username, password, email, etc.
MySQL Table:
...