• Home
  • About
  • Contact
  • Speaking

Should not happen comment

by Piotr Stapp — on code 19 Oct 2015

The code & the comment

Today I found the simple code with the even more simple comment:

[...]
if (entry== null)
       return; // should not happen
[...]

What if the "if" just happened?

The Finagle's law is:

Anything that can go wrong, will—at the worst possible moment.

Especially in IT it is true. Unfortunately I saw variation of above code many times, which is completely sad.

Fail fast

In my private opinion the best way is to fail fast. I know that user/system/boss will be sad/broke/angry, but if abnormal situation happen even once you will be know about it.
So the easiest way is to

if (variable == null)
    throw new Exception("Oh my god the universe just doomed: entry is null");

but I cannot

If you really cannot throw an Exception, log this error. Log it in standard way with all needed information like: stack trace, context, etc. It should be easy to access as all your logs.

The perfect solution

The perfect solution is simple: combine logging and exception. This should allow to track fast what exactly happened, which should not happened


Follow @ptrstpp950

0%
Should not happen comment
words - read.
Recent Posts:

Comments

comments powered by Disqus
Piotr Stapp Author

Piotr Stapp

Developer, engineer, craftsmen, speaker & biker. User of all useful technologies. Believe in people not papers. DevOps & automation adept. In love with web technologies. I speak here for myself only.

Previous post Fighting with corporate proxy and modern tools like git, npm, bower (SSL problems) IT Security and safety world Sometimes in big companies the goal of the IT Security department is to make…
Next post Null pointer exceptions and warnings Simple code I am developer so let's start with following C# code: public static void Main() { var sampleList = GetSamples(…
All content copyright Reset your code © 2015 • All rights reserved.
Proudly published with Ghost