Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How has your coding style changed over the years?

by haj (Priest)
on Aug 07, 2022 at 16:16 UTC ( #11145997=note: print w/replies, xml ) Need Help??


in reply to How has your coding style changed over the years?

People are different, aren't they?

Whenever I read old code I've written I find that my coding style seems to change continuously. Somewhat like the movement of an amoeba: I learn something (which isn't necessarily a new feature, but something I had not known), I like it and include it into my coding habits. Other habits die out, mostly because I don't need them for some time.

At one point in time I broke with several habits at once, that was after working through Perl Best Practices. I am using this book's layout conventions until today, and configured my editor to help me doing so. I am not (and never was) religious about it: I can not claim a single *NEVER* nor *ALWAYS* rule for anything I've written.

I guess this has to do with my personal history. I started working for mainframes, making small changes to huge amounts of code written by someone else. Being the beginner in the team, it seemed a good idea to keep the overall style. With Perl, my first job was to fix a broken CGI, again code written by someone else, I decided to keep its style. When I took part in the CPAN Pull Request Challenge, I found very different styles, and I found that all of them work.

I used to start my OO methods with my $self = shift; because I saw others do this. I can not claim I'm doing it *ALWAYS*: I have adopted the habit to use subroutine signatures, and recently I use Object::Pad which predeclares $self. Over the years I have used plain bless-style OO, Moose (which I consider a game changer), and MooseX::Declare (until it became frowned at because of too much magic). Panta rhei.

Habits I haven't changed in the last decades: I use Emacs, and I like the Perl debugger. Occasionally I work to improve these tools.

  • Comment on Re: How has your coding style changed over the years?

Replies are listed 'Best First'.
Re^2: How has your coding style changed over the years?
by LanX (Sage) on Aug 07, 2022 at 17:56 UTC
    > I am not (and never was) religious about it:

    I mostly agree with you, I have to adapt to the style of my client.

    As a funny side effect, we can mostly tell who made a change just by looking at the style.

    Like one of my colleagues prefers qq~...~ for multi-line SQL and I always use heredocs like <<"__SQL__";

    > and configured my editor to help me doing so

    could you please share your settings? I find configuring layout settings quite tedious and frustrating, because the cross-influences make it a try and error game.

    More so if perltidy is also involved and I can see my code flip-flop-ing between different settings. And also configuring perltidy is no fun...

    In the end I stopped using perltidy and configured my emacs to auto-indent the surrounding expression/block with every typed semicolon.

    Like that I get life formatting without needing to think (and it's much faster than shelling out to a restarted perltidy)

    But of course this isn't as feature rich as perltidy, so I'd like to become "compatible" no matter which style.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      could you please share your settings?

      Sure! If you are using the "current" cperl-mode.el from git, you can do:

      • Interactively: <M-x>cperl-set-style<RET>PBP<RET>
      • In your Emacs config: (cperl-set-style "PBP")
      • As customize option, file- or directory variable (which makes sense if you have different projects with different style requirements): Set cperl-file-style to "PBP"

      For older versions of cperl-mode.el, here's the list of settings:

      ("PBP" ;; Perl Best Practices by Damian Conway (cperl-indent-level . 4) (cperl-brace-offset . 0) (cperl-continued-brace-offset . 0) (cperl-label-offset . -2) (cperl-continued-statement-offset . 4) (cperl-close-paren-offset . -4) (cperl-extra-newline-before-brace . nil) (cperl-extra-newline-before-brace-multiline . nil) (cperl-merge-trailing-else . nil) (cperl-indent-parens-as-block . t) (cperl-tab-always-indent . t))
      In the end I stopped using perltidy

      That reminds me of an issue "A command to run perltidy with the input from an emacs buffer" for which a first implementation never made it to Emacs....

        > That reminds me of an issue "A command to run perltidy with the input from an emacs buffer" for which a first implementation never made it to Emacs....

        I'm not sure I understand, isn't that just a shell-command-on-region command after a mark-whole-buffer ?

        > For older versions of cperl-mode.el, here's the list of settings:

        Thanks, especially (cperl-indent-parens-as-block     .  t) fixed a longstanding issue I had.

        It would be nice tho to have a split screen scenario for experimentation.

        Like with code on one side and "live" config on the other side, where every change is temporarily activated and demonstrated in the other screen. When happy you just save the config buffer to a file.

        Hmm ... probably could be done with a macro already ...

        I'm getting this result now when formatting, but neither my emacs nor my cperl-mode is the newest, and probably other configs are jinxed too...

        for my $x (@x) { my %h = ( 'truncate' => { 'desc' => 'shorten a file', 'usage' => [ 'truncate FILEHANDLE,LENGTH', 'truncate EXPR,LENGTH', ] }, ); };

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        update

        ) I'm pretty sure such code was included in PBP.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2023-03-26 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (63 votes). Check out past polls.

    Notices?