Translate

Tuesday, May 19, 2015

Keep It Simple

This post is based on my experiences with a few functions that I wrote that started off working well, and handling the problem of repetitive code with dignity and grace. As time went on, and "features" were added to it, it became more trouble than it was worth. 

This is the lens through which I am going to write this post, however, it can apply to just about anything within software development where someone has a bright idea to make things easier, and takes it a bit too far. I am not attempting to demote innovation. I am simply giving advice for those who are looking to innovate, so that things don't get over-complicated. 

Without further ado, please enjoy. And don't forget to leave comments or share this with someone you know who may have found themselves in a similar situation.


How many times have you realized that you are rewriting similar database queries, or pasting similar code over and over, and come up with a brilliant idea:


"I'm going to write a function to do this for me! I will pass a few parameters, and it will make my life easier!" 

So you do just that! It works fine for a while, then you run into a query that requires a WHERE statement. So you pop in a few lines of logic and an optional parameter so that you can pass in WHERE options if need be. That works perfectly for a while, although it proves to be a little bit confusing, because you keep forgetting the order of parameters, and the optional one has to be last. But you get used to it.

Then you have a query that requires multiple checks. You need an 'AND' in your query.

That is all fine and good. You add another parameter to the function after the "where" parameter which allows you to add even more checks. Then you get the genius idea to make the "where" parameter an array instead of a string and loop through it, using "WHERE" for the first loop, and "AND" for each subsequent loop. It gets a little bit complex, and you fiddle with it for way longer than you planned to, until finally you get it working.

Now, you have to adjust to this new usage. It takes a little bit, and now when something goes wrong, instead of telling you where the error is, it now just says "line 98 in config.php" for any error relating to the entire function. That's not very useful. That is simply your call to the function. So it is a problem with the way the function was written, or is it a problem with your query?

So you end up using echo($query);die; in way more of your code than you would like. This goes on for a while, until you finally get used to it, and decide you can deal with the misinformed errors. After all, the error would lie in the execution of the query either way. Right? Either way, you shrug it off and move on. You work like this for a while, then get the genius idea to add the option to use prepared statements, because you have some user input to accept and want to be safe. Good for you! Being safe is a good plan!

So you find yourself jamming just one more feature into your messy, incomprehensible function, when it hits you...

Writing out the query, preparation, and execution of each database access would be SIMPLER than this rigmarole. 

So where does this leave you? Well, if you want to look at it negatively, you wasted your time.

However, you have now learned why you were the only one writing this kind of weird function. You have learned why people use open-source libraries to handle this kind of stuff in a "simpler" manner. You have learned that sometimes the shortcut isn't always going to get you there faster.

It's important to remember that libraries are supposed to make your life easier.  If you find yourself stumbling over your own feet, it might be time to re-think things a bit.

In this case Keep It Simple, Stupid is better advice than Don't Repeat Yourself.

Tuesday, May 12, 2015

Why You Should Avoid Dynamic Array Keys

Be extremely careful when assigning array keys dynamically, as you can get data overwrites if two items happen to have the same dynamically assigned value.

While this should probably go without saying, I found myself in this situation recently. I spent a good half hour scratching my head, trying to figure out why the value was correct, but the name was completely wrong. Finally, I realized that two items shared the same value, so the first was being overwritten. I felt I should document the problem on here so that other developers may hopefully keep it in mind when developing dynamic arrays.


Take for example the following array in PHP:

$list = array(
  $item1 => 'Breakfast',
  $item2 => 'Lunch',
  $item3 => 'Dinner'
);


All is good assuming you eat different things for each meal. For example, if you have the following variables:

$item1 = "Cereal";
$item2 = "Salad";
$item3 = "Steak";

Your array will look as such:


array (size=3)
  'Cereal' => string 'Breakfast' (length=9)
  'Salad' => string 'Lunch' (length=5)
  'Steak' => string 'Dinner' (length=6)


But what happens if you are on a budget that month, and have cereal for dinner, too?

$item1 = "Cereal";
$item2 = "Salad";
$item3 = "Cereal";


You end up with:


array (size=2)
  'Cereal' => string 'Dinner' (length=6)
  'Salad' => string 'Lunch' (length=5)


Uh oh. That's no good. 


It's really best to not use dynamic keys, as much as possible. This array can easily be changed to the following without causing any significant changes in the way your code would need to access it.

$list = array(
  'Breakfast' => $item1,
  'Lunch' => $item2,
  'Dinner' => $item3
);

Monday, October 6, 2014

