Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a class or a function [closed]

Tags:

function

c#

class

Sorry for the noob question but I've always had a hard time distinguishing situations when it's good to create a function or a class. For some of the smaller programs I write at work, I write a whole bunch of functions to carry out specific tasks. The programs all work as intended. However, when I have some of my more senior developers take a look to give me their critique, they rewrite a lot of my functions completely over as a class. These are my coworkers so I don't want to look completely incompetent (I just started this job as a junior developer) by asking them why they did that. What do you guys think?

like image 669
Saif Ahsanullah Avatar asked Dec 04 '25 15:12

Saif Ahsanullah


1 Answers

That is too broad question and you really have to understand the concept of the Object Oriented Programming and when you should use it.

Note: Bellow you will find my personal opinions (some of them borrowed from great books' authors and experienced programmers), and for sure the things highlighted bellow, does not reflect the entire power of the Object Oriented thinking and design. These will be gained throughout experience and feedback.

0. A use case of a class

There are many applications, on where to use an internal class to your C# code.

Data Transfer Object (DTO)
One application (of really many) and is used many times in software, is when you are transmitting data from database to your application for processing.
What better than writing an internal class that will store your data, implement useful and re-usable methods that can be used later in your application logic (e.g isAdministrator) and so on.

1. Object-Oriented Design Patterns

I will recommend you reading a book about Object-Oriented Design Patterns.
Books like that, describe some problems scenarios that can be implemented with a class using a pattern. Once you have read about these patterns and possible scenarios on where can be used, you will be able to get the book, find the pattern and solve your problem.

A co-worker of mine, state something really useful. When you are facing a problem, you should ask yourself:

"Does this problem solved again using a design pattern?"

If the answer is yes, then you go back to your reference book to find your design pattern, that will solve your problem, without re-inventing the wheel. This approach, will teach you how and when you should use a separate class; but will also help you to maintain a communication language between you and your co-workers, that is, if you are talking about your code to a co-worker, you will be able to state the design-pattern and you will be immediately understood (given that, your co-worker know about the specific design-pattern).

2. Don't be afraid creating more than one internal classes

Another note, don't afraid to create multiple internal classes. Implement as much as possible, don't try to implement one internal class and mix responsibilities. Your class should be for a specific purpose, and should not do more than one thing (i.e responsibilities, if you are writing a class that is about transmitting data from your database to your application logic, should not - ideally - doing something else, like adding data to your database).

Consider learn more about Polymorphism, Inheritance, Encapsulation and Abstraction.

These four fundamental principles of Object Oriented Programming can also help you to learn how to structure your code object-oriented.

3. General Notes

As a Junior-Developer and not only as a Junior but as a Developer in general, you should always willing to learn from the more experience guys, by asking for feedback. Is not a shame is the law of learning and improve your code.

Another powerful source of learning, is books, consider buy some for the area you are interested in. (e.g Object Oriented Programming, Design Patterns etc).

like image 105
Rafael Avatar answered Dec 06 '25 07:12

Rafael