Thursday, April 29, 2010

When there is a language feature, you don't have to use it.

In my opinion, C# language pushed a lot of features that can help programmers do bad things. One thing I don't like is the "var" keyword. In some cases, it saves some typing, but a lot of miss-using of it can cause the code hard to read. Can you figure out what the type of below variable is?

var key = retriveKeyToLanguage();

Another one I don't like is the miss use of #region tag. Normally if you keep a good coding principle and design, your class should not be long enough to make use of this tag. But strangely, some people just like to use the tag to mass the code. I recently read some source code from NInject. In general the codes are well written and easy to read. The problem I had was, it seems the author likes those garbage parts from C# too much, even in small classes, he use it to mass the otherwise beautiful code. Take a look at below code, except the first 2 regions which wrap the license nonsense and using nonsense, the rest are miss using. On my not big screen, the class should be able to be fully displayed on one page (which is extremely good!), with the mess-around-tags-and-comments, it exceeds the screen.

#region License
//
// Author: Nate Kohari
// Copyright (c) 2007-2008, Enkari, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
#region Using Directives
using System;
using Ninject.Core.Infrastructure;
#endregion

namespace Ninject.Conditions.Builders
{
///
/// A condition that takes the input of a chain of converter delegates and passes the result to
/// a predicate delegate, determining the result of the condition. This class supports Ninject's
/// EDSL and should generally not be used directly.
///
/// The root type of the conversion chain.
/// The subject type that this condition will examine.
public class TerminatingCondition : ConditionBase
{
/*----------------------------------------------------------------------------------------*/
#region Fields
private readonly IConditionBuilder _previous;
private readonly Predicate _directPredicate;
private readonly Predicate _predicate;
#endregion
/*----------------------------------------------------------------------------------------*/
#region Constructors
///
/// Creates a new TerminatingCondition.
///
/// A predicate delegate that directly examines the root of the condition chain to determine the result.
public TerminatingCondition(Predicate predicate)
{
Ensure.ArgumentNotNull(predicate, "predicate");
_directPredicate = predicate;
}
/*----------------------------------------------------------------------------------------*/
///
/// Creates a new TerminatingCondition.
///
/// The last condition builder in the condition chain.
/// A predicate delegate that determines the result of the condition.
public TerminatingCondition(IConditionBuilder last, Predicate predicate)
{
Ensure.ArgumentNotNull(last, "last");
Ensure.ArgumentNotNull(predicate, "predicate");

_previous = last;
_predicate = predicate;
}
#endregion
/*----------------------------------------------------------------------------------------*/
#region Public Methods
///
/// Determines whether the specified object matches the condition.
///
/// The object to test.
/// if the object matches, otherwise .
public override bool Matches(TRoot value)
{
if (_previous == null)
{
return _directPredicate(value);
}
else
{
TSubject subject = _previous.ResolveSubject(value);
return _predicate(subject);
}
}
#endregion
/*----------------------------------------------------------------------------------------*/
}
}

No comments:

Post a Comment