Beginning Laravel PHP Framework

Hey, folks!

It's been quite a while since my last post, but I am planning on getting back on here regularly to post more regular content. I have transferred this blog to my main email address, so there will be one less barrier to entry, hopefully.

Today, I would like to briefly touch on a PHP Framework called Laravel. Now, anyone who has been even kind of active in the PHP community has likely heard of this one, as it is perhaps one of the most popular frameworks for PHP at the present moment.

I have been looking into this framework, and learning a little bit about it in preparation for development of a side project of mine. First of all -- why Laravel? Why not CodeIgniter, or Yii, or one of the many other frameworks out there? Well, I actually did check out CodeIgniter a little bit the other day before coming across Laravel. It seemed very versatile and useful, however, it had a couple of issues which made it difficult for me to get started, and upon a little bit of searching, it looks like Laravel may be the more promising choice for potential job availability in the future.

I have begun reading through a couple learning resources regarding Laravel, and am really liking what I have seen thus far. From my brief flirtation with CodeIgniter, it seems as though routes are a common framework feature, however, I really like how clean and simple it really is to implement in Laravel. To implement a simple route, you need very little code:

Route::get('/about', function()  // When the user visits the about directory of your website...
{
 return View::make('about'); // Serve them the about page.
});


I am also very impressed with the ease in which you can pull in assets. Recently I have been using the method of declaring a BASE_URL constant:
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT'] . '/SomeDevelopmentFolder/');

And declaring it contstantly:

<a href="<?php echo BASE_URL;?>folder/someresource.php">Link</a>
<a href="<?php echo BASE_URL;?>folder/anotherresource.php">Link</a>
<a href="<?php echo BASE_URL;?>folder/evenonemore.php">Link</a>

 While I enjoy it's benefits, it gets a bit old. Laravel allows you to simply use:

<a href="{{ asset('images/image1.jpg') }}">Link</a>
<a href="{{ asset('images/image2.jpg') }}">Link</a>
<a href="{{ asset('images/image3.jpg') }}">Link</a>

While this may seem like a slight difference in terms of length, it sure is quite different in terms of finger movements! I am sure that I have only scratched the surface of the value of this feature of Laravel, and apologize if I have not done it justice!

On top of it all, it also comes with a very useful templating language called Blade. My experience with Blade and Laravel in general so far have been very pleasant.

