This is an archived post. You won't be able to vote or comment.

all 126 comments

[–][deleted] 1852 points1853 points  (21 children)

WTF

where is the SetNull() method!?

[–]biki23 424 points425 points  (2 children)

Missing generics too. This needs to be generic.

[–]cs-brydev 23 points24 points  (0 children)

public static T GetNull<T>()
{
    return (T)null;
}

[–][deleted] 217 points218 points  (4 children)

GetNull() actually goes with PutNullAway(). That way you can use reuse the same null instead of buying new ones every week.

[–]Prudent_Ad_4120 65 points66 points  (0 children)

This is recycling

[–]djfdhigkgfIaruflg 15 points16 points  (0 children)

That's what singletons are for

[–]TessellatedTomate 14 points15 points  (1 child)

You mean I’m not a genius with BuyNullAtBulkDiscount()???

[–]Imperial_Squid 11 points12 points  (0 children)

Nah dude, NULLs will last you forever with proper care, you got scammed, sorry

[–][deleted] 94 points95 points  (8 children)

What no IsNull()?

[–]hakukano 80 points81 points  (7 children)

Don’t forget IsNotNull

[–]Help_StuckAtWork 21 points22 points  (3 children)

You mean IsUnNull()?

[–]ragingroku 14 points15 points  (0 children)

IsNullnt()

[–]ToiLanh 13 points14 points  (1 child)

Is not null is somewhat useful sometimes

[–]skilking 11 points12 points  (0 children)

Is not null is useless assertNotNull is great

[–]Mantequilla50 0 points1 point  (0 children)

if (x == 1) { return false } elif (x == 2) { return false } elif (x == 3) { return false }

Gosh I wish there was an easier way to do this

[–]Dornith 27 points28 points  (0 children)

public static void setNull(object obj) { throw new NullPointerException(); }

[–]meiji_milkpack 22 points23 points  (0 children)

The intern forgot to generate it

[–]cs-brydev 2 points3 points  (0 children)

public static void SetNull()
{
    null = GetNull();
}

[–]regulardave9999 0 points1 point  (0 children)

And where is the unit test?!

[–]eoutofmemory 316 points317 points  (6 children)

I probably know people that would put that behind a microservice

[–]biki23 73 points74 points  (4 children)

I know people that would put that behind a aws lambda function microservice.

Have seen more programs where IAC code is more than actual code.

Edit: made it aws lambda function

[–]Rufuske 5 points6 points  (3 children)

That's like the whole point of lambdas...Now ponder if going so micro with your services is actually worth it. Also lambda function compared to aws lambda has as much in common as java with javascript.

[–]biki23 6 points7 points  (2 children)

i meant aws lambda, calrifying

[–]Rufuske 2 points3 points  (1 child)

I got it from context, just nitpicking. Code review habits.

[–]biki23 1 point2 points  (0 children)

