Skip to content

Commit

Permalink
Fix whitespace in code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jrha committed Sep 6, 2024
1 parent a293490 commit dbea7c4
Showing 1 changed file with 70 additions and 66 deletions.
136 changes: 70 additions & 66 deletions _development/coding_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,53 +163,56 @@ you have to work around some broken API, or some corner case. In such case, plea
Bad examples:

```perl
############################################################
#
# Increment by 1
#
############################################################
$i++;
############################################################
#
# Increment by 1
#
############################################################
$i++;
```

```perl
sub do_something
{
my @args = @_;
sub do_something
{
my @args = @_;

# I do foo and bar here
...
...
...
# And now, let's do bar again, but with a small difference
...
...
}
# I do foo and bar here
...
...
...

# And now, let's do bar again, but with a small difference
...
...
}
```

These are best done like this:

```perl
$i++;
$i++;
```

```perl
# Performs task foo...
sub foo {...}
# Performs task bar...
sub bar {...}
# Performs task baz, which is quite similar to bar
sub baz {...}

# Performs a set of tasks, returning blah blah with arguments
# $arg1:
# $arg2:
sub do_something
{
my @args = @_;
foo (@args);
bar (@args);
baz (@args);
}
# Performs task foo...
sub foo {...}

# Performs task bar...
sub bar {...}

# Performs task baz, which is quite similar to bar
sub baz {...}

# Performs a set of tasks, returning blah blah with arguments
# $arg1:
# $arg2:
sub do_something
{
my @args = @_;
foo (@args);
bar (@args);
baz (@args);
}
```

### Don't use `vars`
Expand All @@ -220,14 +223,14 @@ use the `our` declaration for package-wide variables:
Good:

```perl
our @EXPORT = ...;
our @EXPORT = ...;
```

Bad:

```perl
use vars qw (@EXPORT);
@EXPORT = ...;
use vars qw (@EXPORT);
@EXPORT = ...;
```

### Curly bracket position
Expand All @@ -236,29 +239,29 @@ Follow Kernighan-Ritchie's convention: open curly brackets on the same line as t
they belong to and close them on a line for their own, excepting when it's an else or a do-while block:

```perl
if ($foo) {
...
}
if ($foo) {
...
}
```

```perl
while ($bar) {
...
}
while ($bar) {
...
}
```

```perl
if ($foo) {
...
} else {
...
}
if ($foo) {
...
} else {
...
}
```

```perl
do {
...
} while ($bar);
do {
...
} while ($bar);
```


Expand All @@ -268,10 +271,10 @@ The only exception to this are the curly brackets that open a function. They sho
different line, and have nothing else on the same line:

```perl
sub foo
{
my @args = @_;
}
sub foo
{
my @args = @_;
}
```

The reason for this is that decent editors (f.i, Emacs) are aware that such curly braces mean
Expand All @@ -288,13 +291,13 @@ Use them a lot. When in doubt, use them. Especially:
The reason is that:

```perl
foo();
foo();
```

Is easier to understand than:

```perl
foo;
foo;
```

### Prefer methods over plain functions
Expand Down Expand Up @@ -330,18 +333,19 @@ As the `CAF::Process` module logs the command line that you are executing at ver
you don't need to handle the logging yourself. Do not do:

```perl
$self->debug (5, "Going to run ls -l");
system ("ls", "-l");
$self->debug (5, "Going to run ls -l");
system ("ls", "-l");
```

Instead, do:

```perl
# Or any reporter object.
my $proc = CAF::Process->new (["ls", "-l"], log => $self);
$proc->run();
# One-line version:
CAF::Process->new (["ls", "-l"], log => $self)->run();
# Or any reporter object.
my $proc = CAF::Process->new (["ls", "-l"], log => $self);
$proc->run();

# One-line version:
CAF::Process->new (["ls", "-l"], log => $self)->run();
```

The log option is any `CAF::Logger` object, for instance the component you are writing (`$self`).
Expand All @@ -359,7 +363,7 @@ Writing to files is not as simple as one could think: there are risks that you s
instance, the following code seems harmless but is an example of what shouldn't be done:

```perl
open (FH, ">/tmp/foo");
open (FH, ">/tmp/foo");
```

If `/tmp/foo` already exists and is a symbolic link to `/etc/shadow`, you just lost all accounts on your system.
Expand Down

0 comments on commit dbea7c4

Please sign in to comment.