in reply to Re: What is code readability? in thread What is code readability?
It was pointed out to me by /msg that in my "How many arguments?" example above, I made two changes between the two examples, and it was postulated that only one of these, the additional whitespace, contributed to whatever extra clarity was evident.
By way of further investigation, in which of the following is the number of parameters clearest to your eyes?
- someFunction( someVariable, someOtherVariable, andYetAnotherVariable, andOneMoreFor, luck )
- some_function (some_variable,some_other_variable,and_yet_another_variable,and_one_more_for,luck)
- someFunction (someVariable,someOtherVariable,andYetAnotherVariable,andOneMoreFor,luck)
- some_function( some_variable, some_other_variable, and_yet_another_variable, and_one_more_for, luck )
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: What is code readability?
by itub (Priest) on Jan 04, 2007 at 01:54 UTC
|
The last one, definitely. :-) | [reply] |
Re^3: What is code readability?
by parv (Parson) on Jan 04, 2007 at 03:46 UTC
|
1st & 4th, my favourite one BTW, are much easier on the eyes.
There is one more variation ...
some_function
(
some_variable , some_other_variable , and_yet_another_variable
, and_one_more_for , luck
)
| [reply] [d/l] |
|
Ug :) I really hate that. I see no benefit in spaces both sides of the commas, or breaking the open paren away from the function name. And if you have to break the params across lines, at least balance their lengths :)
some_function(
some_variable, some_other_variable,
and_yet_another_variable, and_one_more_for, luck
);
That's not so bad for simple (void) calls as, but when you're retrieving data and checking you get something like
if( some_return_value = some_function(
some_variable, some_other_variable,
and_yet_another_variable, and_one_more_for,
luck ) ) {
// do some stuff here with some_return_value
} else {
// report or otherwise handle the error
}
It's just a mess.The best I've come up with for this is
if(
some_return_value = some_function(
some_variable, some_other_variable,
and_yet_another_variable, and_one_more_for, luck
)
) {
// do some stuff here with some_return_value
}
else {
// report or otherwise handle the error
}
Which ain't great, but is better than most alternatives to my eyes.
And much better still is
if( someRv = fSome( some, oSome, AYAnother, OneMoreFor, luck )) {
// do some stuff here with someRv
}
else {
// report or otherwise handle the error
}
With the point being that whilst ths abbreviated variable names don't immediately make much sense, by the time a programmer has got familiar enough with the code to consider making changes, they will.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
# round em up...
my $args = { # no need to worry about the order
arg1 => $some_variable,
arg2 => $some_other_variable,
arg3 => $and_yet_another_variable,
arg4 => $and_one_more_for,
arg5 => $luck,
arg6 => some_other_function(),
arg7 => $maybe_a_variable or $default,
};
# ...and herd em in
my $some_return_value = some_function($args);
if($some_return_value) {
# do some stuff here with some_return_value
# and if there are more than a few lines
# I'd consider another sub
}
else { # and don't cuddle your else
# report or otherwise handle the error
# in another sub :-)
}
sub some_function {
my ($args) = @_;
# no need to worry about the order here either
}
I should point out I'm presently having terrible trouble debugging an app (that I'm writing). It gets more vertical by the day! :-)
| [reply] [d/l] |
|
|
if (some_return_value = some_function (
some_variable, some_other_variable,
and_yet_another_variable, and_one_more_for, luck
)) {
// do some stuff here with some_return_value
} else {
// report or otherwise handle the error
}
| [reply] [d/l] [select] |
|
|
There is one more variation
Only one?
some_function
(
some_variable ,
some_other_variable ,
and_yet_another_variable ,
and_one_more_for ,
luck ,
)
some_function
(
some_variable
,
some_other_variable
,
and_yet_another_variable
,
and_one_more_for
,
luck
)
A word spoken in Mind will reach its own level, in the objective world, by its own weight
| [reply] [d/l] |
|
Of course; I did not claim the posted version as "'only|sole|last' one variation" (but "a variation").
Problem with your first variation -- for that matter, any other code in which things are aligned along the columns -- is that aligning commas for short varaibles like "luck" is too much effort. (Second one is just ridiculous overkill.)
(I was hoping not to contribute anything (as I do not have much to say about brain d foy's points) but I was suckered
in anyway.)
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
|
some_function(some_variable,
some_other_variable,
and_yet_another_variable,
and_one_more_for,
luck);
...getting better...
some_function( filename => $some_variable,
address => $some_other_variable,
amount => $and_yet_another_variable,
phone_num => $and_one_more_for,
diameter => $luck );
| [reply] [d/l] [select] |
|
And that bites when you want to cut the first or last, or paste another first or last, argument. I prefer the args on separate lines, and indenting a normal (4 or whatever the standard) number of spaces.
| [reply] |
|
|