Highlighted
Contributor.
Contributor.
778 views

.Net Code

Jump to solution

 I was wondering if there is any advantage over one of these .net methods versus the other.  In the second one, I modeled it after a C# method using .net Cobol syntax of course.  They both function the same, but wanted an opinion if one was better than the other in regards to best practices.

 

First Method.

method-id IsStrongPassword public.
local-storage section.
01 cond-value condition-value.
procedure division using by value tro as type TO_NewUser, t as type TextBox returning strongPassword as type Boolean.
set cond-value = type Regex::IsMatch(tro::Password, type HospitalManagementSoftware.Properties.Settings::Default::pword).
evaluate cond-value
when false
invoke type MessageBox::Show("Please enter a stronger password.", #Title)
invoke t::SelectAll()
when other
set strongPassword to true
end-evaluate
end method.

 

Second Method.


method-id IsStrongPassword(tro as type TO_NewUser, t as type TextBox) returning strongPassword as type Boolean public.
local-storage section.
01 cond-value condition-value value type Regex::IsMatch(tro::Password, type HospitalManagementSoftware.Properties.Settings::Default::pword).
evaluate cond-value
when false
invoke type MessageBox::Show("Please enter a stronger password.", #Title)
invoke t::SelectAll()
when other
set strongPassword to true
end-evaluate
end method.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: .Net Code

Jump to solution
COBOL's EVALUATE verb is very flexible, and works well as as the equivalent both of C#'s switch statement and if ... else if ... constructs. However I would agree with Michael that if there is only one condition (particularly where that condition is just testing the value of a simple boolean variable), using a simple IF statement is better.

Not sure where this comment is in the documentation, but probably the intention is to warn against constructs like:
if n = 1
...
else if n = 2
...
else if n = 3
end-if
This looks equivalent to C#
if (n == 1)
{
}
else if (n == 2)
{
}
else if (n == 3)
{
}
but in fact the COBOL leaves two if statements unterminated and the program flow is unlikely to be as expected.
To express this correctly using if, you would need to write:
if n = 1
...
else
if n = 2
....
else
if n = 3
....
end-if
end-if
end-if
Clearly EVALUATE is a much better choice in this case.

View solution in original post

0 Likes
3 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: .Net Code

Jump to solution

"Best practices" is subjective, of course. In this case, the generated IL will be essentially equivalent, as far as I can see. As for the source code, my recommendation would be:

  • Use the newer syntax consistently, except when you have code that needs to be compiled both as native and managed code (usually only an issue for some copybooks).
  • Avoid extraneous period (aka full stop) characters. Usually they either have no effect (the compiler knows where sentences terminate) or they should be replaced with the ANSI scope terminators (end-if, etc).
  • The evaluate verb here is overkill.

So I'd write your method as:

method-id IsStrongPassword(tro as type TO_NewUser, t as type TextBox) returning strongPassword as condition-value public.
set strongPassword to type Regex::IsMatch(tro::Password, type HospitalManagementSoftware.Properties.Settings::Default::pword)
if not strongPasword
       invoke type MessageBox::Show("Please enter a stronger password.", #Title)
       invoke t::SelectAll()
    end-if
end method IsStrongPassword.
 
0 Likes
Highlighted
Contributor.
Contributor.

RE: .Net Code

Jump to solution
Thanks for the info. However, I was reading in the visual cobol documentation, that the evaluate clause is preferred in Cobol rather than 'if' and 'if/else'. To me it makes code easier to read when using evaluate rather than having if statement and nested if/else statements. Thoughts from anyone on this?
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: .Net Code

Jump to solution
COBOL's EVALUATE verb is very flexible, and works well as as the equivalent both of C#'s switch statement and if ... else if ... constructs. However I would agree with Michael that if there is only one condition (particularly where that condition is just testing the value of a simple boolean variable), using a simple IF statement is better.

Not sure where this comment is in the documentation, but probably the intention is to warn against constructs like:
if n = 1
...
else if n = 2
...
else if n = 3
end-if
This looks equivalent to C#
if (n == 1)
{
}
else if (n == 2)
{
}
else if (n == 3)
{
}
but in fact the COBOL leaves two if statements unterminated and the program flow is unlikely to be as expected.
To express this correctly using if, you would need to write:
if n = 1
...
else
if n = 2
....
else
if n = 3
....
end-if
end-if
end-if
Clearly EVALUATE is a much better choice in this case.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.