Categories
Uncategorized

Wrangling Django

The Backend

Wrestling with the source code of AgBiz Logic has been a phenomenal experience in discovering the structure of a big, foreign codebase. The first few spelunking expeditions into the project were akin to wandering around the New York subway system with a blindfold on. I attempted to trace information as it wound its way through the Django backend, across multiple modules, models, serializers, views, and templates.

Slowly, and after quite a few meetings with our project leader, a method to the madness began to take shape. Django lets developers implement quite robust versions of the classic Model-View-Controller (MVC) design pattern. The framework itself puts a bit of a spin on MVC though, which took me some time to untangle. Rather than using a Django ViewSet as a method of displaying content, the framework uses Templates to serve up information that should be displayed. The ViewSet, instead, becomes a type of controller, wherein the logic for interacting with the application’s database is housed. This confusing relationship often leads to Django’s design paradigm being labeled as Model-View-Template (MVT).

Understanding this structure, and how the project’s AngularJS frontend can make API calls to Django, was key to my recent development progress. Currently, I’m working on a ViewSet function which queries the AgBiz database for agricultural chemicals used in a given farm plan. While it sounds simple enough, the next great hurdle will be to come to grips with the database schema. Then, working with the Models in the project, I will be able to query my way to the desired information.

The chance to dive headfirst into a fully functional, industry software project has been wonderful. Though completely overwhelming at times, the experience will help to further hone my burgeoning development skills.

Categories
Uncategorized

On Clean Code

Writing clean code is important for so many reasons. Developers should adhere to language style guidelines and attitudes of “less is more” wherever possible. Clean code improves the readability, maintainability, and extensibility of our code.

Below, the reader will find two helpful articles for writing awesome code.

  1. PEP-8: A Style Guide for Python Code

PEP-8 is the official style guide for writing code in Python. The standard is kept up-to-date by the Python community and is updated with time as new code conventions come up. The section, A Foolish Consistency is the Hobgoblin of Little Minds, should be especially important for those of us who reside on the more compulsive side of life. These brief guidelines encourage developers to avoid constraining oneself to rigid style rules that do not fit within the code’s local context.

Importantly, the authors of PEP-8 note: “do not break backwards compatibility just to comply with this PEP”. When I reopen an old file, the first thing I do is start refactoring. I’ll spend an exorbitant amount of time updating all the code in the file to my latest style craze simply because I’ve decided that indents of 4 spaces are better than 2.

For the new year, this is a habit that I’m committed to avoiding. I’ll do my best to follow the style hierarchy laid out in PEP-8.

From most to least important:
1. Consistency within a function
2. Consistency within a module
3. Consistency within a project

2. Code Smell by Martin Fowler

This short article opens with a poignant statement from Kent Beck,

“A code smell is a surface indication that usually corresponds to a deeper problem in the system.” 

Fowler uses terribly long functions as an example of a bad code smell. In conjunction with the quote above, a ridiculously long function is probably indicative of developers not keeping code purpose built and on task.

It’s easy to get carried away with a function. Our minds can hold a very organic and non-regimented idea of what a program should be able to accomplish. Computers, on the other hand, have the ability to be extremely granule! Almost every computational process you can think of will be optimized by catering to this modular aspect of the computer. Rather than writing a sprawling mess of a function, growing like a slime mould, developers should strive for precision in the creation of their functions.