Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Few Logical Programming Questions

I started coding recently, and this community has helped me a lot understanding many things which I was not aware of. However, many reputed coders instructed me of some patterns, the way I have to code and the way I shouldn't write codes. Although I accepted their suggestion with gratitude, there was many thing I couldn't understand.

I want your point of view to understand the few questions which has been running over my mind from the past few days.

MySQL

  1. Why is it that many coders gave me thumbs down whenever I used the * in the select statement? Why did they suggest using entityname.tablename even if sometimes I want almost all the data from a table?

  2. Is it okay if my code make a frequent trip to the database on a single page say about 5-8 request? To be more precise in a page I would want to update the value, insert the value, count the rows, sum up the values, and so on.

    I once made almost all the select statement as a single query and one of the reputed user of this community suggested me not to do it that way instead suggested me to use a user defined function. (BTW, user defined function helped me a lot to clean my code and understand the error more quickly). What is your take on this?

Frameworks

When I started learning PHP I knew little about programming and more about web, although I had learned the popular computer languages like C, C++, .NET, Java etc. in my college. It was just the formal and theoretical subject which I learned and when I knew I wanted to be a web developer internet was my best friend and the community helped me out.

Now when I have started my journey of learning programming I have set some goals and aims myself, I want to be a Pro PHP Developer, I want to Master the HTML, JS, CSS, MySQL etc. My question here is

IS FRAMEWORK EVIL FOR LEARNERS LIKE ME?

like image 844
Ibrahim Azhar Armar Avatar asked Sep 23 '10 15:09

Ibrahim Azhar Armar


4 Answers

MySQL

Using * is fine. When you start getting really complicated with MySQL queries - joining and comparing tables - then you want to look at using entityname.tablename just to keep yourself from getting confused.

The next question is too subjective. It depends on your server and the efficiency of your script. It also depends on how many people will be using the script. Obviously, as with anything, the less you use it the better. If you can do one sql query instead of 5 then do that, but if you're only going to have a couple of hundred people viewing your blog then I wouldn't worry too much. Its the same with functions. Obviously its much better to put everything into functions. This helps in the long run because you will only have to edit your script in one place to make changes. Lets put it this way - if you're copying and pasting code then you should be using functions. But then if your script is only 1 file, 200 lines long, then I wouldn't worry if you don't want to.

Frameworks

Frameworks are difficult to gauge the usefulness of. Obviously learning things like Zend or Mage are powerful frameworks that will help you to create much more efficient and complex web projects. However, for learners it may confuse you. I would say definitely not to try and learn them until you get your head completely around PHP. Hopefully then you will have a great enough understanding that you won't have a problem if you come across these. You miss the main point of a language if you learn a framework. For instance - you won't learn javascript if you just learn jquery. You'll learn a bit, but you'll never completely understand it.

Thats my take, but its a very subjective question.

like image 165
Thomas Clayson Avatar answered Sep 29 '22 20:09

Thomas Clayson


Decent arguments to dislike select all in SQL: http://www.joelango.com/2007/04/30/why-you-should-never-use-select-star/

About number of queries: this is an issue of performance. The rule of thumb is to optimize when performance actually becomes a problem. If you are running a site that serves thousands of requests a minute you may need to start worrying. Otherwise, it just doesn't make a difference.

About frameworks: if you want to learn PHP at its foundation then avoid frameworks for now. Otherwise, if you really want to jump in and get things done, starting with a framework should be fine. For example, I don't know JavaScript itself but I work fine with JQuery (a JavaScript library/framework).

like image 35
erisco Avatar answered Sep 29 '22 20:09

erisco


like Thomas Clayson says the use of * is fine for simple queries, but when you have complex join statements you should specify the fields and give the tables a name (like someTable as a) and use a.someField in order to organize your query.

When it comes to learning a specific language, frameworks are just tools to help developers get the job done in less time (including maintance, worst thing ever jaja) but if you are a starter what you really should look is to learn is what programming paradigm does the language support (object orientation, procedual or functional) and focus or learning those paradigm and the specific commands of the language because for example if you preffer object orientation class will always be class, function and procedures will always have the same squelet but the implementation differs in each language so if you learn the paradigm it will be easy to learn any language.

That my humble opinion hope will help, regards

like image 44
jameslimousin Avatar answered Sep 29 '22 21:09

jameslimousin


While I agree with Thomas re: frameworks, I have to disagree, or at least expand, on what he says about MySQL.

While there is technically nothing wrong, in most cases, for using * in SELECT requests, expanding the column names makes the statement easier to understand and more self-documenting for other developers that might be in your code. They can look at the query and see what properties the row object should have. Aside from that, * is also slightly less efficient during the query. It's really nothing to worry about unless you only need a fraction of the columns available.

As for the multiple queries, it depends. If running 10 SELECT for specific items is faster than running 1 SELECT for multiple items and parsing them out, then run the 10 SELECTs. It's far better to run multiple small and fast queries than one large and slow query. Obviously, each application will be different.

like image 25
Ryan Chouinard Avatar answered Sep 29 '22 19:09

Ryan Chouinard