Code Formatting - Functions with long Parameter Names, or many Parameters

robbonick
Member Posts: 40
Hi,
I am just wondering what the best practice is for formatting of code, I have tried to search the coding guidelines for this, but found nothing.
My question is, say I have a function called "SomeVeryVeryVeryLongFunctionName", which returns a boolean.
This then has 4 parameters, which are populated by values from a record.
So the code would look like this...
Variant 1
What are the guidelines for separating this function on to two lines, or more?
Or is it all a matter of opinion?
For instance should it be something like this...
Variant 2
or this...
Variant 3
I am just wondering what the best practice is for formatting of code, I have tried to search the coding guidelines for this, but found nothing.
My question is, say I have a function called "SomeVeryVeryVeryLongFunctionName", which returns a boolean.
This then has 4 parameters, which are populated by values from a record.
So the code would look like this...
Variant 1
IF SomeVeryVeryVeryLongFunctionName(ItemLedgerEntry."Posting Date", ItemLedgerEntry."Entry Type", ItemLedgerEntry."Document Type", ItemLedgerEntry."SourceNo") THEN BEGIN DoSomething(); END;
What are the guidelines for separating this function on to two lines, or more?
Or is it all a matter of opinion?
For instance should it be something like this...
Variant 2
IF SomeVeryVeryVeryLongFunctionName( ItemLedgerEntry."Posting Date", ItemLedgerEntry."Entry Type", ItemLedgerEntry."Document Type", ItemLedgerEntry."SourceNo") THEN BEGIN DoSomething(); END;
or this...
Variant 3
IF SomeVeryVeryVeryLongFunctionName(ItemLedgerEntry."Posting Date", ItemLedgerEntry."Entry Type", ItemLedgerEntry."Document Type", ItemLedgerEntry."SourceNo") THEN BEGIN DoSomething(); END;
0
Best Answers
-
Variant 1 dosent make sense to me, but your right.
Try to look at new code. Ex. Codeunit 80 has been rewritten, and it uses variant 3.
NAV still contains old code.
But you have to read it afterwards, so do what you like.
I understand all 3, so any developer can take over.
Follow me on my blog juhl.blog5 -
I'm personally using variant 4
IF SomeVeryVeryVeryLongFunctionName(ItemLedgerEntry."Posting Date", ItemLedgerEntry."Entry Type", ItemLedgerEntry."Document Type", ItemLedgerEntry."SourceNo") THEN BEGIN DoSomething(); END;
or sometimes variant 5WITH ItemLedgerEntry DO IF SomeVeryVeryVeryLongFunctionName("Posting Date", "Entry Type", "Document Type", "SourceNo") THEN BEGIN DoSomething(); END;
The reason is to be able easilly see when the IF condition ends and the executed code begins, and also to have the IF/THEN BEGIN/END on the same indentation level, so going with cursor up/down one will arrive at beginnig/end of the block.
I'm folding the line with parameters when it is longer than approx 100 - 120 characters - this is to ensure that when I (or someone) will be merging the code in text editor the whole line fits in editor window when two editors (or a single window with two objects code) are positioned side-by-side
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035
Answers
-
Yeah I have looked in standard, and it varies between them all to be honest0
-
Variant 1 dosent make sense to me, but your right.
Try to look at new code. Ex. Codeunit 80 has been rewritten, and it uses variant 3.
NAV still contains old code.
But you have to read it afterwards, so do what you like.
I understand all 3, so any developer can take over.
Follow me on my blog juhl.blog5 -
I'm personally using variant 4
IF SomeVeryVeryVeryLongFunctionName(ItemLedgerEntry."Posting Date", ItemLedgerEntry."Entry Type", ItemLedgerEntry."Document Type", ItemLedgerEntry."SourceNo") THEN BEGIN DoSomething(); END;
or sometimes variant 5WITH ItemLedgerEntry DO IF SomeVeryVeryVeryLongFunctionName("Posting Date", "Entry Type", "Document Type", "SourceNo") THEN BEGIN DoSomething(); END;
The reason is to be able easilly see when the IF condition ends and the executed code begins, and also to have the IF/THEN BEGIN/END on the same indentation level, so going with cursor up/down one will arrive at beginnig/end of the block.
I'm folding the line with parameters when it is longer than approx 100 - 120 characters - this is to ensure that when I (or someone) will be merging the code in text editor the whole line fits in editor window when two editors (or a single window with two objects code) are positioned side-by-side
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions