Questions tagged [null]
This tag should be used for questions where you are handling null variables or values.
158
questions
0
votes
0answers
18 views
Naming suggestion for a C macro that implements “optional-chaining”? [closed]
JavaScript now has a "optional-chaining" operator ?., which, when applied like this:
(null)?.fooMember
would yield ...
3
votes
1answer
97 views
8
votes
3answers
656 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 ...
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 ...
2
votes
1answer
160 views
Using coalescing and conditional operators to check API arguments
I have a web application that allows users to create customs items and later change those items. For eg. a change, the application takes the parameters and performs some basic sanity checks.
If the ...
1
vote
0answers
82 views
Extension method: Task<T>.CoalesceAwait
There has been ongoing concern about the question of awaiting a task that could possibly be null, as when using null-coalescing operators. Consider the following:
...
0
votes
1answer
2k views
Loop to check isNullOrEmpty then setVisibility & setText
My code below is working fine but I know it can be done more efficiently using a loop.
I'm checking the VAL (such as description, ingredient2, etc.) which are Strings from my SQL lite Db to see if ...
2
votes
1answer
50 views
Non-nullable reference type default parameters in C# 8
I was working to upgrade some code to C# 8.0 and came across an interesting pattern with default parameters and non-nullable reference types. I'm working if it's clear enough to the caller.
Given ...
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 ...
-2
votes
1answer
98 views
How to get rid of many if null checks - c# [closed]
How could I refactor this code to get it more cleaner:
...
2
votes
3answers
1k views
String whitespaces
Here is a shortest possible syntax for replacing whitespaces with null and throwing an exception where it is not allowed:
...
4
votes
2answers
4k views
handle null in Comparator class
I have a class LoanAccount that contains an attributes creationDate and loanAmount.
I ...
2
votes
1answer
253 views
check for null before or after casting
I have a list of BasicDBObject that I fetch from my database. This is basically a JSON document converted into a java Object.
Let's assume that we have a ...
51
votes
5answers
211k 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 ...
2
votes
1answer
73 views
TypeScript 3 form validation object
Please, correct me with anything that I say in here (The actual question is below the code).
I've been quickly prototyping a project (ASP.NET Core 2.1) and haven't found the need to structure it with ...
5
votes
2answers
106 views
Multiple null Checks or try/catch NullPointerException [closed]
There is A LOT of information online stating that you should NEVER catch a NullPointerException. Generally I agree, but I am wondering about this one case.
I have inherited code that requires me to ...
6
votes
3answers
79k 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 ...
13
votes
6answers
138k 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 ...
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:
...
6
votes
4answers
969 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 ...
4
votes
4answers
228 views
Reading a text file that may have embedded nulls
I've always used fgets to read a file. However, I want to read a file that may have embedded \0. I thought of using ...
2
votes
3answers
2k views
Validating the presence of properties in a document
How can I reduce cyclomatic complexity of validation and refactor it better way?
...
0
votes
1answer
75 views
Checking For multiple null references [closed]
So this question might be a bit opinion based but I am a bit confused, say i have these two code snippets -
...
1
vote
3answers
8k views
Sorting object with null values underscore sortBy
I'm sorting my object by its property ActivityOrder, which will sometimes contain null values if the user has not explicitly ...
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 ...
4
votes
2answers
156 views
Setting fields, with default values, before saving a row
I would like to find out if there's a cleaner way to go about validating input before it goes into a function. What I'm doing seems like a lot of code, I'm sure there's a better way. I would also ...
-2
votes
1answer
84 views
Refactoring null check before adding to Func collection with different predicates [closed]
I'd want to refactor that if ladder but I'm not sure how to do that or whether it is even possible
...
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 ...
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
...
-1
votes
2answers
125 views
Verifying that all fields of a unit has been suitably filled in
I have unit class which have some properties, and I have acceptancePolicy table which contain values. If values (from one row) are true method should return true. The method is below. How can I ...
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 ...
2
votes
1answer
110 views
Thumbnail caption in vue.js with possibly missing fields in caption
I am using the following to output a thumbnail with text. It works fine but I don't like having the use a conditional to check every prop before outputting.
E.g. this.post.user.name would indicate "...
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 ...
3
votes
3answers
88 views
Transforming XML with null checks vs variables
I'm working on an Spring Integration application that transforms a XML (converted by Jaxb) into a custom Object by checking various information given in the XML.
The problem is that I can't trust ...
4
votes
3answers
195 views
3
votes
2answers
686 views
Kotlin extension functions with null checks
Kotlin defines extension functions like also but they are not null checked. That's fine because you might not need the check. In case you do though, do you see any ...
2
votes
1answer
71 views
Comparing JSON documents for changes to values
Please go through below code and improve the quality by reducing the IF conditions:
I have 2 jsons, first one is the source and second one is the updated json. I have to compare both values and set ...
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 ...
4
votes
3answers
272 views
Caching an integer - distinguishing no value from zero
I would like to store an integer value in the application cache.
The GetOrSet method should always return an int, and never be null. If there is no cached value, ...
2
votes
1answer
45 views
Method that concatenates strings, some of which may be null, with several different separators
I'd like to refactor a Java method I've created, making it cleaner and more beautiful. The method concatenates some strings, which may be empty or not. I'm new on java so i'm not sure how to do that, ...
2
votes
2answers
160 views
Invalidate a JS Object when any of its values is null or empty
I created a simple Javascript method to recursively parse a JS Object and find out if any of its values or sub values is set to null or is empty string.
I could't ...
1
vote
0answers
110 views
Linear search on array
I've recently used this block of code from a quiz app where I check an array for the correct answer that was received from the browser and sent to the mobile device/application.
...
1
vote
1answer
853 views
2
votes
1answer
130 views
C# Function: IsAvailable<T>(T value)
I'm sick of writing negative code statements like String.IsNullOrEmpty(value) and so I'm writing a function that checks for availability. It should answer the ...
2
votes
1answer
336 views
Is catching null pointer exception to handle it instead of null check beforehand always a bad practice or it depends? [closed]
I wrote a static method for an enum that returns the corresponding enum instance based on the source string, trimming any whitespaces and ignoring case as:
...
2
votes
2answers
890 views
Ignore nil values and unwrap optionals returning the value as the wrapped type
My primary concern is that this has been written as a global function. I think it would be better if this was a function of the SignalProducer class but I am not ...
5
votes
1answer
15k views
Removing key/value pairs in list of dicts
I have a list of dicts that all have the same keys. If a key's value is None in all dicts then I want to remove them (A solution that creates a new dict is fine as ...
3
votes
2answers
4k views
Generic extension method for NullIf<T>() supporting nullable and non-nullable value types
I would like a simple, modestly efficient NullIf() generic extension for nullable and non-nullable value types including enums. The trickiness seems to be with ...
1
vote
2answers
51 views
Parameter guards for Firebase calls
I have code to ensure parameters existence all over the place. Is there a better way to do it? So that I do not have so many repeated codes inside different functions
...
3
votes
1answer
472 views
Redis lookup with a default value
I have some code to perform a redis lookup, but if the key to look up either doesn't exist or there's an issue connecting to Redis at all, it will use a default value.
The logic currently looks like ...