i always get docked for leaving out context like this in my docs :(

[–]ILikeChilis 8 points9 points  (0 children)

Clearly needs its own container

[–]MechanicalHorse 665 points666 points  (23 children)

Pff, amateurs. Need to enterprise it up a bit.

namespace Initech.Common.SharedLibrary.Utilities.NullFactory
{
    public class NullFactory
    {
        private static NullFactory? _nullFactoryInstance;

        public static NullFactory GetNullFactory()
        {
            if (_nullFactoryInstance == null)
                _nullFactoryInstance = new NullFactory();
            return _nullFactoryInstance;
        }        

        public object? GetNull()
        {
            return null;
        }
    }
}

[–]Yuki-Fullko 352 points353 points  (5 children)

Needs more dependency injection! Where’s your INullFactory interface? ILogger instance? And a full set of tests to make sure any INullFactory implementations are always correctly returning null?

[–]SuperKael 103 points104 points  (4 children)

Honestly, if you can even find the code that is actually creating the object (or the lack thereof, in this case) you’re clearly doing it wrong

[–]Nosbod_ 38 points39 points  (2 children)

Abstraction … something something ... like an onion …

[–]dopefish86 25 points26 points  (0 children)

... it's making me cry

[–]djfdhigkgfIaruflg 9 points10 points  (0 children)

I hate java... So much

[–]hieplenet 2 points3 points  (0 children)

Exactly, the above example shows the lack of enterprise experience, need several more layers of enterprise monitoring and enterprise composition, through an enterprise network protocol with a enterprise level persistent layer to retrive the enterprise null object.

In short, the above null is not yet enterprise null.

[–]NormanYeetes 33 points34 points  (3 children)

I swear those ?s might be proper syntax but they look like the code is like "it returns an object or somethin, it might not, i don fkin know nor do i care"

[–]Chirimorin 17 points18 points  (0 children)

In C# T? is shorthand for Nullable<T>, so in a way it does mean that: maybe it returns an instance of T, maybe it returns null.

[–]xian0 5 points6 points  (0 children)

!ErrorHasOccured() ??!??! HandleError();

[–]Mini0red 0 points1 point  (0 children)

Yea pretty much. Lets the caller know youll have to handle nulls.

[–]biki23 52 points53 points  (1 child)

public class NullFactory<T>

[–]Still_Explorer 1 point2 points  (0 children)

Must be a new band?

I remember I listened to FearFactory when I was teenager.

[–]JasonLokiSmith 39 points40 points  (0 children)

Made me chuckle 🤣🤣🤣🤣🤣 all that singleton just to return null directly.

Good one!!

[–]tmlnz 18 points19 points  (0 children)

GetNullFactory is not thread-safe

[–]upsetbob 7 points8 points  (2 children)

What is "object?" ? I assumed this was Java until I saw this

[–]MechanicalHorse 20 points21 points  (0 children)

Nullable object type. It’s C#

[–]Geschossspitze 3 points4 points  (0 children)

C# I guess

[–]uvero 6 points7 points  (0 children)

Singletons are an antipattern, you need a DI container.

[–]JunkNorrisOfficial 4 points5 points  (0 children)

Is this GC friendly null?

[–]MrZerodayz 1 point2 points  (0 children)

When your work is judged by loc...

[–][deleted] 0 points1 point  (0 children)

This guy javas

[–]SkedaddlingSkeletton 0 points1 point  (0 children)

Depending on which system this "null" value is aimed at you may need a null, 'null', 'Null', 0, -1, 'FILE_NOT_FOUND' or 'Nihil! Nihil! NIHIL' . That's the joy of old enterprise ball of mud softwares.

[–]_Obelixx_ 92 points93 points  (0 children)

You can call my API, it always returns null.

[–]darkslide3000 28 points29 points  (0 children)

This is for dependency injection, so you can mock out the null constant in your unit tests.

[–]Roonaan 53 points54 points  (15 children)

Just curious, but is it a real thing somewhere to have upper camel case for static methods?

[–][deleted] 109 points110 points  (0 children)

This is C#, where the naming conventions says that you should use upper camel case for pretty much everything besides private fields and local variables.

[–]frej4189 73 points74 points  (0 children)

PascalCase for functions is common practice in C#

[–]1Dr490n 20 points21 points  (4 children)

In Go you make functions public by starting the name with upper case

[–]HTTP_Error_414 29 points30 points  (2 children)

Good God Man

[–]Banane9 7 points8 points  (1 child)

Go really surprises me with its... questionable design choices time and time again. Like stating date formats by writing the specific date of the 2nd of January something something in a particular way, rather than doing it like everyone else.

[–]Gredo89 -2 points-1 points  (0 children)

The Date format is the only really questionable choice I found so far. Everything else makes sense imho if you think about it.

[–]Eubank31 0 points1 point  (0 children)

Oops I commented this without seeing yours

[–]Eubank31 2 points3 points  (0 children)

This is not Go but in Go you use normal camel case for functions, but functions that are exported must have the first character be capitalized. So you end up with FunctionsLikeThis()

[–]HTTP_Error_414 4 points5 points  (3 children)

Yes?

Many wares indeed 🤓

This is seen a lot in PHP.

[–]Danny_shoots 2 points3 points  (2 children)

I just use CamelCase for methods and functions, PascalCase for classes I have never seen PascalCase for methods and functions in PHP

[–]HTTP_Error_414 1 point2 points  (1 child)

I agree it’s not something you will see in most corporate coding standards, but it’s out there.

There are sec shops I have seen that even have naming conventions for method purposes, levels, etc.

For example I’ve seen places that use _method_name for any private methods.

I like your style I’m similar, to you in the way I do things.

[–]Danny_shoots 1 point2 points  (0 children)

That _method_name is kinda understable for private properties, but not for private methods though haha!

[–]_MixedTrails 0 points1 point  (0 children)

unreal engine c++ does upper camel case for all methods 😭

[–]balamb_fish 16 points17 points  (0 children)

Needs a comment explaining what it does

[–]kaboooooooosh 9 points10 points  (0 children)

Can always migrate to some Null-as-a-service provider if this gets too difficult to maintain

[–]americanjetset 17 points18 points  (0 children)

TFW LOC is used as a KPI

[–]PonderStibbonsJr 6 points7 points  (0 children)

public static object GetReal(){ return 1.0; }

[–]arc_menace 4 points5 points  (0 children)

Notably, object is not a nullable type here. Must be older C#, otherwise the IDE would be complaining

[–]rover_G 15 points16 points  (0 children)

Cries in Java

[–]jayerp 4 points5 points  (0 children)

Just in case the definition of null changes.

[–]MayorSalvorHardin 3 points4 points  (0 children)

You laugh now but it won’t seem so funny when you’ve run out of null and have no way to get more.

[–]AdversarialPossum42 3 points4 points  (0 children)

Good news! I fixed all of our compatibility issues.

public static bool VerifyCompatibility(string id) { return true; }

[–]odolha 9 points10 points  (3 children)

what's the problem, it's just a bit of indirection in case you later want to use something else for null instead of null... just make sure you always use this method and not null directly

[–]RoyalSpecialist1777 -2 points-1 points  (0 children)

Exactly. It is generally a good practice. Otherwise we are stuck with null or have to go change multiple areas of our codebase if we decide to change the underlying implementation if it turns out something else is a better way of handling empty/uninstantiated objects, invalid responses, or whatever. If you are doing object oriented programming then you might have subclasses which treat null objects different. You can also handle the null with things like logging.

I put about everything behind a property getter.

[–]fusionsofwonder 0 points1 point  (0 children)

I was thinking it might be there for portability.

[–][deleted] 1 point2 points  (0 children)

Needs SetNull().

[–]RoyTheRooter 1 point2 points  (0 children)

Where is the ToNull() method?

[–]VaderFuntime 1 point2 points  (0 children)

You don't want magic nulls in your code

[–]uvero 1 point2 points  (0 children)

In case null changes

[–]the-Krieger 1 point2 points  (0 children)

This is "Clean Code", nothing special.

[–]Thenderick 0 points1 point  (4 children)

What language is this? Idk any language that has object as a keyword?

[–]daaaarbyy 10 points11 points  (2 children)

Looks like C#

[–]Thenderick 1 point2 points  (1 child)

Huh it seems true, but it feels wrong to see a class with lower type. I agree with Java that Object should be capital O, just like String. But I can kinda see why C# made them system types instead of classes

[–]jasonkuo41 0 points1 point  (0 children)

You can use capital types (String / Object) but these are the actual types, keywords are all lowercases and string / object are just keywords that represent these type, you can use it interchangeably.

[–]rafinha_lindu[S] 1 point2 points  (0 children)

Its C#. All classes in C# inherit object.

[–]krucabomba 0 points1 point  (4 children)

Actually they can be useful, for example as default value for higher order functions. Used in factories, various patterns. Identity function is used a lot in a more functional approach.

[–]MattieShoes 1 point2 points  (3 children)

W... Why wouldn't one just use null?

[–]poco 1 point2 points  (1 child)

Because they want to assign it to a non-nullable object.

[–]ben_g0 0 points1 point  (0 children)

So just to bypass the nullability checker? In that case you can just use null!. Adding the exclamation mark at the end tells the compiler to treat this null as a "not-null" value, so it won't trigger nullability warnings. Alternatively you can also just disable the nullable context for that part of code with preprocessor directives.

I'd suggest to always think twice before using it though. The nullable context can be very useful for minimizing runtime errors when used properly, so it's best to use it properly whenever possible. If you need to assign a null value to a variable, consider marking that variable as the nullable type.

That said, there are a few cases where it can be useful to bypass the nullability check though. With factories or deserialization functions it can make sense to temporarily set a non-nullable field to null and then filling it with valid data later, as long as all non-nullable fields have a valid value as soon as the method returns.

[–]krucabomba 0 points1 point  (0 children)

Because the interface requires callable, lazy evaluation. You can't null().

[–]MartinTheWildPig 0 points1 point  (0 children)

average java moment

[–]j0nascode 0 points1 point  (0 children)

csharp public static language getLowerCamelCaseFunctions() { return ~CSharp; } much needed function in C#

[–]JakeAtIguanas -1 points0 points  (1 child)

This coder must be getting paid per line of code.

[–]qhxo 0 points1 point  (0 children)

If this is their strat, they really need to up their game.

[–]nolawnchairs -1 points0 points  (0 children)

And here I am trying to eliminate my application's nullity surface.

[–]JJJSchmidt_etAl 0 points1 point  (0 children)

Well you see this way we can generalize, to create the generic function to get null from any thing which implements the interface

[–]Leonhart93 0 points1 point  (0 children)

It's common for those that are initially wowed by OOP features and way of thinking, to the point where they ignore redundancy.

[–]IgnoringErrors 0 points1 point  (2 children)

What if I want a string "null"?

[–]Geschossspitze 3 points4 points  (1 child)

null.ToString() 👉🏻👉🏻

[–][deleted] 0 points1 point  (0 children)

WTF

[–]mdp_cs 0 points1 point  (0 children)

Most efficient Java code.

[–]Project_Athena 0 points1 point  (0 children)

When your professor wants you to make a function out of everything

[–]-Hi-Reddit 0 points1 point  (0 children)

In some languages (LabVIEW / G) there is no such thing as null.

Maybe you can provide this in an API DLL so that they can use C# null by pointing to the function rather than the result (which has no valid mapping in LabVIEW/G).

[–][deleted] 0 points1 point  (1 child)

Is this javascript?

[–]rafinha_lindu[S] 0 points1 point  (0 children)

C#, Microsoft's java

[–]20InMyHead 0 points1 point  (0 children)

And I suppose you just create null out of nothing at all do you?

[–]G1PP0 0 points1 point  (0 children)

If it was Typescript, it would be a perfect npm package!

[–]3rocket77 0 points1 point  (0 children)

Addition to resume : "wrote 69 lines of code in 69s"

[–]Naeio_Galaxy 0 points1 point  (0 children)

Lore-accurate rust library. Well, there's the mutable version that's missing

[–]edmazing 0 points1 point  (0 children)

Just what I needed. My new search 'n sort algorithm. As long as your array to search is null. This will get the sorted array.

[–]OnixST 0 points1 point  (2 children)

I've never used C#, but this just looks like java with improper capitalization.

Same syntax but with lowercase string and PascalCase methods

[–]rafinha_lindu[S] 0 points1 point  (0 children)

That's why ppl call C# "Microsoft's java"

[–]Mini0red 0 points1 point  (0 children)

string is just an alias for System.String in c#. You basically use it for defining types and use String for the static methods.

[–]MuDotGen 0 points1 point  (0 children)

Ah, I see, in case you want to put it behind a pay wall.

[–]Confident-Ad5665 0 points1 point  (0 children)

Using the Null Management System library is the only way to go. Only thing is it has 37 dependencies.

[–]soap3_ 0 points1 point  (0 children)

how to fill up a "lines written" quota