I am roughly half way through a wonderful book by Jack Vo called Learning Laravel: The Easiest Way (http://learninglaravel.net/), which easily explains many of the concepts of Laravel. I actually highly recommend it, though I am not very far into it yet. It will also give you a primer on Bootstrap and a couple of other web development tools if you have not yet dabbled with them.

I plan to post again about Laravel when I have a more complete knowledge of the features and uses that it has, however, right now I do not know much more than these basic things.

Please, please, please feel free to correct me or call me on anything if it doesn't sound right, as once again I am a newbie at this big bad Laravel business. These are all of my observations while learning what I have learned, and some of them are bound to be formed in part by my relative ignorance of the true capabilities of this framework.

Again, I thank all of you who are reading this, and plan on keeping it more up to date. I plan to post at least once a week, but I want to shoot for twice. We shall see how it goes, as things have been pretty busy lately, but I will fit it in where I can, as long as I feel like I have something of relative value to post.

Thursday, June 19, 2014

SASS and Compass

You may have heard the terms Sass or Compass being thrown around recently. Or perhaps you haven't. But regardless of if you've heard about it or not, Sass is something that any Front End Web Developer should have tucked in their tool belt. Basically, Sass is a simpler way of writing CSS. You might ask yourself: "Why would I want to write Sass when I am already proficient in CSS?". Well the answer to that varies depending on who you are.

For me, the feature that really draws me into Sass is cross-browser support. Have you ever wanted to use, for example, a linear gradient, but end up having to find a gradient generator, or check CanIUse(caniuse.com) or similar sites so that you know which vendor prefixes to use? That might be fun and exciting the first dozen or so times, but eventually it gets a bit old. One nice thing about Sass is that you can just type:

.some-class {
  @include background(linear-gradient(top left, #fff, #eee 50%, #000 100%));
}

into your CSS, and it will automagically spit out:

.some-class {
  background: -webkit-gradient(linear, 0% 0%, 100% 100%, color-stop(0%, #ffffff), color-stop(50%, #eeeeee), color-stop(100%, #000000));
  background: -webkit-linear-gradient(top left, #ffffff, #eeeeee 50%, #000000 100%);
  background: -moz-linear-gradient(top left, #ffffff, #eeeeee 50%, #000000 100%);
  background: -o-linear-gradient(top left, #ffffff, #eeeeee 50%, #000000 100%);
  background: linear-gradient(top left, #ffffff, #eeeeee 50%, #000000 100%);
}

There is a bit more to it than that, as you need to get Compass, which is basically the Sass compiler and then compile your Sass. But it really is a pleasurable workflow once you have it all set up.

Some other things that Sass can do is have variables, so you don't have to jump into Photoshop and grab your logo's color every single time you want to use it in your stylesheet, resulting in tons of different colors being declared throughout the file. Sass also greatly simplifies nesting. For example:

#someId {
  @include transition(all 0.3s ease);
  .someClass {
     .someDeeperClass {
       @include background-clip(rect(0px,50px,200px,0px));
       @include rotate(-25deg);
     }
  }
}

Would result in:

#someId {
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
#someId .someClass .someDeeperClass {
  -webkit-background-clip: rect(0px, 50px, 200px, 0px);
  -moz-background-clip: rect(0px, 50px, 200px, 0px);
  background-clip: rect(0px, 50px, 200px, 0px);
  -webkit-transform: rotate(-25deg);
  -moz-transform: rotate(-25deg);
  -ms-transform: rotate(-25deg);
  -o-transform: rotate(-25deg);
  transform: rotate(-25deg);
}

It really is very simple once you have messed around with it for an hour or so, and the code that comes out of it feels refreshingly clean!

If any of this Sass stuff sounds like fun, I suggest checking out the following article by Adam Stacoviak on The Sass Way, and checking out Chris Coyier's Screencast on the topic.

Friday, June 6, 2014

Learning Resources

Hello again, everybody! It's been a while, but I hope this post will make up for the hiatus. Work has been busy, and the Summer semester just started back up after a one-week break. I've been around, though!

I felt that it would be beneficial to everyone if I could share some resources for developers like myself. I've begun collecting quite a few resources which I feel would be useful to folks who are trying to keep current in this fast-moving profession that we have found ourselves attracted to.

They will be organized into categories, and I will have the free services up top, and the paid ones down below the free ones. I will move this to its own page soon and keep it updated there to the best of my ability. Enjoy! Let me know if you have anything that should be added to this list.


Interactive Learning

This is the classic that has to be on here. I personally got started here with the basic JavaScript and PHP tutorials. It is a bit limited in it's selection, however, it is a very valuable resource for new programmers, or those who are interested in a new language.

This is a wonderful site for learning Node JS. I haven't made it very far in, however, I really like the way it is presented, and definitely recommend it. You can download a lesson, and it will be added to your Path, so you can play it using your command line. Also provides lessons for Functional JavaScript programming, and Git basics.

This is a fancy little site where one can learn or polish their Python skills in exercises set in a virtual world. You can compete with others, and categorize your solutions so that others can check them out. The sign-up includes a brief JavaScript -> Python comparison for those who are proficient in JavaScript but do not know Python. I believe they plan to add more comparison guides in the future.

Learn SQL with short interactive lessons. A concept will be explained, then shown to you, and then you will be asked to modify the pre-written statement to retrieve a different result. Simple, yet good if you need to brush up on your SQL.

Learn general programming ideas and constructs through an interactive game. I have not proceeded far in this game, nor have I tried the multiplayer, so I do not have much else to say. Give it a shot!

Link Hubs

A Samuel L. Jackson inspired hub of links relating to programming. From Languages, to Frameworks, to Tools, to language-agnostic information about programming, this sure if a motherload of good links. 

Another excellent link hub with sites that will dive into programming languages. This is a great one. Has information on many topics from the entire stack. 

Teach Yourself To Code
This is a great site which links to several sources on how to program in specific languages or frameworks. Some of these are paid, however, the paid ones are denoted by a dollar sign.

This GitHub repo holds links to tons of RSS and Podcast feeds which are targeted towards Front-end developers. Worth a good check-out. I follow quite a few of these with my RSS and Podcast aggregators.

A collection of free books which explore various programming topics. I haven't extensively looked into this one, but it seems like a valuable resource.

This is another one which is hosted on GitHub. Quite a few books are on here. Once, again, this is getting into territory that I simply haven't had time to extensively test, however, I am sure that these books are wonderful.

This is a wonderful site which is dedicated to teaching you to code 'the hard way'. It's not really that hard. In fact, it guides you just enough while making you do the exercises. Definitely check it out. The books are paid books, but there is a free alternative if you do not wish to purchase the books.

Not so much specifically about programming (there are quite a few useful programming links), but a good resource nonetheless. 

Weekly E-mail Newsletters
To say that this is just a JavaScript publication would be severely understating it. There are also Ruby, Postgres, Bitcoin, and more! Right to your inbox each week.

Very similar to JavaScript Weekly, but curated by different folks. Also worth a subscription!

Useful Tools

Legal licenses dumbed down so they can easily be understood. I still recommend reading the licenses, but this is a good start if you are not fluent in Legalese.

This is a good resource for free icons. 

Interesting Articles

A very true and satirical look at programming. Well worth the read.


Good Paid Services

I had the pleasure of checking this site out a while back and loved it a lot. They have a ton of video content. I consider this one to be well worth a check out. They offer a free trial before you buy if you are unsure about the content.

A well known video site where one can learn anything from Photoshop to PHP. Check this one out if you have a few bucks to drop for a subscription. 

This is another video site in a similar vein to Pluralsight and Lynda, however, this one has interactive sections between videos, which I found to really help in hammering the knowledge into your memory. 

Monday, May 12, 2014

Web 3.0 And Standards

I'm sure that most of us remember the marketing storm that was Web 2.0, and I think that just about everyone can agree that we have reached the point in time where some sites take user input and output it as content in some way or another, and some sites just don't. And that is fine. Not every site needs 'Web 2.0'. So after hearing the term Web 3.0 tossed around here and there, I was prompted to look into what exactly it even means.

Web 3.0 isn't here yet, but it has been loosely defined as the point in time which the web has become so semantic that humans and computers can both read the same code and make quick and complete sense of it. This would include the wide-spread acceptance and usage of a solid standard. Although we have web standards bodies such as the W3C, and communities such as GitHub who get together and work in a similar standard way, we still haven't quite reached the point at which everyone is operating in the same basic way.

By this definition, marketing companies will weep, because Web 3.0 will not be the marketing phenomenon that Web 2.0 was. There are several people and articles which suggest different things about what Web 3.0 is, but I feel as though this definition is concise, and doesn't hinge on first having a definition of Web 2.0, which is good because many people still cannot agree upon what Web 2.0 even is. So after hearing the term Web 3.0 tossed around here and there, I was prompted to look into what exactly it even means.

I personally think it would be wonderful if we could reach this point. Education on topics would be much easier to obtain without the arguments about whether element X belongs above or below element Y, and why employing method Z makes you [insert insulting name here]. There is a wide array of arguments like this, and it seems as though any sort of tutorial or video about 'cutting-edge' topics (and I use that term lightly, because what is cutting-edge anyway?) has the potential to turn into an abstract standards debate at the drop of a hat. And to be quite honest, I kind of enjoy these debates, because it is an opportunity to hear opposing points of view, and to truly understand what other folks are thinking about these topics. But at the same time, I feel like debates like this can be compared to debates about support for Internet Explorer. We can stay at the point where we are not quite sure what we are going to do about it for a while, but eventually we are just going to have to make a decision about the TRUE right way to do things, or nothing will ever get done. If the right way is to put that element X below element Y, and everyone can get behind that, then that is the way it will be done.

And that is my understanding of what Web 3.0 will be. I think that we are slowly making our way towards that point, but there are just so many standards out there. Progressive enhancement, mobile first, offline first, open-source only, responsive; And those are just the ones that I can think of in less than 15 seconds. Granted, the majority of those play well together. For example, you can have a mobile-first web application which employs progressive enhancement. That is just the way it all works. But there is a certain point where people don't quite agree on the 'right' way, and that is where we are at this point in time.

With so many standards, which ones will it boil down to? Who will be the last standard standing, so to speak? Maybe it doesn't have to be just one. The important thing is that there ARE standards. That we aren't just jumping head-first into something, with every individual developer writing code completely different at a fundamental level. By no means do I discourage people from jumping into a new programming language, library, framework, and so on. I just want to express the importance of accepting some sort of standard before getting too far into these things. Of course you don't need to sit down and study these standards for a week or two, and them employ them militantly (although I'm sure some people would love it if this was a requirement), but at least be familiar with the common standards, and be prepared to change your ways if a job requires it.

When it all comes down to it, we as developers, are a community. At some point someone will likely have to read your code. We go to English classes (or Spanish, or Italian, or...) to learn the current standards for communication in our language. That doesn't mean that everyone has to speak in the same exact manner, and read through hundreds of pages of language constructs in order to have a conversation. We all take it and adapt it a little bit differently than each other, and that is beautiful. That is the way programming should be. Everyone knows the basic idea and the basic standards, and can make tiny tweaks to it in order to make it work better for them, but fundamentally changing the way the code it written does nothing but make other developers unhappy.

This isn't meant to sound preachy, because I myself am quite guilty of misusing language constructs, or making up my own "awesome" way to write some language. But when it all comes down to it, it is just confusing, and you will either leave a big mess for someone else, or end up having to rewrite or heavily comment your code. To bring a little bit of anecdotal evidence into this, I decided it would be really cool if I did a bunch of complex Ajax calls and pulled data from different locations, and used functions in PHP which match functions in JavaScript in order to make a class page for one of my JavaScript programming classes a little bit 'better'. This is all fine and dandy, until my Professor approached me after class one evening and explained to me that he had no idea what I was doing with my site, because it was so non-standard and uncommented that it is just a slew of needlessly complex JavaScript. Thankfully my Professor was a very nice fellow, and told me that if I comment it well, and he can make sense of it, he would still grade it. So by no means am I exempt from this, and I feel as though a lot of us go through this stage of their early development years where they think it would be really cool if their code was completely different from everyone else's.

All I have to say is this: Standards are standard for a reason.

I would love to hear comments or stories from anyone who might be reading this! I know it go a little bit long-winded and off-topic, and if anyone actually read through all of this -- kudos to you! I hope it didn't sound 'preachy' at any point.

Monday, April 28, 2014

Podcasts

I have been tuning in to a lot of podcasts as of late in order to aggregate a better knowledge about what is going on in the web development and design community. In my travels, I have run across a collections of excellent podcast series. In fact, my two favorites are The Web Ahead hosted by Jen Simmons, The Changelog hosted by Adam Stacoviak, and Up Up Down Down hosted by Allen Pike and Nigel Brooke. They have guests on weekly, and spend about an hour discussing a particular topic in in detail. The Web Ahead and The Changelog focus exclusively on web development, and open-source libraries and projects respectively, while Up Up Down Down focuses on video game trends as well as some other more technical aspects of the industry.

You can check them out on their web sites, or subscribe to them on iTunes in order to receive updates when new episodes come out. They also have individual RSS Feeds which will alert you when there is a new episode. Coincidentally, The Web Ahead and The Changelog are actually hosted on the same site, which is 5by5, They have a ton of other podcasts, but I have not checked the other ones out yet.

These podcasts have a plethora of relevant and interesting information, which I find to be not only entertaining but highly informative. For example, in Episode 67 of The Web Ahead, Jen invited Doug Schepers onto the show to discuss SVG, which I'm sure many of us in the web development/design field have heard of, but not necessarily looked into for one reason or another. After hearing Doug, who has been working with SVG for a decade or more, talk about it so passionately, and in such great detail, I am highly tempted to implement it into one of my personal side-projects in the very near future.

The Changelog is fascinating in it's own right, because although The Web Ahead focuses on free web technologies, the The Changelog sets its sights entirely on the open-source side of things, where users can contribute to the project on GitHub in many cases. Open-Source software of all types has always been an intriguing topic for me, so I find myself tuning into this podcast regularly.

Up Up Down Down is hosted by game developers who are currently working in the industry. They have a guest on, and all debate a topic together. For example, I recently listened to Episode 8, in which they invite Ken Wong, one of the designers of  the new Monument Valley, which is available on the Apple App Store. They discussed and debated the ways in which video games can or can not be considered art, and attempted to define how one thing can be considered art while another is not. Although I am not personally involved in the video game industry, I feel as though the host Allen Pike summed it up perfectly in one of their earlier episodes when he expressed his opinion that even people who do not work on video games can enjoy the show if they have any interest in programming in general.

There are a couple of other excellent podcasts, which I have tuned into for an episode or two and plan on going back to. I feel that one I should mention is CodePen Radio hosted by the founders of Codepen.io, Chris Coyier, Alex Vazquez, and Tim Sabat. I have only listened to one episode from these folks, however, I found it extremely entertaining and am going to download the rest of their episodes to listen to soon.

I would love to hear from anyone who may be reading this to find out what kind of podcasts you listen to. And if you don't listen to podcasts, what sort of content do you subscribe to in order to stay in the loop? For example, weekly shows, screencasts, YouTube channels, etc.

As always, feel free to comment about anything that may be on your mind after having read this. Even if you didn't read a word of this, and you just want to plug your favorite podcast, I would love to hear from you.