-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
346 lines (285 loc) · 8.12 KB
/
Program.cs
File metadata and controls
346 lines (285 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System.Text.Json;
using System.Text.RegularExpressions;
///Tic tac toe
var board = new char[3, 3]
{
{'X', 'O', 'X'},
{'O', 'X', 'O'},
{'X', 'X', 'O'}
};
var ticTacToe = new TicTacToe(board);
Console.WriteLine("Is won by X? " + ticTacToe.IsWonBy('X'));
Console.WriteLine("Is won by O? " + ticTacToe.IsWonBy('O'));
//Reducing the number of parameters - Splitting the method
var gameSaver = new GameSaver();
var gameData = new GameData();
string saveFileName = "game";
string saveFileExtension = "gdf";
var saveFileFinalPath = gameSaver.BuildFileName(
saveFileName,
saveFileExtension);
gameSaver.Save(gameData, saveFileFinalPath);
//Reducing the number of parameters - Removing a boolean parameter
var point1 = new Point(0, 0);
var point2 = new Point(10, 30);
var distance1 = CalculateDistance(point1, point2, true);
var distance2 = CalculateDistance(point1, point2, false);
var distanceInKm = CalculateDistanceInKilometers(point1, point2);
var distanceInMiles = UnitConverter.KilometersToMiles(distanceInKm);
//PersonalDataFormatter - composition approach
bool shallReadFromDatabase = false;
IPersonalDataReader personalDataReader = shallReadFromDatabase ?
new DatabaseSourcedPersonalDataReader() :
new ExcelSourcedPersonalDataReader();
var personalDataFormatter = new PersonalDataFormatter(
personalDataReader);
Console.WriteLine(personalDataFormatter.Format());
Console.ReadKey();
//Reducing the number of parameters - bundling parameters together
//here are too many parameters - better to aggregate them, as below
//IDatabaseConnection ConnectToDatabase(
// string userName,
// string password,
// string databaseName,
// string databaseServerName,
// string databaseClusterName)
//{
// throw new NotImplementedException();
//}
IDatabaseConnection ConnectToDatabase(
UserCredentials userCredentials,
DatabaseIdentity databaseIdentity)
{
throw new NotImplementedException();
}
//poor design - this method does two things instead of one
double CalculateDistance(
Point point1, Point point2, bool shouldBeAsMiles)
{
double deltaX = point2.X - point1.X;
double deltaY = point2.Y - point1.Y;
var distanceInKm = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
if (shouldBeAsMiles)
{
const double kilometersToMilesConversionFactor = 0.621371;
return distanceInKm * kilometersToMilesConversionFactor;
}
return distanceInKm;
}
//better design - two seaprate methods, no need for a boolean parameter
double CalculateDistanceInKilometers(
Point point1, Point point2)
{
double deltaX = point2.X - point1.X;
double deltaY = point2.Y - point1.Y;
return Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
}
var _authorizedUsers = new HashSet<string> { "user1", "user2" };
//reasonable usage of boolean parameter
bool IsAuthorized(string userName, bool isAdmin)
{
if (isAdmin)
{
return true;
}
return _authorizedUsers.Contains(userName);
}
//methods with one job only
float Power(float number)
{
return number * number;
}
void Greet(string name)
{
Console.WriteLine("Hello, " + name);
}
bool IsEven(int number)
{
return number % 2 == 0;
}
//this one also does one task only, but it is composed of substeps
List<User> ReadUsersData()
{
var apiConnection = ConnectToApi("https://someApi/", "api/users");
var usersAsJson = apiConnection.ReadAll();
return JsonSerializer.Deserialize<List<User>>(usersAsJson);
}
//two responsibilities instead of one - should be refactored
void SaveToFile(List<string> words, string filePath)
{
var nonEmptyWords = new List<string>();
foreach (var word in words)
{
if (!string.IsNullOrEmpty(word))
{
nonEmptyWords.Add(word);
}
}
string text = string.Join(Environment.NewLine, nonEmptyWords);
File.WriteAllText(filePath, text);
}
//after refactoring:
void SaveNonEmptyToFile(List<string> words, string filePath)
{
List<string> nonEmptyWords = GetNonEmptyOnly(words);
string textToBeSaved = string.Join(Environment.NewLine, nonEmptyWords);
File.WriteAllText(filePath, textToBeSaved);
}
List<string> GetNonEmptyOnly(List<string> words)
{
var strings = "some,strings,split,with,commas";
var split = strings.Split(',');
var nonEmptyWords = new List<string>();
foreach (var word in words)
{
if (!string.IsNullOrEmpty(word))
{
nonEmptyWords.Add(word);
}
}
return nonEmptyWords;
}
IApiConnection ConnectToApi(string baseAddress, string requestUri)
{
throw new NotImplementedException();
}
//this method will be hard to test,
//because static Now property cannot be mocked
string GetCurrentDayDescription()
{
DateTime today = DateTime.Now;
return $"Today is {today.DayOfWeek}";
}
//solution: a wrapper class + interface
//the interface can be mocked
interface IDateTime
{
DateTime Now { get; }
}
class DateTimeWrapper : IDateTime
{
public DateTime Now
{
get
{
return DateTime.Now;
}
}
}
public static class UnitConverter
{
public static double KilometersToMiles(double kilometers)
{
const double kilometersToMilesConversionFactor = 0.621371;
return kilometers * kilometersToMilesConversionFactor;
}
}
public struct Circle
{
public Point Center { get; }
public float Radius { get; }
//public Circle(float x, float y, float radius)
//better to bundle X and Y together as Point
public Circle(Point center, float radius)
{
Center = center;
Radius = radius;
}
}
public struct Point
{
public float X { get; }
public float Y { get; }
public Point(float x, float y)
{
X = x;
Y = y;
}
}
public interface IDatabaseConnection
{
}
public struct DatabaseIdentity
{
public string DatabaseName;
public string DatabaseServerName;
public string DatabaseClusterName;
}
public struct UserCredentials
{
public string Username;
public string Password;
}
public class User
{
}
public interface IApiConnection
{
string ReadAll();
}
public class AdaptiveHeapSort
{
//reasonable comment example:
//Implements the Adaptive Heap Sort algorithm
// https://en.wikipedia.org/wiki/Adaptive_heap_sort
public static void Sort(int[] input)
{
int length = input.Length;
// Build the initial max-heap
for (int i = length / 2 - 1; i >= 0; i--)
{
Heapify(input, length, i);
}
for (int i = length - 1; i > 0; i--)
{
// Swap the root element (maximum) with the last element
int temp = input[0];
input[0] = input[i];
input[i] = temp;
// Call heapify on the reduced heap
Heapify(input, i, 0);
}
}
private static void Heapify(int[] input, int n, int i)
{
int largest = i;
int leftChild = 2 * i + 1;
int rightChild = 2 * i + 2;
// Find the largest element among the root, left child, and right child
if (leftChild < n && input[leftChild] > input[largest])
{
largest = leftChild;
}
if (rightChild < n && input[rightChild] > input[largest])
{
largest = rightChild;
}
// If the largest element is not the root, swap them and recursively heapify the affected sub-tree
if (largest != i)
{
int swap = input[i];
input[i] = input[largest];
input[largest] = swap;
Heapify(input, n, largest);
}
}
}
public class EmailValidator
{
public static bool IsValidEmail(string email)
{
// reasonable comment example:
// Simple email validation pattern
// This pattern checks for valid characters,
// the "@" symbol, domain names, and top-level domains (TLDs).
// Examples of matching strings:
// somebody@gmail.com
// someone@yahoo.fr
//
// Examples of non-matching strings:
// somebody@gmail
// @yahoo.fr
string pattern = @"^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$";
return Regex.IsMatch(email, pattern);
}
}