Skip to content

Instantly share code, notes, and snippets.

@dstj
Last active March 4, 2025 15:45
Show Gist options
  • Save dstj/66dcbd24f1d3889422d19354a7761432 to your computer and use it in GitHub Desktop.
Save dstj/66dcbd24f1d3889422d19354a7761432 to your computer and use it in GitHub Desktop.
What is bad in this code?
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
namespace Interview;
public class WhatIsBadHere
{
private static readonly string _connectionString = "Server=localhost;Database=MyLocalDb;Trusted_Connection=True";
public void ProcessOrder(string custEmail, decimal amt)
{
try {
if (amt > 1000) {
Console.WriteLine($"Discount applied for customer '{custEmail}'!");
amt *= 0.95m;
}
FileStream fs = new FileStream("order.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
if (custEmail != null && !string.IsNullOrEmpty(custEmail)) {
if (amt > 0) {
var db = new Database()
db.SaveOrder(custEmail, amt);
try {
var emailService = new EmailService();
emailService.SendEmail(custEmail, "Thank you for your order. It is being processed!");
}
catch (Exception ex) {
}
sw.Write($"Customer: {custEmail}, Amount: {amt:C}");
}
}
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
public class Database
{
public static void SaveOrder(string custEmail, decimal amount)
{
using (var conn = new SqlConnection(_connectionString)) {
conn.OpenAsync();
var cmd = new SqlCommand($"INSERT INTO Orders VALUES ('{custEmail}', {amount})", conn);
cmd.ExecuteNonQueryAsync();
}
}
}
public static class EmailService
{
public static void SendEmail(string email, string message)
{
SmtpClient client = new SmtpClient("smtp.example.com") {
Credentials = new NetworkCredential("some-user", "some-password")
};
client.SendMailAsync("[email protected]", email, "Orders", message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment