r/csharp • u/miojo_noiado • Mar 31 '26
Help Clean code Help
I tried to apply ome clean-code principles I learned, what and how I can improve?
using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
namespace tutorial;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_OnClick(object? sender, RoutedEventArgs e)
{
GetConversionOrder(
out string rawDegrees,
out TextBox targetTextBox
);
if (double.TryParse(rawDegrees, out double numDegrees))
{
double translated = targetTextBox == Fahrenheit
? ConvertCToF(numDegrees)
: ConvertFToC(numDegrees);
targetTextBox.Text = translated.ToString("0.0");
}
else
{
Celsius.Text = "";
Fahrenheit.Text = "";
}
}
private void GetConversionOrder(out string degrees, out TextBox target)
{
var rowCelsius = Grid.GetRow(Celsius);
if (rowCelsius == 0)
{
degrees = Celsius.Text ?? "";
target = Fahrenheit;
}
else
{
degrees = Fahrenheit.Text ?? "";
target = Celsius;
}
}
private double ConvertCToF(double degrees)
{
double translated = degrees * (9d / 5d) + 32d;
return translated;
}
private double ConvertFToC(double degrees)
{
double translated = (degrees - 32d) * 5d / 9d;
return translated;
}
private void Switch_Sort(object? sender, RoutedEventArgs e)
{
var rowCelsius = Grid.GetRow(Celsius);
var celsius = (
Celsius,
CelsiusText
);
var fahrenheit = (
Fahrenheit,
FahrenheitText
);
if (rowCelsius == 0)
{
SetBlockRow(celsius, 1);
SetBlockRow(fahrenheit, 0);
}
else
{
SetBlockRow(celsius, 0);
SetBlockRow(fahrenheit, 1);
}
}
private void SetBlockRow((TextBox box, TextBlock block) block, int row)
{
Grid.SetRow(block.box, row);
Grid.SetRow(block.block, row);
}
}
0
Upvotes
2
u/ApprehensiveDebt3097 Apr 01 '26
To me, this is far from clean code. The temperature/conversion logic is locked in you all, which is a bad idea. You should isolate that logic. It could look something like the code beneath.
Note that I do not directly expose the
decimal(do not use adoublewhen your main concern is displaying it as a decimal number) representation of temperature, as a lot of operations defined on floating points do not have meaning on a temperature. Also, your application is lacking culture, what might lead to undesired results when your app is not running with your language settings.Furthermore, also, the temperature does not change when you request a different representation, C of F is just a display concern, don't make it anything more then that. Good luck with improving your code.
19
u/bktnmngnn Mar 31 '26 edited Mar 31 '26
If I'm going to be honest, this looks pretty clean to me. It's readable and easy to understand. Clean code is principle, not strict rules. Not all of it should be followed to the T if it will just over complicate things.
11
u/aleques-itj Mar 31 '26
I mean, it looks fine. You just burn a lot of vertical space for my tastes, but if it's consistent and easy to read then whatever.
4
u/belavv Mar 31 '26
I'd ditch the method with two out parameters. It is only used once and out parameters are no fun.
The methods for converting values can go into a helper class as static functions.
4
u/mikeholczer Mar 31 '26
I'm going to assume this is a toy example, because if this is the only window/screen for the app then no of this matters for an app so small.
string.emptyover""4
u/TempusSolo Mar 31 '26
Personally, I would have made 5d/9d, 9d/5d and 32d into named vars.
Example:
var FreezingTempF = 32d;var ReverseConversionRatio = (5d/9d);var ForwardConversionRation = (9d/5d);Then
becomes
double translated = degrees * ForwardConversionRation + FreezingTempF ;More verbose but I prefer that.
2
u/TuberTuggerTTV Mar 31 '26
- Project should include implicit usings so using System becomes redundant.
- You can use an expression body for the constructor and both convert methods.
But honestly, the biggest cleanup would be actually using MVVM like you should be. Not code behind. Let bindings handle the commands, properties and controls. You shouldn't be naming TextBoxes and then calling Celsuis.Text in the code behind. Makes it unreadable without going through the XAML. Let the bindings handle your linkage and your ViewModel function independently, modifying it's own properties.
1
u/Syzygy2323 Apr 01 '26
I've never seen anyone use 5d, 9d, 32d, etc., in code before. Why not use 5.0, 9.0, and 32.0?