Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several minor improvements to chapter 4 - code readability #48

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions episodes/readable.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@

The communities of developers that use each programming language usually follow a conventional approach when naming objects in their code.

It's also a good idea not to use single-letter names such as `x` or `T` because it may not be clear to someone else what these represent. Also, avoid the common pitfall of naming a variable with the same name as an inbuilt function such as `sum()`.
It's also a good idea not to use single-letter names such as `x` or `T` because it may not be clear to someone else what these represent. Also, avoid the common pitfall of naming a variable with the same name as an [in-built](https://docs.python.org/3/library/functions.html) [function](https://stat.ethz.ch/R-manual/R-devel/library/base/html/00Index.html) such as `sum()`.

::: group-tab

Expand All @@ -210,48 +210,54 @@
Classes use capitalised words

```python
class VectorSpace:
class Bird:
pass
```

Variables use lower case with underscores

```python
my_user_name = "Joe"
bird_name = "Blue jay"
```

Constants are named using upper case with underscores

```python
NEGATIVE_ONE = -1
TAXONOMY_ORDER = "Passeriformes"
```

the [Naming Conventions](https://peps.python.org/pep-0008/#naming-conventions) section of <acronym title="Python Enhancement Proposals">PEP</acronym> 8.
For more information about this aspect of coding style, please read the [Naming Conventions](https://peps.python.org/pep-0008/#naming-conventions) section of <acronym title="Python Enhancement Proposals">PEP</acronym> 8.

### R

Classes use capitalised words

```R
setClass("VectorSpace", representation = character())
setClass("Bird", representation = character())
```

Variables use lower case with underscores

```R
my_user_name <- "Joe"
bird_name <- "Blue jay"
```

Constants are named using upper case with underscores

```R
NEGATIVE_ONE <- -1
TAXONOMY_ORDER <- "Passeriformes"
```

For more information about this aspect of coding style, please read the [Tidyverse Style Guide](https://style.tidyverse.org/).

:::

:::: Challenge

Check warning on line 255 in episodes/readable.md

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[unknown div] Challenge

Try writing a simple example of a research-related script using the style conventions discussed above.

::::

Although these rules aren't strict, because your code will still run without error, it does help clarify your intentions by describing what type of variable or object is being referred to. Whatever you do, please try to follow a consistent style with your collaborators to avoid confusion.

## Comments
Expand All @@ -264,20 +270,28 @@

### Python

Python [comments](https://docs.python.org/3/reference/lexical_analysis.html#comments) start with a hash character (`#`) and are ignored when the code runs.

```python
# Add three to my age
age = 21
age =+ 3
age += 3
```

There's more information about [Python operators](https://docs.python.org/3/reference/lexical_analysis.html#operators) such as `+=` in the documentation for that programming language.

### R

Python [comments](https://cran.r-project.org/doc/manuals/R-lang.html#Comments) start with a hash character (`#`) and are ignored when the code runs.

```R
# Add three to my age
age <- 21
age = age + 3
```

There's more information about [R operators](https://cran.r-project.org/doc/manuals/R-lang.html#Operators) in the documentation for that programming language.

:::

It's best practice to use a very concise style when writing code comments. I recommend using active tense verbs.
Expand Down Expand Up @@ -395,7 +409,7 @@

This is just a brief introduction to code annotation. For the keen coder, there are many more features and tools available to make your software easier for other people to understand and use.

It will take some time and effort to write these labels, but it will pay off in the long run to think about variables tapes and make it easier to interpret how the code will behave as it operates. It's best practice to use an integrated development environment (IDE) that will check your type hints and inform you if it detects a problem with your source code.
It will take some time and effort to write these labels, but it will pay off in the long run to think about variables types and make it easier to interpret how the code will behave as it operates. It's best practice to use an integrated development environment (IDE) that will check your type hints and inform you if it detects a problem with your source code.

::::::::::::::::::::::::::::::::::::: keypoints

Expand Down
Loading