Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are default parameters bad practice in OOP?

Tags:

c#

oop

default

Do default parameters for methods violate Encapsulation?

What was the rationale behind not providing default parameters in C#?

like image 422
abhilash Avatar asked Mar 08 '09 18:03

abhilash


People also ask

Are optional parameters bad practice?

The thing with optional parameters is, they are BAD because they are unintuitive - meaning they do NOT behave the way you would expect it. Here's why: They break ABI compatibility ! so you can change the default-arguments at one place.

Why is default parameter important?

These allow developers to initialize a function with default values if the arguments are not supplied to the function call. Initializing function parameters in this way will make your functions easier to read and less error-prone, and will provide default behavior for your functions.

What is the benefit of using default parameter argument in a function?

Advantages of Default Arguments: Default arguments are useful when we want to increase the capabilities of an existing function as we can do it just by adding another default argument to the function. It helps in reducing the size of a program. It provides a simple and effective programming approach.

Can we use default parameter in Java?

No, the structure you found is how Java handles it, (that is, with overloading instead of default parameters). If the overloading is getting complicated. For other methods, renaming some cases or using a parameter object can help.


1 Answers

I would take this as the "official" answer from Microsoft. However, default (and named) parameters will most definitely be available in C# 4.0.

like image 195
Noldorin Avatar answered Sep 27 '22 16:09

Noldorin