Because they help me write code faster. You will wonder that why is faster? They take me more time to do another thing instead of focus writing code. I though it is as you are thinking. Let me sharing my experience about it.
I have many experience years about programming, almost problems I can solve them by using my knowledge, experience and google, ofcourse. But my solving issues speed is not fast as I expected even I solved them before. One year ago, I realized my problem from recent projects and find down why. There are points I found:
What action do I face a problem/issue? Check the issue I have met or not
-
If I haven't met it before, depend on its complex
- Simple: I solved it myself. DONE. I didn't do anything else.
- Complex: Google. Almost problems are solved by someone before, just copy or apply (change few codes) it suit for my case. My next action is that leaving a comment contains a link to the solution I used on my code and bookmark it.
-
Otherwise, I have solution already. But where is it? It is in somewhere of my past projects. I will check my memory that where did it come from?
- It is my own solution: I will search code of all projects or specific project I remember (luckily) to find it. No, I haven't ever done it. Because I didn't provide any keyword relates to issue or its solution. My way is that reinvent solution again.
- It comes from someone: Google and find the solution again.
As you see, I repeat same action even I met issue before, having solution already. Leaving comment on code is a very good habit, it helps you understand code again. But it isn't helpful to finding your existing solution.
My suggestion is that putting solutions you have (do them yourself or copy them from somewhere, remember provide a reference link) to somewhere (eg: github, your blog) you have ability find them again. I put them to my github account.
I had a place (github) keeps my solutions (or call them are code snippets because I just copy them when I need). It helps me save a lot of time. But I have issues when using them:
-
I take a look solution I want to use again and wonder why it did that, it seems have a case will be wrong. Because I didn't leave a comment to explaining why code is, so I need to make some tests to check it is right or not (remember, I tested it before).
Example: pagination issue (always face it in web development). How many pages when knowing results per page, total of results? My solution (javascript code) is
function countPages(totalResults, resultsPerPage) { return (totalResults + resultsPerPage - 1) / resultsPerPage; }
Why need
+ resultsPerPage - 1? Why shouldn't code bereturn totalResults / resultsPerPage;? I must test the solution again to make sure it is still right. I use black-box testing (don't worry if you don't know it, just give specific inputs and test output is right or not). So I made some test-cases:- totalResults=0, resultsPerPage=50 ==> (0 + 50 -1) / 50 = 0 (
/isinteger divisionoperator) right - totalResults=1, resultsPerPage=50 ==> (1 + 50 -1) / 50 = 1 right
- totalResults=10, resultsPerPage=50 ==> (10 + 50 -1) / 50 = 59 / 50 = 1 right
- totalResults=50, resultsPerPage=50 ==> (50 + 50 -1) / 50 = 99 / 50 = 1 right
- totalResults=51, resultsPerPage=50 ==> (51 + 50 -1) / 50 = 100 / 50 = 2 right
All cases I listed are right, so the solution is still right (at least my test-cases). I remember that I made some test-cases like that before because I wondered same questions. Again, I repeat same action for issue I met.
To avoid it, I just include test-cases into comment of solution. Future, I may be wonder same question again, but I just test new cases are not listed at that time
- totalResults=0, resultsPerPage=50 ==> (0 + 50 -1) / 50 = 0 (
-
There are many ways to solve same issue. Why did I choose the solution (Because it is the best or I just liked it)? Which case should I use another? There are questions I wonder when reuse solution comes from someone.
Example: check element has given attribute or not by jQuery. I found 3 ways from post, but I just list 2 first ways for example
-
1st way
var attr = $(this).attr('name'); // For some browsers, `attr` is undefined; for others, `attr` is false. Check for both. if (typeof attr !== typeof undefined && attr !== false) { // Element has this attribute }
-
2nd way
$(this)[0].hasAttribute("name"); jQObject[0].hasAttribute("name");
Both are working.There are my questions:
- Which one should I choose now? Which one did I choose before and why? I am really confused now, I need to make a decision. I waste my time again. Finally, I choose 2nd way because it is short, takes only line, so my code is dry.
- Next question is that should I keep 2nd way and removing 1st way? No, I should keep both. I leave a comment I choose 2nd way because it's short.
- So should I leave a comment for 1st way? Yes, I should, a comment like that it's long, I don't like it; or it's another way, just reference. Future, I will not wonder why I didn't choose 1st way.
There are many solutions for a issue. Leave comment for each solution and take note which one you choose and why.
-
My suggestion when you save solutions:
- Always take note
- Solution relates to algorithm, includes test-cases you tested before in comment
- There are several solutions relate to technique issue, leave a comment covers: which solution you chose and why (should put it at begin, you will see it first), why didn't you choose remaining solutions? Which case/context do remaining solutions suit to?
PHP supports both proceduced and oriented-object programming. Almost built-in functions are code by proceduced and follow underscore_style, but methods (functions are defined in class) named by camelCase; for variables, I saw some follow camelCase, some follow underccore_style. I confused that when should using same rule when defining variables (in functions, in method, in class: field) or not? Should I follow Zend or PSR coding style? Depend on your style, maybe mixin them. But you must to make decision and write down your own style. Don't just reference them, it's hard to follow.
Example: Sometime I review my code to update something, I realized that I used mixin code style: $is_selected, $hasItem and I wonder which one I choose to use finally.
Example 2: for functions are also, I tried to write code by OOP because I love it. But naming methods (functions are in class), I follow underscore_style because of built-in functions style. After that, I see built-in classes (eg: DateTime) and 3rd classes use camelCase. They conflict and I should use camelCase for methods. So should functions are not defined in class (public functions) follow camelCase or keep underscore_style? I know I should follow community style. I found several code styles, eg: Zend and PSR. I should choose which one. They have many rules, I can't remember them, so I just focus naming style, remaining rules are supported by IDE I'm using. You can choose one of them or mixin depend on your style.
Your style maybe wrong, but it must be systematic wrong