Skip to main content
Share Your Experience: Take the 2024 Developer Survey
23 votes
Accepted

Generic Null/Empty check for each property of a class

First of all I'd reduce indentation. It makes your code really too hard to read. Doing that you will see it may be simplified. First of all try/...
Adriano Repetti's user avatar
22 votes
Accepted

REST API to list books, where null means "everything"

I personally think it's fine, but it helps to turn your thinking around from a "selector" to a "filter". Then, the default is to return all books, and you selectively filter down the set you're going ...
Jon Raphaelson's user avatar
16 votes

C# consecutive and redundant null checks

Two things to think about: Could you code this so it's impossible to create an invalid Theme instance thus not having to worry about it in this method? How does a ...
RobH's user avatar
  • 17k
15 votes
Accepted

C# consecutive and redundant null checks

Since your Theme class is a user defined class, you could do something like : ...
iSR5's user avatar
  • 5,278
14 votes
Accepted

handle null in Comparator class

it is possible to handle null pointer exception using Comparator.comparing static method. If you want to comparing loanAmount fields in your objects and you want ...
dariosicily's user avatar
  • 3,961
12 votes

Checking whether textboxes have data

Why so complicated? One possible approach is: ...
tym32167's user avatar
  • 229
11 votes

Inserting a DBNull value into a database

The most terse and declarative way to write this is to use C#'s ?? operator. Your line becomes: ...
Mr Cochese's user avatar
10 votes
Accepted

Inserting a DBNull value into a database

All you have to do is to use the ?: (ternary operator): ...
t3chb0t's user avatar
  • 44.3k
9 votes

String whitespaces

Not sure that adding a 7-line validator class per validation rule (plus typecasting abuse) is any shorter/better than a set of more straightforward composable validating one-liners, something like: <...
Bronx's user avatar
  • 191
9 votes
Accepted

Error solution: Uncaught TypeError

Does this open up the possibility for any buggy behavior that I don't know about? It's not likely to. The if statement is fine, though it can be made cleaner: ...
CertainPerformance's user avatar
8 votes
Accepted

Getting ready for Nullable Reference Types

There are some disadvantanges about this solution. You not only loose the argument-name inside the exception but if you are using ReSharper then you won't be able to quick-fix this in future. Thus I ...
t3chb0t's user avatar
  • 44.3k
8 votes

String whitespaces

Edit: the tone of this might seem a bit negative, so I'll add that this was a fun one to review in hopes of lightening the mood ;) Miscellaneous thoughts: This is horrifying! Why did you do this?! ...
VisualMelon's user avatar
  • 7,431
8 votes

Simplify & Reduce steps in the IsDistinct function

You have no idea how excited I am about this. Years after learning about the Xor operator and I finally get to use it!! VBA - Logical Operators: Xor Operator ...
TinMan's user avatar
  • 4,133
7 votes

Using default None values in Python when assigning split() to a tuple

You can concatenate an array with your default value. track, num_tracks = (track_info.split('/', 1) + [None])[:2]
Clint's user avatar
  • 71
7 votes

Generic Null/Empty check for each property of a class

There are several problems with the answers here, most notably that the original question doesn't seem to be answered, namely, that we want to create a clone a class with ONLY the fields that are set ...
Stacy Dudovitz's user avatar
7 votes

Checking whether textboxes have data

While the suggested approach with enumerating and checking all text-boxes works as an easy workaround, it's a horrible solution from the user point of view because he either gets noticed about some ...
t3chb0t's user avatar
  • 44.3k
7 votes
Accepted

String whitespaces

Usability I would have expected Optional to transform null and whitespace only content into ...
dfhwze's user avatar
  • 13.9k
7 votes

handle null in Comparator class

Here is another twist, roughly similar to dariosicily's proposal. It differs in the following points: It create the aggregated comparator only once, then store it in a static field. Why? Because ...
James's user avatar
  • 171
6 votes
Accepted

Calling a function on an object if not null

To be sure it always works you should add a constraint to the method: where TInput : class This will ensure that TInput is a ...
t3chb0t's user avatar
  • 44.3k
6 votes
Accepted

Removing key/value pairs in list of dicts

Any solution will have to read the values associated to each key of each dictionary; so you won't be able to drop under \$\mathcal{O}(n\times{}m)\$ where \$m\$ is the length of each dictionary. This ...
301_Moved_Permanently's user avatar
6 votes

Smart pointer that cannot be nullptr

Well, in principle having non-nullable pointers, smart or not, is a good thing. It's just a shame that nullability is not an optional non-default trait (not only for pointers, but for all types; for ...
Deduplicator's user avatar
  • 19.3k
6 votes
Accepted

Reading a text file that may have embedded nulls

I find your code hard to read, for the following reasons: the variable file is not really a file but a buffer the variable f (...
Roland Illig's user avatar
  • 21.3k
6 votes

Handling Null values (and equivalents) routinely in Python

Generally I don't think you should have a class for this functionality. There's no state and no particular meaning to MyUtils object here. You can make this into a ...
Quinn Mortimer's user avatar
6 votes
Accepted

Multiple null Checks or try/catch NullPointerException

The problem with catching NullPointerException is “which one did you catch?” A null can be returned from ...
AJNeufeld's user avatar
  • 33.7k
6 votes

C# consecutive and redundant null checks

It sure looks somehow clumsy like it is. You should place such stuff deep down the road, meaning you should have a method inside Theme e.g. ...
Heslacher's user avatar
  • 50.4k
5 votes
Accepted

Row-wise mean imputation in R

You can use rowMeans with indexing. ...
m0nhawk's user avatar
  • 366
5 votes
Accepted

Avoiding "warning: Use of uninitialized value" when fetching an integer CGI parameter

Standard practice is to scope variables using my. Since Perl 5.10, the preferred way to write this is to use the logical defined-or operator to provide a default ...
200_success's user avatar
5 votes
Accepted

Validating that only certain values are unset

A few things: your indentation looks weird you could store data[0] in some variable with a relevant name. you could iterate over both keys and values with ...
SylvainD's user avatar
  • 29.2k
5 votes

Merging two hashes with the mean values of each key if both hashes exist

There are many ways to do this, but all have two common elements. Firstly, since hash1 and hash2 are methods, you want to only ...
Cary Swoveland's user avatar
5 votes
Accepted

Grabbing SQL records: Nullable bool to bool

// as long as we found a result, we're happy if(result != null) { return (bool)result; } This could be written as simply: ...
Mathieu Guindon's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible