Best Practice for Function Input

Is there a guideline or recommendation about how to pass object to functions? Is it recommended to always pass the full object to a function:
calculateSomething(car1, car2, aircraft)
Or is it better to only pass the properties that are really needed to the function?
calculateSomething(car1.speed, car1.length, car2.speed, aircraft.height)
The first approach seems to be more convenient, especially when the function requires many more properties. However, my intuition tells me that the second approach is more computation efficient as the function does not has to handle the full objects.
Is there a general programming advice for this or is it for every function a trade-off between readability and speed?

답변 (1개)

BhaTTa
BhaTTa 2024년 10월 21일

0 개 추천

Hey @Ni Wi, Its better to use the first way as it is more readable and not does not get cluttered as the number of properties of the class 'car' that function needs as an argument increases. Also you can pass the object by refernce in order to avoid creating of new object.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2018년 1월 19일

답변:

2024년 10월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by