开发者

Few Logical Programming Questions

开发者 https://www.devze.com 2023-01-17 13:01 出处:网络
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 cod

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 us开发者_如何学运维er 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?


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.


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 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


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.


  1. On using * in a SELECT statement: While it is perfectly valid syntax, it is often recommended against for two reasons. One, speed; two: code quality. With *, it is not evident which rows you are requesting (and getting) and in which order; if the database is ALTERed and rows are added, you are getting these as well, and if the order of rows is changed, the results will be quite unexpected. If you specify the rows you want, that results in safer and faster code.

That being said, you can still use * for convenience and some testing. But for production code, please consider specifying the rows.

  1. Making separate DB calls with SQL statements will slow down the processing a lot, since every one of these has to be sent, received, compiled and interpreted, results sent back - just to receive them again in another call. stored procedures (or "user defined functions") are more efficient, and also much safer against attacks like injection. If you can make them without sacrificing flexibility or other aspects you need, do so. But don't do that while you're still finding your way - Premature optimization is the root of all evil, says He.

  2. Frameworks are quite useful, but indeed not a good idea for Learners / Beginners. They lead you to skip important parts of what you should be learning and limit your thinking. To judge the advantages and shortcomings of frameworks, to chose frameworks, and to overcome their flaws, you should have a solid knowledge of the basics first.

So learn your basics, and once you have a firm grip on them, you can start to use frameworks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号