1

If you have multiple variables on one line it's always a guess which one might be the null.

Sometimes I split one inline statement over two statements on purpose so I can easier understand what variable it's talking about if an exception occurs.

But I assume that the compiler can anyway decide to merge these lines into one 'thing'? So why can it then differentiate between the variables, but not if it's in one statement?

I assume there is a really good reason, but to understand compilers and the underlying workings better; why can't it tell me which variable (or column) is causing the nullref exception?

// NullRefException pointing to this line
if ( GetSomeItemFromX().AmountOfLolCats > GetSomeItemFromY().AmountOfLolCats )
    return "yay";

vs

// now I can see which method is causing problems   
int amountX = GetSomeItemFromX().AmountOfLolCats;
int amountY = GetSomeItemFromY().AmountOfLolCats;

if ( amountX > amountY )
    return "yay";

A typical ASP.NET exception; which one is null?

enter image description here

I hope someone can teach us about the underlyings of compilers and a better understanding of things happening under the hood and the tradeoffs that are being made on that level.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Dirk Boer
  • 8,522
  • 13
  • 63
  • 111
  • 2
    It will be a very expensive operation for the Common Language Runtime to lookup the name of the variable / property that is causing the issue. – Nick Dec 04 '18 at 09:07
  • 2
    Apart from this I can´t see how your image relates to your code. Anyway in order to see debug-information you need the pdb-fle for your assembly. – MakePeaceGreatAgain Dec 04 '18 at 09:08
  • 1
    I'm not sure about C#, but usually when a program is compiled, information like variable names is thrown away. C# stores the variable names in a .PDB file for use with a debugger. – Hans Kilian Dec 04 '18 at 09:10
  • 1
    @HimBromBeere; can you figure out from the image which of the two variables is pointing to null? The whole callstack doesn't say anything more, and all the debugging data is loaded. – Dirk Boer Dec 04 '18 at 09:10
  • Required reading: https://github.com/dotnet/coreclr/issues/25#issuecomment-332675052, https://blogs.msdn.microsoft.com/devops/2016/03/31/using-the-new-exception-helper-in-visual-studio-15-preview/ and https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2371587-better-nullpointerexception-error-message. – Patrick Hofman Dec 04 '18 at 09:11
  • 1
    @DirkBoer You have three candidates for null on that line. ;) – Silvermind Dec 04 '18 at 09:12
  • @Silvermind argh true ;) – Dirk Boer Dec 04 '18 at 09:14
  • 2
    _It will be a very expensive operation for the Common Language Runtime to lookup the name of the variable / property that is causing the issue._ Um, when we crash, time is not an issue. – TaW Dec 04 '18 at 09:24

0 Answers0