Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: What is code readability?

by BrowserUk (Pope)
on Jan 03, 2007 at 20:28 UTC ( #592828=note: print w/ replies, xml ) Need Help??


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.


Comment on Re^2: What is code readability?
Select or Download Code
Re^3: What is code readability?
by itub (Priest) on Jan 04, 2007 at 01:54 UTC
    The last one, definitely. :-)
Re^3: What is code readability?
by parv (Priest) 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 )
      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

        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.)

      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.
        Even more vertical.

        And you're always going to get all those args in the right order? I know I wouldn't.

        # 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! :-)

        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 }

        My take on it is pretty close, I just see OK to open more than one paren in a line (and hence close them all in the same line):

        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 }
      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 );
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://592828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-08-28 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (257 votes), past polls