patterncsharpModerate
FizzBuzz in accordance with standards
Viewed 0 times
withstandardsaccordancefizzbuzz
Problem
I wrote FizzBuzz. My aim is to use C# Code Standards correctly, and write flexible code. How can I improve my solution according to code standards? How can it be better in general?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private IEnumerable Sequence(int startPoint, int endPoint)
{
return
Enumerable
.Range(Math.Min(startPoint, endPoint), Math.Abs(startPoint - endPoint) + 1);
}
private bool IsDivisible(int number, int divisor)
{
return (number % divisor).Equals(0);
}
private static SortedDictionary GetRules()
{
var rules = new SortedDictionary
{
[3] = "Fizz",
[5] = "Buzz"
};
return rules;
}
private bool IsInRules(int number)
{
return
GetRules()
.Any(rule => IsDivisible(number, rule.Key));
}
private string FizzBuzz(int number)
{
if (IsInRules(number))
{
var result =
GetRules()
.Where(rule => IsDivisible(number, rule.Key))
.Aggregate(new StringBuilder(), (stringBuilder, rule) => stringBuilder.Append(rule.Value));
return Convert.ToString(result);
}
return Convert.ToString(number);
}
private void button1_Click(object sender, EventArgs e)
{
var result =
Sequence(1, 20)
.Select(FizzBuzz);
listBox1.DataSource = result.ToArray();
}
}
}Solution
This is how you end up with projects with forms with 25,000 lines of code. It's also how you end up with duplicated code.
Your (poorly named)
And what happens if you have another form that wants to do some of the same FizzBuzz things? Well, now you have to copy & paste all of your code. As soon as you start to copy & paste anything while programming, that should be a massive red flag that you're absolutely doing something wrong. You need to take a step back and figure out how to do it without copy & pasting.
In this case, we need a separate class to hold all of our FizzBuzzing logic. Now, any form or anything else, that wants to use our FizzBuzz code instantiates our
Your (poorly named)
Form1 class should only be responsible for stuff directly related to the form it's displaying. Otherwise, complicated forms end up with even more complicated code.And what happens if you have another form that wants to do some of the same FizzBuzz things? Well, now you have to copy & paste all of your code. As soon as you start to copy & paste anything while programming, that should be a massive red flag that you're absolutely doing something wrong. You need to take a step back and figure out how to do it without copy & pasting.
In this case, we need a separate class to hold all of our FizzBuzzing logic. Now, any form or anything else, that wants to use our FizzBuzz code instantiates our
FizzBuzz class and uses its method (although perhaps, we've implemented as a static class and we just call its methods without instantiation).Context
StackExchange Code Review Q#104179, answer score: 10
Revisions (0)
No revisions yet.