<commit_before>---
layout: post
title: Encapsulating Functionality
permalink: /encapsulating-functionality/
tags: programming coding encapsulation abstraction functions information-hiding
---

Computer science authors and professors often discuss the concepts of <a
target="_blank"
href="https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)">encapsulation</a>,
<a target="_blank"
href="https://en.wikipedia.org/wiki/Abstraction_(software_engineering)">abstraction</a>,
and <a target="_blank"
href="https://en.wikipedia.org/wiki/Information_hiding">information hiding</a>.
These concepts are fundamental to programming, and they are required in order to
manage complex systems. By encapsulating information into separate components,
programmers can reason about those subsystems alone. Instead of understanding
and mentally grasping the whole system at once, coders can partition systems
into coherent sections. Unfortunately, encapsulation, abstraction, and
information hiding are usually discussed with respect to objects and
object-oriented programming (as you can see in the links above). But
encapsulation is equally important in the lower-level tools of functions and
methods.

Fundamentally, functions are designed to encapsulate related functionality.
Because the functionality and information are abstracted away in a function, the
programmer can avoid thinking about those lower-level details when they are
reasoning about the calling code. If the function is well designed, coders can
also more easily modify it because they will not need to think about the outside
world when doing so.

Here is an oversimplified example of a function with poor encapsulation:

```
// There are three configuration files:
// custom.config.xml, default.config.xml, and core.config.xml
public List<File> getConfigFiles() {
    ArrayList<File> configFiles = new ArrayList<File>();
    configFiles.add(new File("default.config.xml"));
    configFiles.add(new File("core.config.xml"));
    return Collections.unmodifiableList(configFiles);
}
```

Because the `getConfigFiles()` method requires the developer to know which of
the three configuration files it returns, the method has poor encapsulation.
When the developer wants to use this method, they must think about the
internals. Then they must obtain the **`custom.config.xml`** separately.
Therefore the method does not properly encapsulate the functionality of getting
the configuration files.
<commit_msg>Edited "Encapsulating Functionality"<commit_after>---
layout: post
title: Encapsulating Functionality
permalink: /encapsulating-functionality/
tags: programming coding encapsulation abstraction functions information-hiding
---

Computer science authors and professors often discuss the concepts of <a
target="_blank"
href="https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)">encapsulation</a>,
<a target="_blank"
href="https://en.wikipedia.org/wiki/Abstraction_(software_engineering)">abstraction</a>,
and <a target="_blank"
href="https://en.wikipedia.org/wiki/Information_hiding">information hiding</a>.
These concepts are fundamental to programming, and they are required in order to
manage complex systems. By encapsulating information into separate components,
programmers can reason about those subsystems alone. Instead of understanding
and mentally grasping the whole system at once, coders can partition systems
into coherent sections. Unfortunately, encapsulation, abstraction, and
information hiding are usually discussed with respect to objects and
object-oriented programming (as you can see in the links above). But
encapsulation is equally important in the lower-level tools of functions and
methods.

Fundamentally, functions are designed to encapsulate related functionality.
Because the functionality and information are abstracted away in a function, the
programmer can avoid thinking about those lower-level details when they are
reasoning about the calling code. If the function is well designed, coders can
also modify it more easily because they will not need to think about any
external code when doing so.

Here is an oversimplified example of a function with poor encapsulation:

```
// There are three configuration files:
// custom.config.xml, default.config.xml, and core.config.xml
public List<File> getConfigFiles() {
    ArrayList<File> configFiles = new ArrayList<File>();
    configFiles.add(new File("default.config.xml"));
    configFiles.add(new File("core.config.xml"));
    return Collections.unmodifiableList(configFiles);
}
```

Because the `getConfigFiles()` method requires the developer to know which of
the three configuration files it returns, the method has poor encapsulation.
When the developer wants to use this method, they must think about the
internals. Then they must obtain the **`custom.config.xml`** separately.
Therefore the method does not properly encapsulate the functionality of getting
the configuration files.

{'fraction_non_alphanumeric': 0.07144298688193744, 'fraction_numerical': 0.0, 'mean_word_length': 5.706359945872801, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4816219', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1146, 'n_words': 548}