commit time author line
da15d9 +11Y
# Programming Conventions
da15d9 +11Y
da15d9 +11Y
Description of file structures, naming conventions, and coding conventions used through the Giterary codebase.
da15d9 +11Y
c31bac +11Y
### Sure, it's MVC... ish.
1a016f +11Y
1a016f +11Y
I suppose, technically, Giteary's codebase follows an MVC-type architecture. That is, if you say that:
1a016f +11Y
1a016f +11Y
* git is the "model"
1a016f +11Y
* "views" are the *render* and *layout* templating mechanism
c31bac +11Y
* "controllers" are a series of functions to route requests based on user parameters
1a016f +11Y
c31bac +11Y
That said, it's certainly not pretty, and violates quite a few things that other codebases implement much better. I try my best to separate display logic from data logic, but honestly, PHP itself is sort of a templating language that got out of hand.
1a016f +11Y
1a016f +11Y
### File Structure
1a016f +11Y
1a016f +11Y
/*.php Files for accepting parameters, routing to functions,
1a016f +11Y
and providing the right bootstrapping "require"
1a016f +11Y
statements to handle requests.
1a016f +11Y
1a016f +11Y
/css/ CSS
1a016f +11Y
1a016f +11Y
/js/ Javascript
1a016f +11Y
1a016f +11Y
/include/*.php Function definitions, named according to their site function.
1a016f +11Y
/include/config.php Configuration bootstrapping
1a016f +11Y
/include/util.php Utility methods used application-wide
1a016f +11Y
1a016f +11Y
/include/git.php Functions for interacting directly with git
1a016f +11Y
/include/git_html.php Functions for translating git output to HTML
1a016f +11Y
/include/display.php Functions for rendering and translating documents from Markdown, CSV,
1a016f +11Y
text, or any other formats.
1a016f +11Y
1a016f +11Y
/include/config/*.php Configuration PHP and config "libraries"
1a016f +11Y
1a016f +11Y
/theme/ Storage for "theme" renderables for the templating mechanism
1a016f +11Y
/theme/default/ Storage for default theme
1a016f +11Y
/theme/*/renderable/ Storage for "renderable" PHP pages for the templating mechanism
1a016f +11Y
/theme/*/renderable/layout Storage for "renderable" PHP pages for the templating mechanism
1a016f +11Y
1a016f +11Y
b7a031 +11Y
### Function naming
1a016f +11Y
b7a031 +11Y
* **git_* functions**
43fe60 +11Y
b7a031 +11Y
Functions that interact directly with git. Usually the suffix will correspond directly to the git "verb" being used.
43fe60 +11Y
b7a031 +11Y
* **gen_* functions**
43fe60 +11Y
b7a031 +11Y
Functions that "generate" HTML output, but also have the possibility to serve as "shim" functions to separate functionality like "Am I allowed to perform this function?" from the actual execution of the function.
43fe60 +11Y
b7a031 +11Y
Additionally, serves to provide a "staging area" for handling default variables or configured variables before passing off to the "executing" functions.
b7a031 +11Y
b7a031 +11Y
* **\_gen_* functions**
b7a031 +11Y
b7a031 +11Y
Functions prefixed with *_gen* tend to be the "executing" functions, counterparts to their parameterizing and authenticating *gen* functions. These tend to have more or less options than their *gen* counterparts, being the "advanced" interface to a particular feature, or being a function that serves more than one set of *gen* functional areas.
da15d9 +11Y
97bcd4 +11Y
### Keep Things Simple Where They Should Be Simple
94b571 +11Y
94b571 +11Y
If at all possible, it is recommended that you keep "logic" code out of the root \*.php files, instead delegating display logic to underlying functions. For instance, the PHP file to display *search.php* is
94b571 +11Y
94b571 +11Y
94b571 +11Y
require_once( dirname( __FILE__ ) . '/include/header.php');
94b571 +11Y
require_once( dirname( __FILE__ ) . '/include/footer.php');
94b571 +11Y
require_once( dirname( __FILE__ ) . '/include/util.php');
94b571 +11Y
require_once( dirname( __FILE__ ) . '/include/git_html.php');
94b571 +11Y
require_once( dirname( __FILE__ ) . '/include/edit.php');
94b571 +11Y
94b571 +11Y
94b571 +11Y
$term = substr( $_GET['term'], 0, 100 );
94b571 +11Y
94b571 +11Y
echo layout(
94b571 +11Y
array(
94b571 +11Y
'header' => gen_header( "Search" ),
94b571 +11Y
'content' => gen_search( $term )
94b571 +11Y
)
94b571 +11Y
);
94b571 +11Y
97bcd4 +11Y
Its only concern is accepting the request, search terms, and passing them to the *gen_search()* function. It does not call any of the *git_* functions, instead relying on the *gen_* HTML generation to indicate display, success, or errors.
94b571 +11Y
94b571 +11Y
94b571 +11Y
### Architectural Concerns
94b571 +11Y
da15d9 +11Y
* **Why don't you use library X to do Y? Wouldn't that be easier?**
da15d9 +11Y
6de7a8 +11Y
Or: *Why did you build your own templating system, git interface, user permissions, and user auth systems?*
6de7a8 +11Y
c769a4 +11Y
At least I used git, doesn't that count? :)
6de7a8 +11Y
467229 +11Y
It's hard to strike a good distance between a piece of code doing exactly what you want and the amount of work it takes to get you there. Some problems are better left to those with doctorates in computer science or mathematics, or those that get paid handsomely to solve such problems for their employers and contribute their efforts back to the open source community.
6de7a8 +11Y
467229 +11Y
That being said, it's rare that I find a programming library that solves a particular set of problems in ways that:
6de7a8 +11Y
6de7a8 +11Y
* Fit with my level of paranoia
6de7a8 +11Y
* Fit with my level of vague technological elitism
6de7a8 +11Y
* "Get out of my way" if I want to do something that might be considered *unwise*.
6de7a8 +11Y
6de7a8 +11Y
I don't pretend to be a great programmer, and I'm usually willing to defer to the expertise of others. However, sometimes you have to do *bad things* © for good reasons. Using libraries built on the design decisions of others means that eventually your requirements draw outside the lines of the intended use of a library. I appreciate the risks. But I don't like to be constrained by them.
6de7a8 +11Y
6de7a8 +11Y
Also, package management. There are great systems out there that manage packages and their dependencies. I like to build things that are simple enough that they don't require package management, or, can exist without the need for package management.