-
Notifications
You must be signed in to change notification settings - Fork 14
Function expression without braces #44
Comments
There are many reasons why you would want an expression without braces. We mainly use kotlin for tests and test factories. Kotlin allows us to have 'default values' for tests by using a style like this fun documentDescriptorAssociatedEvent(
propertyId: PropertyId = PROPERTY_ID,
appraisalId: AppraisalId = APPRAISAL_ID,
loanTransactionId: LoanTransactionId = LOAN_TRANSACTION_ID,
documentDescriptors: List<DocumentDescriptorId> = listOf(DOCUMENT_DESCRIPTOR_ID)
) = DocumentDescriptorAssociatedEvent(propertyId, appraisalId, loanTransactionId, documentDescriptors) Note that this is a test factory, it's only purpose is to define default values for tests in the case that you don't want to provide values. Every single one of our test factory methods are only set to call a constructor. Adding in braces, a return type and a return makes that code at least 50% greater in length depending on how you format it (due to that line now overflowing the line length limit). fun documentDescriptorAssociatedEvent(
propertyId: PropertyId = PROPERTY_ID,
appraisalId: AppraisalId = APPRAISAL_ID,
loanTransactionId: LoanTransactionId = LOAN_TRANSACTION_ID,
documentDescriptors: List<DocumentDescriptorId> = listOf(DOCUMENT_DESCRIPTOR_ID)
) : DocumentDescriptorAssociatedEvent {
return DocumentDescriptorAssociatedEvent(propertyId, appraisalId, loanTransactionId,
documentDescriptors)
} |
I suggest the alternative
|
@snowe2010 I don't see a single function expression in your code snippets, only functions. Are you sure we are talking about the same thing? |
@voddan I think I am misunderstanding your suggestion then. |
@snowe2010 A function expression is a special syntax for anonymous functions:
|
oh wow. ok, yes I was way off. I thought you were referring to functions bodies being created using |
Do NOT use
functional expression
in the short function declaration form without curly braces.The short declaration for is almost never used for function expressions, and when it is, it easily leads to terrible unreadable code:
Since the most common reason to use a function expression over a lambda is to get early
return
s, this rule should not affect production code, but it is worth it to have it in the formatter nevertheless.The text was updated successfully, but these errors were encountered: