Using C# as an example. Say there is a class named ExampleClass and the variable is just value. Why would one declare a variable like this
private ExampleClass value;
Using C# as an example. Say there is a class named ExampleClass and the variable is just value. Why would one declare a variable like this
private ExampleClass value;
You wouldn't declare a variable named value
unless there was some unavoidable requirement to do so.
The reason why is because it is not meaningfully named.
When we name variables meaningfully, our code becomes self-documenting. Well documented code is always worthy goal.
This is a field and can be declared inside a class. Example:
private class Test
{
private int _customField;
}
_customField
is my field with type int
(instead of ExampleClass
). People use it to make something more accessible since C# isn't just functions, it's OOP.
There is a thing called Primitive Obsession. Using a class/specific type here is the solution to it.
If you use a class, you got all the Compiler Type checking to help you avoid invalid values.
Take this Enum vs Integer question for examples.