Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 18 | Comments: 87

filter by tags archive

Comments

Paul Stovell

One subject in basic pure math that I wish all developers would take is factorization - taking an equation, recognising and removing the duplicates and simplifying.

I still can't fathom how a developer can see the same thing over and over again - like 6 lines of code repeated every single time an exception needs to be logged - and not recognize that they can factor it out.

Paul

Grimace of Despair

"Using a naming convention where the company name is prefixed to all local variables"

I've witnessed a Database that was prefixed with the DEVELOPERS initials, as were all tables in it...

I still shiver while writing this.

Markus Zywitza
  
try
  
{
  
	ActiveRecordMediator.Save(foo);
  
}
  
catch (Exception ex)
  
{
  
	Logger.WarnFormat(ex, "Error saving Foo {0}", foo.Name);
  
	while (ex.InnerException != null)
  
	{
  
		ex = ex.InnerException;
  
		Logger.Info("Inner Exception", ex);
  
	}
  
}
  

If you count the brace lines, it's 6 lines for the exception, too. But absolute necessary unless

Castle.ActiveRecord.Framework.ActiveRecordException: Could not perform Save for Foo at ... in ...

is enough for you to see the error's cause, which isn't the case for me.

Ayende Rahien

Markus, is there a reason that you are doing it this way?

ex.ToString() gives you the inner exception as well, after all

Markus Zywitza

Hmm, it seems that the fact that I always use Logging now avenges. Perhaps I should have just used

Console.WriteLine(exception)

:-(

Markus Zywitza

What is Logging?

No special product, I simply wrote it with a capital letter because I'm accustomed to that practice. German language capitalizes all nouns, hence "Logging" instead of logging.

Ayende Rahien

Well, most of the logging frameworks that I am aware of will handle this correctly.

Markus Zywitza

Windsor's LoggingFacility with loggingApi="console" obviously doesn't.

Paul Stovell

@Markus:

class LogHelper

{

public static void Warn(Exception ex, string format, params object[] args) 

{

Logger.WarnFormat(ex, format, args);

while (ex.InnerException != null)

{

ex = ex.InnerException;

Logger.Info("Inner Exception", ex);

}

}

}

Now in the many places you use that code, you can have:

try

{

ActiveRecordMediator.Save(foo);

}

catch (Exception ex)

{

LogHelper.Log("Error saving Foo {0}", foo.Name, ex);

}

}

There are thousands of libraries which require multiple lines to do something. There's no reason you should write those multiple lines twice.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. Buffer allocation strategies: A possible solution - 2 days from now
  2. Buffer allocation strategies: Explaining the solution - 3 days from now
  3. Buffer allocation strategies: Bad usage patterns - 4 days from now
  4. The useless text book algorithms - 5 days from now
  5. Find the bug: The concurrent memory buster - 6 days from now

There are posts all the way to Sep 11, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    03 Sep 2015 - The industry at large
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats