Is there a way a more efficient way to write the IF statement
up vote
1
down vote
favorite
Is the there a more efficient way to write the below IF statement so I only need 2 lines of code or can this be solved with a ternary operator ?
if(postIds.size()>0)
PostProperty(postIds);
if(putIds.size()>0)
PutProperty(putIds);
apex list conditional if
add a comment |
up vote
1
down vote
favorite
Is the there a more efficient way to write the below IF statement so I only need 2 lines of code or can this be solved with a ternary operator ?
if(postIds.size()>0)
PostProperty(postIds);
if(putIds.size()>0)
PutProperty(putIds);
apex list conditional if
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Is the there a more efficient way to write the below IF statement so I only need 2 lines of code or can this be solved with a ternary operator ?
if(postIds.size()>0)
PostProperty(postIds);
if(putIds.size()>0)
PutProperty(putIds);
apex list conditional if
Is the there a more efficient way to write the below IF statement so I only need 2 lines of code or can this be solved with a ternary operator ?
if(postIds.size()>0)
PostProperty(postIds);
if(putIds.size()>0)
PutProperty(putIds);
apex list conditional if
apex list conditional if
asked Nov 10 at 10:57
Thomas
631213
631213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
We use the coding convention that the curly brackets can be skipped if the condition is placed on a single line to make these common cases less cluttered looking. (Such conventions generally cause great debate though.)
So in your case it would be:
if (postIds.size() > 0) PostProperty(postIds);
if (putIds.size() > 0) PutProperty(putIds);
Your logic has 4 outcomes so a simple ternary if
won't work as that only has 2 outcomes; ternary if
is really for methods that return a value that is to be assigned to a variable.
Guards like those could be put inside the methods, particularly if the methods are called from several places. Then the calling code reads more cleanly.
If by "efficiency" you are thinking about execution speed then don't: making it easy for people to understand what the code is doing is the most important thing most of the time.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
We use the coding convention that the curly brackets can be skipped if the condition is placed on a single line to make these common cases less cluttered looking. (Such conventions generally cause great debate though.)
So in your case it would be:
if (postIds.size() > 0) PostProperty(postIds);
if (putIds.size() > 0) PutProperty(putIds);
Your logic has 4 outcomes so a simple ternary if
won't work as that only has 2 outcomes; ternary if
is really for methods that return a value that is to be assigned to a variable.
Guards like those could be put inside the methods, particularly if the methods are called from several places. Then the calling code reads more cleanly.
If by "efficiency" you are thinking about execution speed then don't: making it easy for people to understand what the code is doing is the most important thing most of the time.
add a comment |
up vote
4
down vote
accepted
We use the coding convention that the curly brackets can be skipped if the condition is placed on a single line to make these common cases less cluttered looking. (Such conventions generally cause great debate though.)
So in your case it would be:
if (postIds.size() > 0) PostProperty(postIds);
if (putIds.size() > 0) PutProperty(putIds);
Your logic has 4 outcomes so a simple ternary if
won't work as that only has 2 outcomes; ternary if
is really for methods that return a value that is to be assigned to a variable.
Guards like those could be put inside the methods, particularly if the methods are called from several places. Then the calling code reads more cleanly.
If by "efficiency" you are thinking about execution speed then don't: making it easy for people to understand what the code is doing is the most important thing most of the time.
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
We use the coding convention that the curly brackets can be skipped if the condition is placed on a single line to make these common cases less cluttered looking. (Such conventions generally cause great debate though.)
So in your case it would be:
if (postIds.size() > 0) PostProperty(postIds);
if (putIds.size() > 0) PutProperty(putIds);
Your logic has 4 outcomes so a simple ternary if
won't work as that only has 2 outcomes; ternary if
is really for methods that return a value that is to be assigned to a variable.
Guards like those could be put inside the methods, particularly if the methods are called from several places. Then the calling code reads more cleanly.
If by "efficiency" you are thinking about execution speed then don't: making it easy for people to understand what the code is doing is the most important thing most of the time.
We use the coding convention that the curly brackets can be skipped if the condition is placed on a single line to make these common cases less cluttered looking. (Such conventions generally cause great debate though.)
So in your case it would be:
if (postIds.size() > 0) PostProperty(postIds);
if (putIds.size() > 0) PutProperty(putIds);
Your logic has 4 outcomes so a simple ternary if
won't work as that only has 2 outcomes; ternary if
is really for methods that return a value that is to be assigned to a variable.
Guards like those could be put inside the methods, particularly if the methods are called from several places. Then the calling code reads more cleanly.
If by "efficiency" you are thinking about execution speed then don't: making it easy for people to understand what the code is doing is the most important thing most of the time.
edited Nov 10 at 11:17
answered Nov 10 at 11:05
Keith C
93.7k1088199
93.7k1088199
add a comment |
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238938%2fis-there-a-way-a-more-efficient-way-to-write-the-if-statement%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown