我只是很好奇,因为这段代码的代码复杂度为16,如果我想在CC上达到完美的9/10,那么最好的方法是什么?我知道它不会杀了我,但我很想知道其他人是否会以不同的方式编写这段代码
class SentMessages
{
public SentMessages() { }
public SentMessages(int id, string userName, string message, string messageType, DateTime createdAt)
{
this.Id = id;
this.UserName = userName;
this.Message = message;
this.CreatedAt = createdAt;
this.MessageType = messageType;
}
public string UserName { get; set; }
public int Id { get; set; }
public string Message { get; set; }
public string MessageType { get; set; }
public DateTime CreatedAt { get; set; }
public List<SentMessages> GetMessages()
{
if (AllMessages.Count == 0) AllMessages = SentMessages.InitializeMessages();
return AllMessages;
}
public List<SentMessages> AllMessages = new List<SentMessages>();
static private List<SentMessages> InitializeMessages()
{
List<SentMessages> messages = new List<SentMessages>();
return messages;
}
public void ClearMessages()
{
AllMessages.Clear();
}
}
我从http://objectlistview.sourceforge.net/cs获得了此代码的概念,并获得了stackoverlfow回答
答案 0 :(得分:2)
我认为你应该考虑原则
只是一些想法:
Message
'类作为数据持有者,以保留Id
,Username
属性SentMessages
IList<SentMessage>
类
哦,GetMessages
毫无意义。列表为空时调用它。但是,它会创建一个新的空列表。
答案 1 :(得分:1)
public class SentMessage : Message, IMessage
{
public SentMessage(int id, string userName, string message, string messageType, DateTime createdAt)
{
Id = id;
UserName = userName;
Message = message;
CreatedAt = createdAt;
MessageType = messageType;
}
public string UserName { get; private set; }
public int Id { get; private set; }
public string Message { get; private set; }
public string MessageType { get; private set; }
public DateTime CreatedAt { get; private set; }
}
public class MessageCollection<T> where T : IMessage
{
private List<T> _messages;
public MessageCollection()
{
_messages = new List<T>();
}
public IEnumerable<T> GetMessages()
{
return _messages;
}
public void AddMessage(T message)
{
_messages.Add(message);
}
public void ClearMessages()
{
_messages.Clear();
}
}