New answers tagged

6

One way that you can simplify much of this code is by building out a set of classes to represent your different operations. You can then encapsulate the rules for that operation within those classes. To get started, you can define an abstract Operation class to define the shared properties and methods of the operations: public abstract class Operation { ...


1

This is my refactor based on all the suggestions made here. Works quite nice. Thank you all for your help and teaching me a few things. Stopwatch.cs using System; namespace ExerciseOne { public class Stopwatch { private DateTime timeStart; private DateTime timeStop; private bool isRunning; private void StartTimer(...


2

I agree with @Henrik_Hansen's answer. My answer builds upon his. I absolutely don't see a reason for anything to be static. Your Stopwatch class should only be for a stop watch. Right now you have commingled a UI into the stopwatch. In the name of Separation of Concerns, I see no reason for any Console.Write or Console.WriteLine in the Stopwatch class....


6

public static void RunProgram() { .... } Why do you run the application as a method on the Stopwatch class it self? You have made all the methods (StartTimer(), StopTimer()) private. This means that they only can be run from your RunProgram method, which occupy the main thread of the program and measures nothing. This set up makes the entire effort ...


2

You need to consider using classes more often. For your current work, you need two classes (at least) to handle the application process. A class would be for holding the Questions and another class to hold the application logic. // define the number of levels you need which would be used for random. // so if the user chose Easy, this means it will pick ...


1

I would create more classes: An interface Operation with the functions: getQuestionString checkAnswer at the moment, you don't need this. Add this when you want to add questions with multiple possible answers eg. 3/4 and 6/8 getAnswer next checkAnswer takes a String: the one the user answered and return if it is the correct answer. You then can create ...


3

Refactor the code so each file type gets it's own function for counting the words like excel_counter() and pdf_counter(). Then use a dict to map file extensions to the functions. Something like: def docx_counter(file): text = docx2txt.process(file) return len(text.split()) def txt_counter(file): f = open(file, "r") text = f.read() ...


3

I recommend using pathlib and Path objects instead of os, and you should use a context manager when manipulating files (e.g. with open("file.txt", "r") as file: ...). You also have a lot of repeating code when you're checking extensions, and you keep checking the rest of the if statements even if it's matched an earlier one. And the final else: pass does ...


Top 50 recent answers are included