Buscar en este blog....

jueves, 2 de marzo de 2017

Little Awful Practices (I)

The first post of this series is about repeating code. We all know that code repetition doesn't smell good, but sometimes we just don't have the time to fix it (which I know, should however be no excuse). But I've recently seen this piece of code, which really annoyed me. Why? Not only because it's a lot of repeated code, but also it was like 70 lines of code instead about the 17 it could be. One must scroll the code over and over to see what is was doing instead of having all in the same screen! I know, that can't hurt anybody, but I still disagree with this smalls things ;)

So, this was the original code:

public static void LoadPersistentData() 
{
  try 
  {
    // load plug-in configurations
    Manager01.LoadPersistentData();
  } 
  catch( Exception ex ) 
  {
    Utilities.Logger.Instance.LogError( ex );
  }

  try 
  {
    // load user preferences
    Manager02.LoadPersistentData();
  } 
  catch( Exception ex ) 
  {
    Utilities.Logger.Instance.LogError( ex );
  }

  // lots of try-catchs...
  
  try 
  {
 // load model configurations
    Manager07.LoadPersistentData();
  } 
  catch( Exception ex ) 
  {
    Utilities.Logger.Instance.LogError( ex );
  }
}


I've change the real name of the objects because of privacy, but they were all "managers". If we just apply some basic refactoring, just look what do we have in the end. Is it worth the "effort"?

public static void LoadPersistentData()
{
 try
 {
  Manager01.LoadPersistentData();
  Manager02.LoadPersistentData();
  /// lots of calls...
  Manager07.LoadPersistentData();
 }
 catch (Exception ex)
 {
  Utilities.Logger.Instance.LogError(ex);
 }
}

I think it's not a big deal to perform such refactoring... And it also doesn't take any time to do it, whether  you have the time or not.

No hay comentarios:

Publicar un comentario

Comments are subject to moderation, only in order to avoid insults and disguising things.

Los comentarios están sujetos a moderación, solo con el fin de evitar insultos y cosas por el estilo.