Mangini
Active Coder
Like a lot of others here, coding has been a part of my life for a long time. Like any other skill one masters, a deep connection is developed within your mind and body to the craft.
I don't know how crazy this sounds but there are certain file names, functions, and other blocks of code that I can look at that I wrote years ago and suddenly be transported back in time to the moment I wrote them.
Suddenly I can remember where I was, a smell around me, and even the people who were present in my life both in that moment or just in general. Coding really has become a spiritual experience for me and I will fight tooth and nail against anybody who doesn't see the art in what we do.
The kind of connections I am describing here are ingrained into our brains through a series of "Aha!" moments; the kinds of lessons that we learn once and will know forever because of how monumental of a discovery they were to future creations.
In this thread let's share our biggest moments of clarity and concepts learned that have been invaluable to our work. There are three concepts I always come back to that completely changed the way I write code:
I don't know how crazy this sounds but there are certain file names, functions, and other blocks of code that I can look at that I wrote years ago and suddenly be transported back in time to the moment I wrote them.
Suddenly I can remember where I was, a smell around me, and even the people who were present in my life both in that moment or just in general. Coding really has become a spiritual experience for me and I will fight tooth and nail against anybody who doesn't see the art in what we do.
The kind of connections I am describing here are ingrained into our brains through a series of "Aha!" moments; the kinds of lessons that we learn once and will know forever because of how monumental of a discovery they were to future creations.
In this thread let's share our biggest moments of clarity and concepts learned that have been invaluable to our work. There are three concepts I always come back to that completely changed the way I write code:
- Passing data through functions
This one took me longer to understand than I'd care to admit, but learning how to pass data through functions quickly reoriented my mind to write more repeatable and organized code. At the most basic level in PHP, this concept looks like this:
PHP:function my_name( $name ) { return 'My name is ' . $name; } echo my_name( 'Alex' );
- Foreach and building data arrays
The key concept here is to learn how to think in terms of data and not just single elements. Building and traversing through arrays of data allows you to accomplish far more by writing less code and can make your code far easier to plug into. For example, do you think it's easier for a client to edit a list of text elements by scrolling through an HTML document, or would a single file with an array list be easier for them to plug into? Furthermore, if you can isolate values into a simple array list, what else can you do with it? The possibilities are endless when you think in the mindset of data.
PHP:<?php $data = array( 'section_1' => array( 'title' => 'My first headline', 'text' => 'Some description text', 'url' => 'https://domain.com' ), 'section_2' => array( 'title' => 'Another headline', 'text' => 'Even more description text', 'url' => 'https://another-domain.com' ) ); foreach ( $data as $section_id => $fields ) : ?> <div id="<?php echo $section_id; ?>"> <h2><?php echo $fields['title']; ?></h2> <p><?php echo $fields['text']; ?></p> <p><a href="<?php echo $fields['url']; ?>">Visit website</a></p> </div> <?php endforeach; ?>
- Class extension and OOP
Maybe a more common one, but I was skeptical about how useful writing classes were. Sure, I could come up with much simpler method (function) names now that I have a unique Class wrapper, but just that alone didn't seem worth all of the extra indents and more long-winded ways to access those methods outside of the class. But then I learned how to extend classes and use an entirely premade class environment in a brand new class without writing any extra code.
PHP:// Create a main class with master methods class MainClass { public function name( $name ) { return 'My name is ' . $name; } } // Create a subclass that uses the name() method from MainClass class SubClass extends MainClass { public function someMethod() { return $this->name( 'Alex' ); } }