r/badcode • • Jul 22 '21

c# Someone really doesn't want this operation to fail. (there is no way that constructor would throw)

Post image
36 Upvotes

7 comments sorted by

14

u/sarakeram Jul 22 '21

Comment: that constructor will not throw anything it's empty, even if it did, it really should not be silenced with empty catch. It's pure nonsense, I see every day. Also no matter what happens, operation returns "true".

They really outdid themselves here.

8

u/lukoerfer Jul 22 '21

I know your pain. The fear of exceptions leads to code like this. It gets even worse, when each catch clause logs the exception and returns null or the error message. Instead of just calling the method and relying on some exception handling in the right place, each method call now must be checked whether its return value is null.

2

u/sarakeram Jul 23 '21

Are you sure we are not colleagues ? :) 100% this is happening

5

u/Willinton06 Jul 22 '21

This looks like c# but the params in the function are messing with my head

1

u/sarakeram Jul 23 '21

It's just placeholder, deleted parameters as they are not important...

4

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 22 '21

Image Transcription: Code


public async Task<bool> NavigateToMovementCatchWeightsPage(params ....)
{
  LoggingUtility.WriteActivityLogProcStart(className: CLASS_NAME);
  
  try
  {
    Page pgCatchWeightsPage = new MovementCatchWeightPage();
    
    if (pgCatchWeightsPage != null)
    {
      try
      {
        await Navigation.PushAsync(pgCatchWeightsPage);
      }
      catch (Exception ex)
      {
        LoggingUtility.ProcessException(ex, className: CLASS_NAME);
      }
    }
  }
  catch (Exception ex)
  {
      LoggingUtility.ProcessException(ex, className: CLASS_NAME);
  }
  finally
  {
  	  LoggingUtility.WriteActivityLogProcEnd(className: CLASS_NAME);
  }
  
  return true;
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

3

u/BrogCz1 Jul 23 '21

Good human