Should I Use Separate Functions for Each Search Criteria or a Single General Function?

June 19, 2025 5 answers
Asked by Decathlon
Hey everyone! I'm diving into back-end development and currently exploring SQLite with C#/WPF. I'm facing a design choice: should I create distinct functions like `FindById()`, `FindByName()`, and `FindByEmail()`, or would it be better to just have a single function, such as `GetEntry(string field, string value)`? The single function seems cleaner, but I'm concerned it might introduce issues or not be as straightforward as I think. I'd love some advice, especially since I'm still learning and figuring things out.

Answers

26km_detour
It all depends on your application type. For example, in some research projects, it's great to let super users use detailed queries. However, if you're building something for students, a single input might be easier. It really varies based on your specific needs!
Reply to this answer
sailing lessons
Both approaches can work, but having separate functions often feels cleaner and makes the intent of your code obvious. Plus, many modern ORM frameworks auto-generate these methods for you. If you write the methods yourself, stick to what you feel is most manageable for you.
Reply to this answer
~soilEC2
I'd suggest keeping separate functions! Each of them will do just one thing, making it simpler to read and understand. If you use the generic approach, it could confuse future developers, especially if they call it with unexpected variables. Imagine how stressful it would be if they unknowingly use a field that doesn't exist anymore โ€“ you'd only find out at runtime. Itโ€™s definitely worth it to have clear and self-documenting code that helps avoid bugs later.
Reply to this answer
okra.in.season
It's usually best to go with separate methods like `FindById()`, `FindByName()`, etc. because they clearly state what each function does, making the code easier to understand. However, you can have those methods internally call a generic method like `GetEntry()` to handle the actual database logic. This way, you maintain clarity in the code's intent while also having reusable code underneath. Just be cautious with using a plain string for `field`; it's better to define it as an enum to prevent errors due to typos or incorrect values.
Reply to this answer
retake_the quiz
A single entry point for multiple operations can make unit testing a hassle and might not align with the SOLID principles of OOP. Keeping methods focused on single tasks helps in maintaining clean, testable code.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions