Skip to content

Code Cluthering, SRP, KISS, 10/100, ... Violation? #109

@db-007

Description

@db-007

I like MetroLog idea but I have 2 problems.

Take a look at the code I borrowed from your documentation below

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        
        // flat strings...
        if (this.Log.IsInfoEnabled)
            this.Log.Info("I've been navigated to.");

        // formatting...
        if (this.Log.IsDebugEnabled)
            this.Log.Debug("I can also format {0}.", "strings");

        // errors...
    		try
    		{
    			this.DoMagic();
    		}
    		catch(Exception ex)
    		{
                if (this.Log.IsWarnEnabled)
    				this.Log.Warn("You can also pass in exceptions.", ex);
    		}
    }

The code above pollutes otherwise nice and clean code with lots of logging calls. It gets even worse if you add to it exception handling, instrumentation calls (i.e. to measure the execution time of each method by using StopWatch, etc) and the method that should have 1 or 2 lines of code became a tangled complex spaghetti ball.

This violates KISS, SRP, 10/100 and bunch of other principles. The method above should really look like this:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        
    	this.DoMagic();
    }

It would be nice if MetroLog could be used as AOP (Aspect Oriented Programming) library, have you ever thought about that? That way, the method above could be used something like this:

    [LogAspect]
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        
    	this.DoMagic();
    }

Where all the logging statements could be moved into a new class LogAspect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions