问:
try-catch in javascript... isn't it a good practice?
There is a provision for try-catch block in javascript. While in java or any other language it is mandatory to have error handling, I don't see anybody using them in javascript for greater extent. Isn't it a good practice or just we don't need them in javascript?

答:
try-catch in javascript is just as valid and useful as in any other language that implements them. There is one major reason its not used as much in javascript as in other languages. Its the same reason javascript is seen as an ugly scripting language, its the same reason as why people think javascript programmers aren't real programmers:

Javascript is an incredibly accessible and pervasive language

The mere fact that so many people are exposed to javascript (by virtue of the only supported language by browsers) means that you have lots of unprofessional code out there. Of course there are also many minor reasons:

some things in javascript are asynchronous and thus aren't catchable (asynchronous stuff)

there has been much overblown talk about how try-catch has a huge performance hit. It has a bit of a performance hit, but for most code, it is well worth it.

javascript was (unfortunately) implemented in a way that often silently ignores errors (automatically changing strings to numbers for example)

Regardless, try-catch should be used, but of course you should learn how to use them properly - like everything else in programming.


问:
How much freedom should a programmer have in choosing a language and framework?
I started working at a company that is primarily C# oriented. We have a few people who like Java and JRuby, but a majority of programmers here like C#. I was hired because I have a lot of experience building web applications and because I lean towards newer technologies like JRuby on Rails or nodejs.

I have recently started on a project building a web application with a focus on getting a lot of stuff done in a short amount of time. The software lead has dictated that I use mvc4 instead of rails. That might be OK, except I don't know mvc4, I don't know C# and I am the only one responsible for creating the web application server and front-end UI.

Wouldn't it make sense to use a framework that I already know extremely well (Rails) instead of using mvc4? The reasoning behind the decision was that the tech lead doesn't know Jruby/rails and there would be no way to reuse the code.

Counter arguments:

He won't be contributing to the code and is, frankly, not needed on

this project. So, it doesn't really matter if he knows JRuby/rails or not.

We actually can reuse the code since we have a lot of java apps that
JRuby can pull code from and vice-versa. In fact, he has dedicated
some resources to convert a Java library to C#, instead of just
running the Java library on the JRuby on Rails app. All because he
doesn't like Java or JRuby

I have built many web applications, but using something unfamiliar is causing some spin-up and I am unable to build an awesome application in as short of a time as I'm used to. This would be fine; learning new technologies is important in this field. The problem is, for this project we need to get a lot done fast.

At what point should a developer be allowed to choose his tools? Is this dependent on the company? Does my company suck or is this considered normal? Do greener pastures exist? Am I looking at this the wrong way?

答:
I'm going to assume that you were honest up front during your interview about your lack of C# knowledge, because if you weren't then you might be in a very precarious position from a legal standpoint.

Good programmers know programming.  Whilst nobody can obviously be well-versed on all languages and frameworks, there is considerable commonality amongst most of them.  Unless you're being asked to work in a language that's massively different from what constitutes the mainstream these days (Lisp, for example), then a good programmer should be able to adapt.

Naturally there's a learning curve.  If the employer hired you then they must be confident in your abilities to follow that curve in a reasonable amount of time (again, assuming you were honest up front regarding not knowing C#).  The C# language borrows heavily from Java, and in more generally, most class-based programming languages are fundamentally quite similar (you mentioned node.js, which builds on top of ECMAScript, which is a prototype-based language, so you're obviously comfortable with other programming paradigms.

Good programmers should, in addition to being flexible, be eager to learn new things.  In software development you're generally either learning or becoming irrelevant.

Of course your employer, assuming they knew you didn't know C#, has to meet you half-way.  If you show eagerness to learn then they have to give you the time and resources to do so.  Throwing you in at the deep end is unfair and needlessly stressful.  You need to sit down and have a calm, rational discussion with your superior.  If they want it in C# then they must be prepared to accept that you'll be on a learning curve whilst working on it and it would be unfair for them to impose tight deadlines on you.  If the deadlines aren't flexible and if they are of high strategic importance, then they need to be prepared to allow you some latitude to get the job done within that deadline.  If they need it to be in the more commonly-used language in their office, then you can perhaps request to implement it now in what you're familiar with to meet the deadline, and then as your next project re-implement it in C# as a learning exercise and to bring the software into compliance with internal requirements once it meets the external ones.  Like I said, most of the more commonly-used languages today have a lot in common so it mostly come down to implementation details.

You do have to be prepared to accept sooner or later that you're working in a C# shop and therefore you do need to have C# under your belt.

ContentLicense: CC BY-SA 3.0


问:
Why HTML select is not searchable by default?
I was wondering why the default select tag of HTML, for example:

```<code><select name="company_name">
    <option value=""></option>
    <option value="1">Company 1</option>
    <option value="2">Company 2</option>
    <option value="3">Company 3</option>
    <option value="4">Company 4</option>
    <option value="5">Company 5</option>
    <option value="6">Company 6</option>
</select>
</code>```

is not searchable by default. Of course this is a browser feature, but it is just really weird that none browser (as far as I know) doesn't have it.

Am I missing something here? Is there any browser that actually have as default a searchable select tag? Is it so complicated to achieve it, or is it just that people doesn't want it? Is this a problem with OS?

I know that I can achieve that easily with JavaScript (e.g. Jquery Chosen plugin... e.t.c.) but I was wondering if there is any plan for popular browsers like firefox and chrome to have it as default.

The result that I am expecting is like the below image on Windows:

答:
Dropdowns are semi-searchable in chrome and firefox. Just start typing and it will jump to option that matches typed text. You probably didn't notice it in your example since all options start with same text and pressing space finishes the searching.

Example at http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select


问:
How is DRY principle ( applied at class level ) related to SRP?
In other words, is DRY (don't repeat yourself) applied at a class level a subset of SRP (single responsibilty principle)?

What I mean is that while SRP states that each class should have only a single responsibility ( ie. class should only have one reason to change ), is it the application of DRY at class level that prevents two classes from having the same responsibility ( thus it is DRY that prevents the repetition/duplication of same responsibility in two different classes )?

Thank you

答:
SRP means that your class should have only a single responsibility, so you can easily name your class, and you can tell only by reading the name, what it does.

DRY means that you should not repeat yourself, so by maintaining your code you don't have to override the same code twice or more.

They have certainly a connection, because both of them are about maintaining the code. DRY is a low abstraction level tool to do that, because it has no clue about what the code does. SRP is a high abstraction level tool to do that, because it is about responsibilities and not the code itself. So you should use both concepts together by developing your application. Based on every case I met until now, I recommend the SRP over DRY rule. So by any conflict between the two principles you can decide which one to use.

For example, where A and B are responsibilities:

If you have a DRY code which does not meet SRP:

```<code>class AB
    doSomethingArelated()
    doSomethingBrelated()
</code>```

you can split it up the following way, so it will meet with DRY and SRP as well:

```<code>class A
    doSomethingArelated()

class B
    doSomethingBrelated()
</code>```

If you have a DRY code, but A and B use the same properties:

```<code>class AB
    x
    y
    doSomethingArelatedToX()
    doSomethingBrelatedToXAndY()
</code>```

then if you want to meet with DRY and SRP at once you should use polymorphism. You should use composition over inheritance by that, because it is easier to maintain.

```<code>class AB
    a
    b
    doSomethingArelatedToX()
        a.doSomethingArelatedToX()
    doSomethingBrelatedToXAndY()
        b.doSomethingBrelatedToXAndY()

class A
    c
    doSomethingArelatedToX()
        c.doSomethingGeneralToX()

class B
    c
    y
    doSomethingBrelatedToXAndY()
        c.doSomethingGeneralToX()
        doSomethingToY()
    doSomethingToY()

class C
    x
    doSomethingGeneralToX()
</code>```

In really special cases SRP can override DRY:

```<code>class AB
    a
    b
    doSomethingArelatedToX()
    doSomethingBrelatedToXAndY()

class A
    x
    doSomethingArelatedToX()

class B
    x
    y
    doSomethingBrelatedToXAndY()
</code>```

This happens by DDD when you have properties related to multiple bounded contexts. The most common thing is the aggregate id in here, but for example by a webshop the product size or weight can be important in the delivery and inventory contexts either.

Body:
SRP means that your class should have only a single responsibility, so you can easily name your class, and you can tell only by reading the name, what it does.

DRY means that you should not repeat yourself, so by maintaining your code you don't have to override the same code twice or more.

They have certainly a connection, because both of them are about maintaining the code. DRY is a low abstraction level tool to do that, because it has no clue about what the code does. SRP is a high abstraction level tool to do that, because it is about responsibilities and not the code itself. So you should use both concepts together by developing your application. Based on every case I met until now, I recommend the SRP over DRY rule. So by any conflict between the two principles you can decide which one to use.

For example, where A and B are responsibilities:

If you have a DRY code which does not meet SRP:

class AB
    doSomethingArelated()
    doSomethingBrelated()

you can split it up the following way, so it will meet with DRY and SRP as well:

class A
    doSomethingArelated()

class B
    doSomethingBrelated()

If you have a DRY code, but A and B use the same properties:

class AB
    x
    y
    doSomethingArelatedToX()
    doSomethingBrelatedToXAndY()

then if you want to meet with DRY and SRP at once you should use polymorphism. You should use composition over inheritance by that, because it is easier to maintain.

class AB
    a
    b
    doSomethingArelatedToX()
        a.doSomethingArelatedToX()
    doSomethingBrelatedToXAndY()
        b.doSomethingBrelatedToXAndY()

class A
    c
    doSomethingArelatedToX()
        c.doSomethingGeneralToX()

class B
    c
    y
    doSomethingBrelatedToXAndY()
        c.doSomethingGeneralToX()
        doSomethingToY()
    doSomethingToY()

class C
    x
    doSomethingGeneralToX()

In really special cases SRP can override DRY:

class AB
    a
    b
    doSomethingArelatedToX()
    doSomethingBrelatedToXAndY()

class A
    x
    doSomethingArelatedToX()

class B
    x
    y
    doSomethingBrelatedToXAndY()

This happens by DDD when you have properties related to multiple bounded contexts. The most common thing is the aggregate id in here, but for example by a webshop the product size or weight can be important in the delivery and inventory contexts either.


问:
Passing a dependency to a controller class
I'm looking for an approach to the following problem:

I have a reusable library. It contains a controller class, in the MVC sense. Let's say it's called UserAdministrationController, and it's for editing users. This class requires an object implementing IUserStorage, for storing its data. The library doesn't need to know the details of how this object works.

Now I want to write an application that includes the library. In it, I'll provide an implementation of IUserStorage. To instantiate this, I'll need to pass it some dependencies, like a database object, and other things that my application only knows about at runtime.

But how would I then pass this object from my application to the library? I'm not instantiating the UserAdministrationController directly from my application; it gets created by the library itself.

Hope this makes sense. I'm trying to keep my description abstract and not specific to a particular framework or language.

答:
If the reusable library is an already existing library and it constructs the UserAdministrationController internally, then the library should already have an its own mechanism by which it provides the IUserStorage during initiallization. You just need find out how the library is doing it and use that.

If you were setting out to write the reusable library yourself then there would be a question of choosing DI libraries or language features for doing this, but it sounds like the library is already written and you are are just trying to use it. In that case all those decisions have already been made by the library writers; you just need to find out what they are and use them.


问:
How to insert/update a large amount of data into mysql using php
I have an excel sheet which has a large amount of data. I am using php to insert the data into mysql server.

I have two problems

1) I have to update a row if the id already exists, else insert the data.

2) BIG PROBLEM : I have more than 40,000 rows and the time out on the sql server which is set by the admin is 60 seconds. When i run the update/insert query it will take more than 60 seconds, and because of this there will be a timeout. So the whole process will fail.

Is there a way I can do this ?

Currently I am checking the student id if it exists, then update otherwise insert. This I feel is taking a lot of time and causing the server to time out.

Also I have this field in the mysql stating the last time the data was updated(last_update). I was thinking of using this date, and if it is past a particular date(ie last time i ran the program) then only those rows should be updated.

Will this help in anyway ?

And what is the query i can run so as to check this date in the mysql database, that if it is past a particular date only those rows need to be updated and not everything else.
(Please help me with an example query for the above!!!!!!!!!!!!!!!!!)

答:
Thanks for all your answers.
I have finished my project. I Have found a way where I can overcome any of these memory issues , so since i already mentioned that i am going to get my files in the form of an excel sheet. And excel can easily be converted in the form of a csv.

I used the LOAD DATA INFILE Feature.

The LOAD DATA INFILE Feature just uses the csv and can directly load the entire file in one shot and need to write the query again & again.

Also Thanks guy for suggesting the INSERT ON KEY DUPLICATE.

This answer will give you a clear idea of what I have done.
https://stackoverflow.com/questions/15271202/mysql-load-data-infile-with-on-duplicate-key-update


问:
Is it alright to create another reference to a javascript object just for ease of access
I have a use case where I need to create say two javascript objects & use their properties in one another. eg -

```<code>var Object1 = {
  settings: {
   property1: 'someValue',
   property2: 'someValue'
  }
}

var Object2 = {
  foreignProperty: Object1.settings.property1;
}
</code>```

I wanted to know if its alright to use a reference object for settings if I know that I will be using the settings property a lot. eg-

```<code>var Object1Settings,
    Object1 = {
      settings: {
      property1: 'someValue',
      property2: 'someValue'
    }
}
var Object1Settings = Object1.settings;

var Object2 = {
  foreignProperty: Object1Settings.property1;
}
</code>```

Is this approach acceptable in terms of right ways of coding & performance?

Thanks

答:
Not only it increases readability, but such code also runs way faster.

Generally when programming I believe this is the order of priorities:

Readibility

CPU performance

Saving memory

Based on this you can see, that caching any value is probably a good idea. It costs memory and saves you CPU cycles.

But I hope you do know that changing Object2.foreignProperty will render original Object1.settings.property1 unchanged.


问:
why create mobile applications for web oriented programms, if it's just possible to use websites?
There are a lot frameworks on the market who wrap actual websites into mobileapps.  I found myself thinking about the question - why is it needed to create mobileapps if it's possible to just having mobile version of the site?</p>

答:
Those frameworks offer additional features, most relating to the hardware of the smartphones. Those include:

accessing and manipulating hardware functions such as cameras, flashlight, screen brightness, gyroscope, vibration...

software access, for example contacts, calendar, phone number, mails, HWID...

running in the background, receiving (push) notifications

cookie-independent clientside storage

Still you often have a meaningless notion of "we need an App for that stuff, Apps are what the cool kids have nowadays" in the management - or an independent contractor who uses this notion to make a few thousand bucks more out of pushing his already finished work through PhoneGap.


答:
There are many reasons why making an app has advantages. René said some. The ones I would like to add are : Native feel, touch UI optimizations and better UI frameworks.

Existing web pages are not optimized to use on small, touch screens. Also, web technologies make it harder to optimize them for touch. So if you are going to invest into making the website accessible from touch device, you can just as well invest into making an app and have website optimized just for desktop.

With this comes native feel of application. For user, it is much better if the application he uses feels and is used same way as everything else in his ecosystem. Something like that is impossible for webpage, but almost guaranteed for custom mobile app. And while it is possible to optimize the UI for specific mobile platform, I cannot even begin to describe how stupid it is to see iOS-like UI webpage on my Windows Phone.

The third one might be just my opinion, but from my perspective HTML+CSS+JS are worse than terrible for application UI. Thanks to it's heritage of hacks and committee-based design and fact it is meant for documents, not applications it is extra hard to create design that doesn't break at simplest thing, like different DPI display. Something that is quite common in mobile world. All major mobile platforms have UI frameworks that are many times better than anything you can achieve with HTML+CSS+JS. Or at least make things easier.

Title: why create mobile applications for web oriented programms, if it's just possible to use websites?

Body: There are a lot frameworks on the market who wrap actual websites into mobileapps.  I found myself thinking about the question - why is it needed to create mobileapps if it's possible to just having mobile version of the site?</p>


问:
while(true) and loop-breaking - anti-pattern?
Consider the following code:

public void doSomething(int input)
{
   while(true)
   {
      TransformInSomeWay(input);

      if(ProcessingComplete(input))
         break;

      DoSomethingElseTo(input);
   }
}
```

<p>Assume that this process involves a finite but input-dependent number of steps; the loop is designed to terminate on its own as a result of the algorithm, and is not designed to run indefinitely (until cancelled by an outside event). Because the test to see if the loop should end is in the middle of a logical set of steps, the while loop itself currently doesn't check anything meaningful; the check is instead performed at the "proper" place within the conceptual algorithm.</p>

<p>I was told that this is bad code, because it is more bug-prone due to the ending condition not being checked by the loop structure. It's more difficult to figure out how you'd exit the loop, and could invite bugs as the breaking condition might be bypassed or omitted accidentally given future changes.</p>

<p>Now, the code could be structured as follows:</p>

<pre class="lang-java prettyprint-override"><code>public void doSomething(int input)
{
   TransformInSomeWay(input);

   while(!ProcessingComplete(input))
   {
      DoSomethingElseTo(input);
      TransformInSomeWay(input);
   }
}
</code>```

However, this duplicates a call to a method in code, violating DRY; if TransformInSomeWay were later replaced with some other method, both calls would have to be found and changed (and the fact that there are two may be less obvious in a more complex piece of code).

You could also write it like:

public void doSomething(int input)
{
   var complete = false;
   while(!complete)
   {
      TransformInSomeWay(input);

      complete = ProcessingComplete(input);

      if(!complete)
      {
         DoSomethingElseTo(input);
      }
   }
}
```

... but you now have a variable whose only purpose is to shift the condition-checking to the loop structure, and also has to be checked multiple times to provide the same behavior as the original logic.

For my part, I say that given the algorithm this code implements in the real world, the original code is the most readable. If you were going through it yourself, this is the way you'd think about it, and so it would be intuitive to people familiar with the algorithm.

So, which is "better"? is it better to give the responsibility of condition checking to the while loop by structuring the logic around the loop? Or is it better to structure the logic in a "natural" way as indicated by requirements or a conceptual description of the algorithm, even though that may mean bypassing the loop's built-in capabilities?

答:
If a particular flow of control is naturally expressed as a break statement, it is essentially never a better choice to use other flow control mechanisms to emulate a break statement.

(note that this includes partially unrolling the loop and sliding the loop body down so that the loop begin coincides with the conditional test)

If you can reorganize your loop so that the flow of control is naturally expressed by having a conditional check at the beginning of the loop, great. It might even be worth spending some time thinking about whether that is possible. But it not, don't try to fit a square peg into a round hole.

ContentLicense: CC BY-SA 4.0


问:
OSS dual licensing / market segregation
I have an open-source project, which I would like to commercially license to various types of businesses.

xGPL licenses do not work for me since they don't allow any conditional addenda.

What I'm seeking to achieve is:

require commercial licensing for businesses

allow use and changes under some OSS license for individuals

allow free use (same as above) to specific business branches on individual selection basis (e.g. educational institutions).

I'm really confused about how to achieve this and don't violate the terms of the OSS part of license at the same time.

Edit: I'm looking for an existing free license, which doesn't explicitly forbid dual licensing like GPL family does.

答:
If you are the copyright holder of the code, then you can provide the sources to different clients using different licenses. These licenses don't even need to be compatible with each other.

The common mode of dual licensing is that you provide two or more licensing options:

a (L)GPL (or other copyleft) license for open source projects. The copyleft nature of the GPL ensures that it can't be used for closed-source projects (which most commercial ones still are).

one or more commercial licenses for use in commercial/closed-source projects. You can vary the fee and license restrictions in any way you like (so a separate license for educational institutions is a possibility).

The thing to watch out for in dual-licensed projects is that you don't blindly accept contributions on your open source version.

Unless you get the copyright on contributions assigned to you, you can't move those contributions over from the open version to the commercially licensed versions, because you would be bound by the GPL license under which the contribution is made.

If the copyright has been assigned to you, then you are free to use the code as you see fit.

Using an open source license that allows sub-licensing means that anyone who gets a copy of your code can legally use it in a closed source project as well.

There are no open-source licenses that prohibit commercial use of the code, as that goes against the ideas behind open source code.

Title: OSS dual licensing / market segregation


问:
Time complexity implications when designing shell scripts for large data/high number of files?
There are many questions about the performance of commands or scripts on the U+L SE asset. As time is of the essence, this is often evaluated using the bash time reserved word or the external time command and a subset of the target data, under average or no load.

Yet there are scenarios which are either about a large number of files or just operations on very big files. In some instances a careful examination of what happens in the shell, intricate knowledge of command behavior, even hardware considerations, provide insight into efficiency. But benchmarking remains no simple affair.

In one instance recently a community member made a comment about the time complexity of a command, implying that since there was no sorting operation - and not merely no sort command - it would ultimately scale better with more data. The solution involved awk, whereas another solution involved for instance a combination of the grep, sort and head commands.

Is this just an instance of "the simpler the better" - what are main implications of time complexity when designing shell scripts which operate on huge data sets(number or size)?

Beyond common sense and sorting (an operation for which there is a seemingly high level of intuition about performance costs, even for a non expert), is there a well known common time complexity example which would showcase how the concepts apply in practice to shell scripting?

答:
First you have to understand what the time complexity of a program is telling you. Time complexity is a measure of how the running time of a program changes as the size of the input data changes. If n is the size of the input data, then saying that program A is O(n) and program B is O(n * log n) means that the running time of program A is always less than C * n, and the running time of program B is always less than K * n * log n. C and K are some constants, which we usually don't know.

If you graph (C * n)  and (K * n * log n) versus n, there will be some point at which the line for  (K * n * log n) will cross above the line (C * n). That means that for all data sets larger than that point, program A will compute the answer faster than program B. Because we typically don't know what the constants C and K are, we don't know what this threshold is, we just know that it exists.

Time complexity is calculated for shell scripts the same way it is for any language. The only difference is that some of the statements are more opaque, and you may have to research their implementation, or make assumptions that they are implemented 'reasonably'. The awk script in the example you linked to is just a 'for' loop that touches each element of the input data exactly once. That means it almost certainly has a time complexity of O(n).

I say almost certainly, because it is theoretically possible that awk is doing something bizarre internally that would change the time complexity of the script. If you needed a guarantee, you could look at the awk source code and get a definitive answer. However, awk has been around for forty years and worked on by a lot of competent programers, so it's a pretty reasonable assumption that an awk 'for' loop that touches n items is going to have a time complexity of O(n).

On the other hand, it's a well known result from analysis of algorithms that no sort routine can have a time complexity better than O(n * log n). That means that the sort command can be treated as a black box, but we can be sure that its time complexity is worse than O(n log n). That means that if the input data gets big enough, there will be some point in which the awk script overtakes the sort and head script.

All of the above discussion is really only relevant if your script needs to handle arbitrarily sized data sets. Shell scripts are often written to carry out 'once only' chores on small data sets. In those cases it may not be worth thinking about a running time analysis. Who cares if the awk script solves the problem 2 seconds faster than the sort and head script if you have to invest 45 minutes in learning awk?*

*If your job requires you to solve problems like this all the time, then yes, of course you should learn awk, and probably three or four other scripting languages. However, that's more about optimizing your career, then about expediently solving the problem at hand.


问:
Bug fixing in Scrum, at the end of the project
We have a project that is reaching a deadline for the final production release. It's a fixed price project and up till now we've managed to work in 2 week iterations, doing regular releases to our "inner test environment" where BAs and people acting as POs from the client's side had a chance to have a look, give feedback etc.

We're now at the point where we have only 4 weeks to end this project and as we go to deploy the separate modules of this application to the client's environment, we've been struggling with a lot of bugs appearing.

We have some functionality to be done still but my question is - what is the best way to handle those bugs as they appear?

Since there's really a lot of them (due to project specifics, we didn't have the ability to test it on client's environment earlier) and it's impossible to estimate them up front + we have only 4 weeks till the end, does it make sense to keep Scrum or switch to Tech Lead dispatching work daily to the devs directly?

答:
I would go to some kind of kanban mode in this case. This is how I would handle this:

Create a backlog of bugs. Ask the PO to give them priorities, but do this together with the development team as they can indicate the impact a bug has. This will allow you to fix the most important issues first from a business point of view. (Of course you can help the PO to understand the consequences if a certain bug doesn't get fixed)

Let the team decide who fixes which bug. Like this developers can commit to get bugs fixed. This also allows the team to sit together and discuss the bugs.

Keep the bi-weekly demo. Show the bugs that are fixed. Let the team get the reward for the work they have done. Get feedback from the PO, because even the way bugs are fixed can be functionally not what is expected.

I cannot comment on the situation that you are in because I don't know all the specifics, but I strongly recommend you and your team to investigate how you can test earlier. I always try to get the complete cycle (from userstory to working code in production or at least a production like environment on the customers site) finished as soon as possible, even if the application we deliver says only 'hello world' at that time. It helps to limit the risk that you get in a situation where you end up in the situation that you are in now.


问:
tracking time for personal projects
Does everyone track time spent on personal projects? I feel that it is valuable to know how much time (and potential money) has been put into a hobby.

I am currently using harvest to track time.

答: If you're idea of a good time is counting and tracking things, then go for it. I'd rather get lost/zoned out/lose track of time in a hobby project. Chances are you would have to code completely different, track time, bill, take requests, if you wanted to sell it. Sounds too much like work. </p>

ContentLicense: CC BY-SA 2.5

Title: tracking time for personal projects


问:
How to fix poorly designed software?
I am working on large project solo as a hobby, and I made a mistake in the very beginning: I jumped right into programming without giving a second though to design. Now I am nearly 6 months in and things are starting to fall apart: I cant get anything done, the code is very inconsistant, and the whole thing is a mess. Ive definitely learned the importance of design in software development, but I dont know how one designs software to begin with. Are there any programs that can help with this? Or do I need to sit down with pen and paper for the next few weeks trying to work things out?</p>

答:
Things are falling apart and you are reflecting about it. You try to do it better next time. So the good news is: You are on the right way, and what you experience right now is called learning.

Let me tell you a story. Once ago, I was in a similar situation. There was a piece of code which suddenly became more complex as we (two unexperienced people) were able to handle it. The signs were similar to the ones you see: The code began to become brittle, and fixing one bug introduced two new bugs. In a nutshell, the whole thing started to develop kind of its own, uncontrollable life.

That's when we found out that unit testing can be a great tool. It was years ago before such things started to become mainstream, and it was the single insight that basically saved the project. We started writing test cases methodically, which made sure that we left the code in a better state after the next issue had been fixed. For every issue fixed and for every new feature, we added at least one test case to make sure everything worked and none of the identified and fixed problems do rise again their ugly head - and if so, we would at least be warned immediately.

The book I was reading at that time helped us with that insight. It was Steve Maguire's "Writing Solid Code". Sure, it is a little outdated in the details, but most of the principles in that book are still valid today, and not only for C programmers. The basic message is to do whatever you can do, and to use whatever tool is suitable to make your code rock-solid.

I jumped right into programming without giving a second though to design. [...] I cant get anything done, the code is very inconsistant, and the whole thing is a mess.

The best advice here is probably: Don't be afraid to throw code away and rewrite stuff that isn't working. It will take a little more time today, that's true, but you trade that against much larger benefits in the long run, because you don't have to deal with the mess any longer. Have your unit tests ready to check the expected outcome. Consider using TDD, it may be what you need to get the beast tamed. And last not least, test your error handling.

Ive definitely learned the importance of design in software development, but I dont know how one designs software to begin with.

That's something you should absolutely learn, both in theory and in practice. Try to study up good solutions in other people's code, for example Open Source projects are a very good source. Try to understand what the code does, maybe ask people whenever you don't understand it.

A good pattern and anti-pattern book may help as well (e.g. the GOF stuff), but you should try to apply patterns into practice to make sure you really understand how they work and what they are good for. Try to implement some of your code again with these principles in mind. Develop the ability to see the big picture behind what you are doing. How could you split that big ball of mud into small, re-usable pieces? What patterns can you identify? How to design the interface between these components? Then try it, practice and learn. Talk to people and let them review your stuff.

Title: How to fix poorly designed software?


问:
Difference between lexical scoping and passing variables in JS
There are several ways to reach the same end point in JS, for example:

```<code>(function (foo, bar) {
  return {run: function () {return foo(bar);}};
} (foo, bar));

(function (foo, bar) {
  return (function(foo, bar) { return {run: function () { return foo(bar); }}} (foo, bar))
} (foo, bar))

(function (foo, bar) {
  var Fizz = function (foo, bar)  { this.run = function () { return foo(bar); }; };
  return new Fizz(foo, bar)
} (foo, bar))
</code>```

Now for these, I know that one difference is that the prototype in the third case for the returned object would be different, but other than that, all three paths lead to an object which provides the same api. And since Javascript is a duct typed language, what a thing is is usually defined by its behavior.

Are there any other implications of choosing any one approach of the three that I am ignoring ?

答:
The only difference between the first two is the depth of the stack(though it's not outright impossible to optimize that away), since the only difference is the amount of scopes you are creating. Notice that you can continue creating as many scopes as you whim:

```<code>(function (foo, bar) {
        return (function(foo, bar) {
                return (function(foo, bar) {
                        return (function(foo, bar) {
                                return (function(foo, bar) {
                                        return (function(foo, bar) {
                                                return {run: function () { return foo(bar); }}
                                        } (foo, bar))
                                } (foo, bar))
                        } (foo, bar))
                } (foo, bar))
        } (foo, bar))
} (foo, bar))
</code>```

Behind the scenes, a JavaScript function keeps a pointer to it's lexical scope, so the more scopes you create the more deep you'll have to go up in the scope of the function you put in the run field of the object. Again, this might be optimized away and has no effect on the logic since JavaScript function objects do not provide you with a way to access that scope(though a JavaScript implementation might add such a field)

Body:
The only difference between the first two is the depth of the stack(though it's not outright impossible to optimize that away), since the only difference is the amount of scopes you are creating. Notice that you can continue creating as many scopes as you whim:

(function (foo, bar) {
        return (function(foo, bar) {
                return (function(foo, bar) {
                        return (function(foo, bar) {
                                return (function(foo, bar) {
                                        return (function(foo, bar) {
                                                return {run: function () { return foo(bar); }}
                                        } (foo, bar))
                                } (foo, bar))
                        } (foo, bar))
                } (foo, bar))
        } (foo, bar))
} (foo, bar))

Behind the scenes, a JavaScript function keeps a pointer to it's lexical scope, so the more scopes you create the more deep you'll have to go up in the scope of the function you put in the run field of the object. Again, this might be optimized away and has no effect on the logic since JavaScript function objects do not provide you with a way to access that scope(though a JavaScript implementation might add such a field)


问:
How to properly handle conditional dependencies in a factory?
Let's say you have a string $action run-time, that specifies which type of $object needs to be created: $dbobject, $memcacheobject, $fileobject, $xmlobject, etc. Assume also, that creation of an object is non-trivial and a $factory is required.

Obviously, different object types depend on different things, one on PDO, another on Memcache and so on. So how should I create an object in this conditions? I'm left with a few options:

Create a switch ($action) {} outside of a factory in order to call different subclasses of my factory, which will call it's type of object, for example: case 'db': $obj = new DBFactory($dbh); break;.

This is clearly bad, since this switch have to be present everywhere where the object is needed.

Have a switch ($action) {} inside some sort of a factory factory, which will do the code in #1. What should then the parent factory depend on, everything it might create? Since it might need database and memcache, in order to follow dependency injection rules you need to supply parent factory with both pdo and memcache instances, even if it won't use them. That also sounds pretty bad.

Another problem is that if I create another object type that depends on something new, then I have to add it's dependencies all over the code where theese factories are instantiated, which is a horizontal change which I should in fact be avoiding. That tells me there is some concept for this problem I don't see right now, therefore the question.

What I'm trying to point out is that even if only the $fileobject is needed, which does not depend on anything, the code that parses $action still needs all dependencies (which is an overkill) or it will instantiace some of them (which is against DI) or needs to reach out to global state/use singletons/registry, which is also considered a bad practice. What would then be a good practice in this situation?

答:
You can inject dependencies separately by introducing a registry of factories.

Split you big factory into smaller ones. Each factory only creates one type of the object, and their aggregation - registry, manages them and dispatches creation request. Only needed factories are created (and populated with dependencies) and a caller invokes registry instead of direct access to factory.

This way you can have dependencies only for factories (and type of objects) needed while still being able to use registry in a way you used to.

Beware of runtime failures due to missing factory types, you should ensure that no invocation of registry will request absent factory.


答:
Using mobile apps you can access native APIs to control the device.
For example "what is the buttery status?", to do it from a web site someone should write an API for you first. If you have the native app, all the APIs ready once the SDK released on the platform.

There are also hybrid apps, that are between websites and mobile apps, so you'll have an access to device features, such as PhoneGap.


答:
It is mostly because they need different GUI solutions, check for example windows 8 metro style, which is optimized for touchscreen (big buttons, etc...) and compare it with windows 7 which was designed for personal computer usage...

Mobile devices have different browsers, so it may happen, that they don't support a javascript or css feature you are using.

Mobile devices may have weak processors (because they should work with low energy consumption, because the energy density of the current accumulators is too low), so they cannot display for example a long list fast enough, while for personal computers it is usually not a problem. (By expensive devices this is no longer a problem nowadays.)


问:
java.util.HashMap lock on actual HashMap object compare to lock on object that encapsulate the HashMap
The below Javadoc is an snippet of HashMap documentation. Why authors would emphasize on putting a lock on the object that encapsulate a HashMap? Lock on the actual HashMap Object makes for sense.

Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method...

答:
Encapsulation is necessary here in order to restrict direct access of client code to methods of HashMap.

As for recommendation on what object to use as a lock, it serves a purpose to allow client code to iterate over the map in a thread safe way.

If you intend your map to be synchronized but lock on actual HashMap object, without hiding (encapsulating) it, then any code having access to that object would be able to invoke its unsynchronized methods and break synchronization. Purpose of encapsulation in this case is stated further in javadocs you refer: "to prevent accidental unsynchronized access to the map".

Documentation also refers to Collections.synchronizedMap as an example on how one is expected to synchronize:

Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map...

You see, synchronizedMap documentation reiterates the importance ("it is critical") for the client code not accessing "backing map" directly.

This documentation also further provides the example explaining why it is recommended to synchronize on the enclosing object:

It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views:

```<code>  Map m = Collections.synchronizedMap(new HashMap());
      ...
  Set s = m.keySet();  // Needn't be in synchronized block
      ...
  synchronized (m) {  // Synchronizing on m, not s!
      Iterator i = s.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }
</code>```

Failure to follow this advice may result in non-deterministic behavior.

You see, in the above code example, if client code would not have access to "known by convention" lock object, it would be impossible to properly synchronize: some other code could have used other lock to wrap the iteration which would lead to unsynchronized access.

Body:
Encapsulation is necessary here in order to restrict direct access of client code to methods of HashMap.

As for recommendation on what object to use as a lock, it serves a purpose to allow client code to iterate over the map in a thread safe way.

If you intend your map to be synchronized but lock on actual HashMap object, without hiding (encapsulating) it, then any code having access to that object would be able to invoke its unsynchronized methods and break synchronization. Purpose of encapsulation in this case is stated further in javadocs you refer: "to prevent accidental unsynchronized access to the map".

Documentation also refers to Collections.synchronizedMap as an example on how one is expected to synchronize:

Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map...

You see, synchronizedMap documentation reiterates the importance ("it is critical") for the client code not accessing "backing map" directly.

This documentation also further provides the example explaining why it is recommended to synchronize on the enclosing object:

It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views:

  Map m = Collections.synchronizedMap(new HashMap());
      ...
  Set s = m.keySet();  // Needn't be in synchronized block
      ...
  synchronized (m) {  // Synchronizing on m, not s!
      Iterator i = s.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }

Failure to follow this advice may result in non-deterministic behavior.

You see, in the above code example, if client code would not have access to "known by convention" lock object, it would be impossible to properly synchronize: some other code could have used other lock to wrap the iteration which would lead to unsynchronized access.


问:
What are the warning signs of impending doom to watch out for on a project?
Having worked on a failed project is one of the few things that most programmers have in common, regardless of language used, industry or experience.

These projects can be great learning experiences, soul-crushing disasters (or both!), and can occur for a multitude of reasons:

upper management change of heart

under-skilled / under-resourced team

emergence of superior competitor during dev cycle

over/under management

Once you've worked on a couple of such projects, is it possible to recognise at an early stage exactly when a project is doomed to fail?

For me, a big sign is having a hard & fast external deadline combined with feature creep. I've seen projects which were well planned out and proceeding right on schedule go horribly off the rails once the late feature requests started to roll in and get added to the final "deliverable". The proposers of these requests earned the nickname of Columbo, due to rarely leaving the room without asking for "just one more thing".

What are the warning signs you look out for that set off the alarm bells of impending doom in your head?

答: When non-technical managers start insisting on making technical decisions that they are in no way qualified to make.  Big, big red-flag!</p>


问:
How do you react to non-programmers with ideas of 'The Next Big Thing'?
Recently and quite often, people with no programming background come and say they have this great idea that can be the next big thing and that the idea(s) is worth a fortune by itself. Then as they know I'm a programmer, they ask me if I'm willing to "code it up" for them or find someone willing to do it for next to nothing.

Judging from the enthusiasm, it's like they're drunk on their idea and that that by itself is the most important thing, but they just need a programmer. My response to them, depending on my mood and their general attitude towards what we do, is something along the lines of:

"Having the core of an idea is one thing. Developing it to the point that it becomes a platform that changes the world in which it lives is another, and you're going to be willing to pay proportionately to how big you think your idea is worth."

Have you been approached by these business type entrepreneurs (with no technical/developer's knowledge) with such a proposal and how do you react to them?

答:
If they are only willing to pay next to nothing, that means they really don't believe in the idea.  They just want to try a long shot and see if they get lucky.

I usually respond with something along the lines of "you get what you pay for" for development services, and if they don't want to pay anything, they aren't likely to get much.

If they say they'll give you stake in the company, that's a bit different, then you have to evaluate how you feel it will do as the next big thing.


问:
Programming Language vs Markup Language vs Scripting Language
Up until now I don't know the KEY differences between these three. When someone asks me about this, I only tell them that C# is a programming language, HTML and XML are Markup Languages, and JavaScript and VBScript are scripting languages. But what are the key differences that distinguish them from one another?

答:
A markup language is a language which is used to represent structured data. For example, HTML enables to specify that some part of the document is a title or some other part is a list, by comparison to a flat text document.

Markup languages are not considered as programming languages¹.

The difference with programming languages is not always obvious. For example, XSLT is a Turing-complete language², but is based on XML which is a markup language.

Wikipedia itself makes important efforts to avoid qualifying XSLT as either a programming language or a markup language. It only says it's a declarative language, and that it uses “optimization techniques found in functional programming languages and database query languages”.

A scripting language is a programming language which is interpreted, rather than compiled, which means that scripting languages represent a subset of all programming languages.

It is not always obvious whether a programming language is a scripting language, like it's not always obvious whether a language is compiled or interpreted. For example, PHP may be compiled to intermediate bytecode³ and then ranslated into machine code by JIT compiler, while being still considered as scripting language⁴.

See also: Splitting Meta-Hairs by Steven Lott.

¹ “XML is not a programming language”. See XML in 10 points.

² “XSLT is a Turing-complete language” See XSLT on Wikipedia

³ “HHVM compiles Hack and PHP into an intermediate bytecode. This bytecode is then translated into x64 machine code dynamically at runtime by a just-in-time (JIT) compiler.” See HHVM.

⁴ Scripting languages in List of programming languages by type on Wikipedia.


答:
A markup language is used to describe data rather than logic. A typical use of them is to describe document formating, HTML is designed for this for example. But they are sometimes used as general data formats as well, XML is a markup language that is often used to just describe data.

The difference between programming and scripting languages is very fuzzy, both tend to be turing complete in that you can solve any computable problems with them. There are some general 'hints' that you can look for to see if a language is a programming language or not.

Scripting Languages are often interpreted rather than compiled, or at least provide the option of being interpreted.

Scripting Languages are often built to be fast to work with rather than fast to run.

Scripting Languages tend to come with very extensive standard libraries. Many programming languages do as well, but it is more optional for them.

But in the end, if a language is scripting language or programming language is more a matter of convention rather than any strict criteria. Even the hints above are just trends, you will find programming languages that fulfill all the criteria for a scripting language yet are still considered programming languages.

Title: Programming Language vs Markup Language vs Scripting Language

Body: Up until now I don't know the KEY differences between these three. When someone asks me about this, I only tell them that C# is a programming language, HTML and XML are Markup Languages, and JavaScript and VBScript are scripting languages. But what are the key differences that distinguish them from one another?


答:
If the person has a lot of domain experience, they may have more than just an idea. Now whether or not they're willing to make any type of commitment is another thing. Will they help you with design and testing and do they know anyone else in their situation that would buy it? Better yet, would they help you sell it to those people? Are they well liked in their industry?

Example: A person walks into a store and notices a clerk doing a paper and pencil inventory and thinks, "Wow, someone should program a smart phone app to take inventory." Another person says, "I've been running a store for 10 years and nobody has built a descent smart phone app that does XYZ and I've met a lot of other store owners at conventions..." This might be a person to take a little more seriously.

Title: How do you react to non-programmers with ideas of 'The Next Big Thing'?

Body:
Recently and quite often, people with no programming background come and say they have this great idea that can be the next big thing and that the idea(s) is worth a fortune by itself. Then as they know I'm a programmer, they ask me if I'm willing to "code it up" for them or find someone willing to do it for next to nothing.

Judging from the enthusiasm, it's like they're drunk on their idea and that that by itself is the most important thing, but they just need a programmer. My response to them, depending on my mood and their general attitude towards what we do, is something along the lines of:

"Having the core of an idea is one thing. Developing it to the point that it becomes a platform that changes the world in which it lives is another, and you're going to be willing to pay proportionately to how big you think your idea is worth."

Have you been approached by these business type entrepreneurs (with no technical/developer's knowledge) with such a proposal and how do you react to them?


答:
I understand it as saying that an "easy" way is to use the synchronized wrappers or make the class holding the map synchronized (I.e. using synchronized methods, which is what the wrappers does essentially).

But there are other ways, such as synchronising on the map itself if it is private and final, using a separate lock object or using a ConcurrentMap. I wouldn't overthink what is said here and simply apply usual concurrency patterns.

Title: java.util.HashMap lock on actual HashMap object compare to lock on object that encapsulate the HashMap

Body:
The below Javadoc is an snippet of HashMap documentation. Why authors would emphasize on putting a lock on the object that encapsulate a HashMap? Lock on the actual HashMap Object makes for sense.

Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method...


问:
Is backing up a MySQL database in Git a good idea?
I am trying to improve the backup situation for my application. I have a Django application and MySQL database. I read an article suggesting backing up the database in Git.

On the one hand I like it, as it will keep a copy of the data and the code in sync.

But Git is designed for code, not for data. As such it will be doing a lot of extra work diffing the MySQL dump every commit, which is not really necessary. If I compress the file before storing it, will git still diff the files?

(The dump file is currently 100MB uncompressed, 5.7MB when bzipped.)

Edit: the code and database schema definitions are already in Git, it is really the data I am concerned about backing up now.

答:
My two cents: I do not think it is a good idea. GIT does something like "storing snapshots of a set of files at different points in time", so you can perfectly use GIT for something like that, but that doesn't mean you should. GIT is designed to store source code, so you would be missing most of its functionality, and you would be trading a lot of performance for just a little bit of convenience.

Let me assume that the main reason why you are thinking about this is to "keep a copy of the data and the code in synch", and that this means you are worried that version 2.0 of your code needs a different database schema than version 1.0.  A simpler solution would be to store the database schema, as a set of SQL scripts with CREATE statements, along the source code in your Git repository.  Then, a part of your installation procedure would be to execute those scripts on a previously installed database server.

The actual contents of those just CREATE-d tables have nothing to do with the version of your source code. Imagine you install your software, version 1.0, on server A and on server B, which are used in different companies by different teams.  After some weeks, the contents of the tables will be very different, even though the schemas are exactly the same.

Since you want to back up the contents of the database, I would suggest to you that you use a backup script that tags the backup dump with the current version of the software that the dump belongs to. The script should be in the GIT repository (so that it has access to the source code version string), but the dumps themselves do not belong into a version control system.

EDIT:

After reading the original post that motivated the question, I find this an even more dubious idea.  The key point is that the mysqldump command transforms the current state of a DB into a series of SQL INSERT statements, and GIT can diff them to get only the updated table rows.

The mysqldump part is sound, since this is one of the backup methods listed in MySQL's  documentation. The GIT part is where the author fails to notice that database servers keep a transaction log in order to recover from crashes, including MySQL.  It is using this log, not GIT, that you should create incremental backups for your database.  This has, first and foremost, the advantage that you can rotate or flush the logs after recovery, instead of bloating a GIT repository into infinity and beyond ...


答:
Personally, I don't think it's a good idea to use a source control version system to store the backup files, because the GIT version control is designed for data files, not for binaries or dump files like a MySQL backup dump file. The fact that you can do it doesn't mean automatically that you should do it. Moreover, your repository, considering a new database backup for each new commit, will dramatically grow, using a lot of hard disk space and the performance of GIT will get affected, resulting in a slow source control system.
For me it's fine to execute a backup strategy and have always ready a backup file when you need to restore the database when something in your code goes wrong, but source control tools aren't made to store binary data.

For these reasons, I don't see any utility in storing the backup files for day 1 and for day 2, and then seeing the differences between the two backup files. It will require a lot of extra and useless work. Instead of using GIT to store database backups when you commit new code, store the database backups in a different path, separated by date and time, and insert in your code some reference to the new database backups created for each version, using the tags, as someone already suggested.

My final note about the database backups and GIT: A database administrator, when he needs to restore a database because some data has been lost, doesn't need to check the differences between the backup file for day 1 and the backup file for day 2, he needs just to know which is the last backup file that will allow him to restore the database, without any error and data loss, reducing downtime. Indeed, the task of a database administrator is to make the data available for recovery as soon as possible, when the system, for some reasons, fails. If you store the database backups in GIT, linked to your commits, you don't allow the database administrator to restore the data quickly, because your backups are limited to points in time that you stored in the GIT repository, and to reduce the downtime of the system, because the performance of your GIT repository will be dramatically reduced having a lot of data to store.

Then, I don't recommend to store the backups using GIT, use instead a good backup software solution (there are some of them here), that will provide more granularity and will allow you to keep your data safe and secure, and making your data recovery simple and fast in case of disasters.

Title: Is backing up a MySQL database in Git a good idea?

Body:
I am trying to improve the backup situation for my application. I have a Django application and MySQL database. I read an article suggesting backing up the database in Git.

On the one hand I like it, as it will keep a copy of the data and the code in sync.

But Git is designed for code, not for data. As such it will be doing a lot of extra work diffing the MySQL dump every commit, which is not really necessary. If I compress the file before storing it, will git still diff the files?

(The dump file is currently 100MB uncompressed, 5.7MB when bzipped.)

Edit: the code and database schema definitions are already in Git, it is really the data I am concerned about backing up now.


答:
Let me attempt to find a dividing line between these three types of language. Of course, there will be numerous exceptions and counterexamples, since this is just my opinion.

A markup language is used to control the presentation of data, like "represent these user names as a bullet list or as a table".

A scripting language is used to mediate between programs in order to generate data.  This is specially true of shell scripting languages like bash, but if you reflect about it, also Python or Perl came from the need to accomplish tasks in UNIX without writing a program in C. The program that you control most of the time in those languages is the interpreter of the language itself, which accomplishes general tasks for you. Other typical programs you interact with are database servers, or web servers.

Going back to the user list metaphor, in a scripting language you ask the database "give me all user names", then ask the web server "send this user list to this requester".

A programming language is used to transform data. It does so by creating CPU instructions that rewrite the input data into the output; hopefully, the desired output. Examples of transforming data is to compute a sum out of a number of addends, or solving a system of differential equations from a set of conditions, or writing and reading from a tree-like structure in a consistent manner given a sequence of possibly simultaneous queries.

Going back to the user list metaphor, in a programming language you write how to traverse a table of records, extract from each record the "name" field, and return all of them to the requester.

Note that scripting languages are a subset of programming languages i.e. a language may be both "scripting" and "programming": Python is regularly used to "mediate between programs", and also to "transform data". There are other languages like Java which are seldom used to "mediate between programs", not because this is impossible but because they are not designed to make this easy.  The key feature of a scripting language is that it can orchestrate other programs, just like a script gives the cue to an actor to start his part.


问:
How can I have a shell script log into a remote server and execute imqcmd there?
I want to write a shell script to connect to the remote server and execute a command (any linux command).

I am using linux and want to prepare a shell script which can login to the virtual directory server and execute a given command.

I.e. if I want to execute the imqcmd command, how can I do this?

答:
This is the direct ssh command for command execution it asks for password after you enter this command. Any way the below shell script shall work.

#!/bin/bash
ssh root@ipaddress yourcommand


问:
Automatically analyze excel files
I have to replicate a manual generation of a large number of excel files. I started to manually track the relations between cells ( files, formulas, etc). I also had a talk with the person which generates those files. For now I have a general understanding about how the excel files are generated, but "devil is in the details".

I assume that I can write a script which will generate the hierarchy between cells and files, but this might require the same effort as manually noticing the relations. Also, I'm afraid that I'm not too experienced and my app is more prone to error approach than a manual analyze.

How to handle this problem?
Do you know about an open source project which analyze the excel files in a recursive mode following the formulas ?

答:
Write a lot of tests.  Use the manual output from the people who have been generating those files up to now as the results that must come out of your tests; on the other hand, present very simple situations to those people and collect their results as the first tests on which you start developing. (I can't emphasize enough the words "very simple situations").

The more tests you write, the more confident you will be that all output from your app matches what is expected from it. If some output is still wrong, then you did not write all possible tests, or the expected behavior was not completely detailed to you.

Let me remark this is a common situation in software development, because software is very good at repeating instructions, but precisely this amount of repetition can make you 'blind' as a human analyst, i.e. you can't keep an overview perspective of the results. Testing is the best technique to regain that overview.

Title: Automatically analyze excel files


问:
Is it important to obfuscate C++ application code?
In the Java world, sometimes it seems to be a problem, but what about C++? Are there different solutions?

I was thinking about the fact that someone can replace the C++ library of a specific OS with a different version of the same library, but full of debug symbols to understand what my code does. Is it a good thing to use standard or popular libraries?

This can also happen with some dll library under Windows replaced with the "debug version" of that library. Is it better to prefer static compilation? In commercial applications, I see that for the core of their app they compile everything statically and for the most part, the DLLs (dynamic libraries in general) are used to offer some third party technologies like anti-piracy solutions (I see this in many games), GUI library (like Qt), OS libraries, etc.

Is static compilation the equivalent to obfuscation in the Java world? In better terms, is it the best and most affordable solution to protect your code?

答:
Thinking about this, there are several different types of obfuscation. Let's start with obfuscation of source code, which is a complete waste of time; it's hard enough to understand without that! So let's instead focus on obfuscation of the delivery package, of how the code gets delivered to the user.

Minor obfuscation

Minor obfuscation exists to prevent the casual user from poking their fingers in and breaking things easily. It doesn't keep out the determined hacker, but it does have value in helping to ensure that the things you are asked to support are what you've actually delivered. The level of protection required for this sort of thing is really quite low; the delivery package merely has to not look readable and editable (without specialist tools) and that's quite good enough.

Javascript minification is an example of this, though it's not marketed as such. Nobody in their right mind would want to read and edit a minified JS file, even if it is quite technically possible to do so if you're determined/persistent enough.

Similarly with delivering Java applications. Merely packaging the code into an executable JAR will stop the majority of foolishness, even though it's got all the force of a polite “Please Keep Off The Grass” sign in a city park.

Even when delivering C++ code, stripping the unnecessary symbols from the executable will be enough to qualify as minor obfuscation. The key is that it is awkward to read the result as a user, but not a problem to execute it as a computer.

Major obfuscation

Major obfuscation is keeping the determined and knowledgeable user out. It's also a total losing game; if a computer can execute it, a person can pick it apart and work out what it does. The closest you could get would be to make the program decrypt itself continually, transforming what it does at one time into a completely different thing that it does at another time. Creating such thing would be rather difficult and still wouldn't keep a really good hacker out (though they would be really quite cross with you by the end of it at the amount of effort required to decrypt all that self-modifying code).

It's much better to think in terms of other solutions. For example, you could keep the “crown jewels” of the code on servers that you control and only permit service calls to it, making the client an essentially free giveaway that is a front-end to the valuable bits. Or you could go the more contract/legal route, and only hand over executables to organisations that formally agree to not poke around inside your code or compensate you if they do so (so that'd be some sort of NDA). The aim would be to create a strong incentive for the hacker to not hack, and for the users to keep the code away from any hackers not bound by the agreement.

But you must not assume that your code can never be cracked. With virtualisation, any program state of an execution can be examined and tracked, and anything that tries to defeat that (e.g., clock tracking to an external time source) will be much more likely to cause problems for legitimate users than hackers. (See the history of DRM for how even very determined publishers of information can't keep their systems secure once the code is in the hands of their opponents.) It's much better to focus on actually making legit users happy. The losses from the occasional crack will be as nothing compared to the extra money brought in through satisfying customers properly.


答:
I ask leading questions, try to figure out how much of their idea they actually thought through and whether or not they understand how much work will be involved or if they think of programmers as just a commodity. To a lot of people software development is magic and they treat it as a minor task item. After all, some kid wrote Facebook, so it can't be that difficult, can it?

If they show that they understand the problem domain, I would take them seriously, otherwise not so much.


问:
How to represent a tree structure in NoSQL
I'm new to NoSQL and have been playing around with a personal project on the MEAN stack (Mongo ExpressJs AngularJs NodeJs). I'm building a document editor of sorts that manages nodes of data. Each document is actually a tree.

I have a CRUD api for documents, to create new trees and a CRUD api for nodes in a given document.

Right now the documents are represented as a collection that holds everything, including nodes. The children parent relationship is done by ids. So the nodes are an map by id, and each node has references to what nodes are their children. I chose this "flat" approach because it is easier to get a node by id from a document.

Being used to having a relation table between nodes and documents, a relation table between nodes and children nodes I find it a bit weird that I have to save the entire "nodes" map each time I update a node.

Is there a better way to represent such a data type in NoSQL?

答:
You have a number of options when representing a tree structure with MongoDB. Here are five "patterns" that you could apply (link to details at the end).

The Child References pattern stores each tree node in a document; in addition to the tree node, document stores in an array the id(s) of the node’s children.

The Parent References pattern stores each tree node in a document; in addition to the tree node, the document stores the id of the node’s parent.

The Array of Ancestors pattern stores each tree node in a document; in addition to the tree node, document stores in an array the id(s) of the node’s ancestors or path.

The Materialized Paths pattern stores each tree node in a document; in addition to the tree node, document stores as a string the id(s) of the node’s ancestors or path.

The Nested Sets pattern stores each tree node in a document; in addition to the tree node, document stores the id of node’s parent, the node’s initial stop in the left field, and its return stop in the right field.

This tutorial in the MongoDB tutorial discusses the pros and cons of each pattern:
http://docs.mongodb.org/manual/tutorial/model-tree-structures/


问:
One method with many behaviours or many methods
This question is quite general and not related to a specific language, but more to coding best practices.

Recently, I've been developing a feature for my app that is requested in many cases with slightly different behaviours.

This function send emails , but to different receivers, or with different texts according to the parameters. The method signature is something like

```<code>public static sendMail (t_message message = null  , t_user receiver = null , stream attachedPiece = null)
</code>```

And then there are many condition inside the method, like

```<code>if(attachedPiece != null)
{
}
</code>```

I've made the choice to do it this way (with a single method) because it prevents me to rewrite the (nearly) same method 10 times, but I'm not sure that it's a good practice.

What should I have done? Write 10 sendMail method with different parameters?

Are there obvious pros and cons for these different ways of programming?

Thanks a lot.

答: Yes, it's a good practice, but if you don't feel comfortable with it you might want to consider a fluent interface. That way, you'll be able to put the different behaviours in different methods, but instead of writing a method for each combination of arguments you only need to write a method for each argument.</p>


答:
is the attachment rarely missing? If so do as you've done. If it's rarely there, make a method without the attachment parameter that calls this method. No need to recode everything a dozen times.

Say with 3 parameters, 1 of which are almost always there, the third rarely, you'd get something like:

```<code>method1(param1, param2, param3) {
  // full implementation
}

method1(param1, param2) {
  method1(param1, param2, null);
}
</code>```

Title: One method with many behaviours or many methods

Body:
This question is quite general and not related to a specific language, but more to coding best practices.

Recently, I've been developing a feature for my app that is requested in many cases with slightly different behaviours.

This function send emails , but to different receivers, or with different texts according to the parameters. The method signature is something like

```<code>public static sendMail (t_message message = null  , t_user receiver = null , stream attachedPiece = null)
</code>```

And then there are many condition inside the method, like

```<code>if(attachedPiece != null)
{
}
</code>```

I've made the choice to do it this way (with a single method) because it prevents me to rewrite the (nearly) same method 10 times, but I'm not sure that it's a good practice.

What should I have done? Write 10 sendMail method with different parameters?

Are there obvious pros and cons for these different ways of programming?

Thanks a lot.

Body:
is the attachment rarely missing? If so do as you've done. If it's rarely there, make a method without the attachment parameter that calls this method. No need to recode everything a dozen times.

Say with 3 parameters, 1 of which are almost always there, the third rarely, you'd get something like:

method1(param1, param2, param3) {
  // full implementation
}

method1(param1, param2) {
  method1(param1, param2, null);
}


答:
You should not store binary data in Git - especially database.

Code changes and database DML changes are totally different things.

MySQL and Oracle can write archive logs for the purpose of being restored to any point in time. Just backup those logs to somewhere safe and you will be okay.

To use Git to back up these "archive logs" does not make sense. Archive logs in production environments are rather heavy and should be removed after making regular full backups. Also it is useless to put them in git - those are already a repository in some sense.


问:
Fiction that includes BASIC programs
Back in what must've been the mid-80s, when every microcomputer included BASIC and Choose Your Own Adventure Books were really popular, there were some novels that had BASIC programs listed in the text, for you to type in and further enjoy.

For example, as I recall, in one of these books, the adventurers were infiltrating an undersea base (and eventually wiped the bad guy's data storage [I do believe it was a hard drive, even though they were uncommon then]).  One of the programs involved a game were you piloted a submarine and had to avoid or shoot sharks.  The code was in BASIC; minor changes (such as 'CLS' -> 'HOME') had to be made for your specific computer, and it used good old 40-column text-mode to display the action.  IIRC, the plot never depended upon the programs.

My question is, does anyone else recall these sorts of books?  Do you know any titles that I could look for, or of any online?  I am toying with the idea of writing a story like this (no, not in BASIC!), and would really like to see how it was done, back in the day.

答:
I remember those old books. I didn't actually own one, but I do remember flipping through the pages of one at the time and thinking that the programs were a bit too simplistic. (I was into BBC Basic with inline assembler at the time).

Ye Olde Infocomme Shoppe maintains a list of old book titles that may be of interest.

If you're thinking of writing something like this yourself, perhaps consider BBC Basic and then linking to an online BBC Basic interpreter or one for Windows which will allow you to provide simple to understand programs that can be typed in (that's important, no simple linking to an external website for instant download) and executed.

You'll be targeting a niche market, though.

Of course, if you publish this online you could have fun creating your own language and getting people to write programs for it through the browser. Your site could present problems that need to be solved using code before they can progress. This could be achieved using a similar process to the Mathematics Olympics where unit tests verify the solutions.

Title: Fiction that includes BASIC programs


问:
How do you handle domain logic that spans multiple model objects in an ORM?
So I know that business logic should be placed in the model. But using an ORM it is not as clear where I should place code that handles multiple objects.

E.g. let's say we have a Customer model which has a type of either sporty or posh and we wanted to customer.add_bonus() to every posh customer. Where would we do this? Do we create a new class to handle all this? If yes, where do we put it (alongside all the other model classes, but not subclass it from the ORM?)?

I'm currently using django framework in python, so specific suggestions are even more wanted.

答:
In general it depends on your architecture. Since you're using django, it's natural to walk the django path of fat models.

It's not clear from your example when you'd like to customer.add_bonus() but one approach you can take is modelling posh and sporty customers as different classes using inheritance. e.g.

```<code>class Customer(models.Model):
  pass

class PoshCustomer(Customer):
  def discount(self):
    self.add_bonus()

  class Meta:
    proxy = True

class SportyCustomer(Customer):
  def discount(self):
    pass

  class Meta:
    proxy = True
</code>```

Here we're using Django's proxy classes (https://docs.djangoproject.com/en/dev/topics/db/models/#proxy-models) to indicate that these customers are stored in the same database table, and only the behaviour is different.

Update:

Your comments below taken into consideration, I'd recommend not locking yourself into an architectural decision until you know more about your use cases. Continuing on your last comment, it sounds like you have behavior that's mostly split in two:

Identifying a customer group

Applying an action to the customers of the identified group.

I would probably model this as two different concepts, and stitch them together with use case-aligned services.

Identifying customer groups can be encapsulated using a custom model manager. (https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers). E.g.

```<code>class CustomerManager(models.Manager):
  def made_purchase_in(self, timespan):
    return self.filter(last_purchase__in=timespan)
</code>```

The actions could be modelled anyway you want, e.g. as methods on the Customer or as classes or functions that accepts customers as parameters:

```<code>class Customer(model.Model):
  ..
  def remind(self, email):
    self.send_mail(email)
</code>```

or

```<code>class RegularCustomerReminder:
  def __init__(self, customers):
    self.customers = customers

  def remind(self):
    for customer in self.customers:
      customer.send_email(self.make_reminder())
</code>```

And an example service:

```<code>class CustomerPromotionService:
  def send_promotion(self):
    customers = Customer.objects.made_purchase_in(timespan.LAST_30_DAYS)
    reminder = RegularCustomerReminder(customers)
    reminder.remind()
</code>```

Body:
In general it depends on your architecture. Since you're using django, it's natural to walk the django path of fat models.

It's not clear from your example when you'd like to customer.add_bonus() but one approach you can take is modelling posh and sporty customers as different classes using inheritance. e.g.

class Customer(models.Model):
  pass

class PoshCustomer(Customer):
  def discount(self):
    self.add_bonus()

  class Meta:
    proxy = True

class SportyCustomer(Customer):
  def discount(self):
    pass

  class Meta:
    proxy = True

Here we're using Django's proxy classes (https://docs.djangoproject.com/en/dev/topics/db/models/#proxy-models) to indicate that these customers are stored in the same database table, and only the behaviour is different.

Update:

Your comments below taken into consideration, I'd recommend not locking yourself into an architectural decision until you know more about your use cases. Continuing on your last comment, it sounds like you have behavior that's mostly split in two:

Identifying a customer group

Applying an action to the customers of the identified group.

I would probably model this as two different concepts, and stitch them together with use case-aligned services.

Identifying customer groups can be encapsulated using a custom model manager. (https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers). E.g.

class CustomerManager(models.Manager):
  def made_purchase_in(self, timespan):
    return self.filter(last_purchase__in=timespan)

The actions could be modelled anyway you want, e.g. as methods on the Customer or as classes or functions that accepts customers as parameters:

class Customer(model.Model):
  ..
  def remind(self, email):
    self.send_mail(email)

or

class RegularCustomerReminder:
  def __init__(self, customers):
    self.customers = customers

  def remind(self):
    for customer in self.customers:
      customer.send_email(self.make_reminder())

And an example service:

class CustomerPromotionService:
  def send_promotion(self):
    customers = Customer.objects.made_purchase_in(timespan.LAST_30_DAYS)
    reminder = RegularCustomerReminder(customers)
    reminder.remind()


答:
Before you lose any data, let me try to introduce a sysadmin perspective to this question.

There is only one reason we create backups: to make it possible to restore when something goes wrong, as it invariably will. As such, a proper backup system has requirements that go far beyond what git can reasonably handle.

Here are some of the issues I can foresee with trying to backup your database in git:

The repository will grow dramatically with every "backup". Since git stores entire objects (albeit compressed) and then diffs them later (e.g. when you run git gc), and keeps history forever, you will have a very large amount of data stored that you don't actually need or even want. You might need to limit the amount or retention period of backups you do to save disk space or for legal reasons, but it's difficult to remove old revisions from a git repo without a lot of collateral damage.

Restoring is limited to points in time that you have stored in the repository, and since the data is so large, going back more than a trivial amount of time may be slow. A backup system designed for the purpose limits the amount of data stored while potentially providing more granularity, and provides faster restores, reducing downtime in the event of a disaster. Database-aware backup solutions (example) can also provide continuous backup, ensuring that not a single transaction is lost.

Commits are likely to be slow as well, and get slower as the database grows. Remember that git is essentially a key-value data store mapped onto a filesystem, and thus is subject to the performance characteristics of the underlying filesystem. It is possible for this length of time to eventually exceed the backup interval, and at that point you can no longer meet your SLA. Proper backup systems also take longer to backup as the data grows, but not nearly so dramatically, since they will automatically manage their own size based on the retention policy you will have configured.

Despite the fact that there are apparently several interesting things you can do with a database dump if you put it into git, overall I can't recommend it for the purpose of keeping backups. Especially since backup systems are widely available (and many are even open source) and work much better at keeping your data safe and making it possible to recover as quickly as possible.


问:
Use unnamed object to invoke method or not?
If I have a class with only only public method. When I use this class, is it good to use unnamed object to invoke its method?

normal:

```<code>TaxFileParser tax_parser(tax_file_name);
auto content = tax_parser.get_content();
</code>```

or unnamed object version:

```<code>auto content = TaxFileParser(tax_file_name).get_content();
</code>```

Because I've told that we should avoid temporary as possible. If tax_parser object is used only once, can I call it a temporary and try to eliminate it?

Any suggestion will be helpful.

答:
The temporary object is still created, whether you gave it a name or not. Your two lines are equivalent.

If there is only one public method, I'd ask why this is even a class. Can it be handled by being a function with tax_file_name as parameter? Then you would not have an otherwise useless temporary object.

```<code>class TaxFileParser
{
   public:
     static whatever_t get_content(const std::string& file)
     {
       return result_from_file_in_question();
     }
};

auto content = TaxFileParser::get_content(tax_file_name);
</code>```

Body:
The temporary object is still created, whether you gave it a name or not. Your two lines are equivalent.

If there is only one public method, I'd ask why this is even a class. Can it be handled by being a function with tax_file_name as parameter? Then you would not have an otherwise useless temporary object.

class TaxFileParser
{
   public:
     static whatever_t get_content(const std::string& file)
     {
       return result_from_file_in_question();
     }
};

auto content = TaxFileParser::get_content(tax_file_name);


答:
There is a downside to having all permutations on the parameters in the same method, since you will end up with a high cyclomatic complexity, i.e. there are many paths to traverse your logic, and this is hard to test and reason about.  Every time you add a null-able parameter, you increase this complexity by a factor of 2 and it is recommended not to exceed 10, except for rare situations.

The simplest solution to avoid cyclomatic complexity is to refactor into smaller methods. Let me use Python as pseudocode for this simple example:

class MyApp():
    def send_mail(message=None, receiver=None, attachedPiece=None):
        if message:
            # ... do something ...
        if receiver:
            # ... do another thing ...
        if attachedPiece:
            # ... attach to message ...
```

<p>should be rewritten into:</p>

<pre class="lang-py prettyprint-override"><code>class MyApp():
    def send_mail(message=None, receiver=None, attachedPiece=None):
        process_body(message, receiver)
        attach_piece(attachedPiece)
    def process_body(message, receiver):
        if message:
            # ... do something ...
        if receiver:
            # ... do another thing ...
    def attach_piece(attachedPiece):
        if attachedPiece:
            # ... attach to message ...
</code>```

Now instead of a single method with complexity 8, you have two methods of complexities 4 and 2. This is not repeating yourself, since code is not shared between the refactored methods. I grouped two of the conditionals in the original send_mail method into a single refactored method in order to show that this type of refactor does not necessarily lead to one small method for each of the original parameters.

Body:
There is a downside to having all permutations on the parameters in the same method, since you will end up with a high cyclomatic complexity, i.e. there are many paths to traverse your logic, and this is hard to test and reason about.  Every time you add a null-able parameter, you increase this complexity by a factor of 2 and it is recommended not to exceed 10, except for rare situations.

The simplest solution to avoid cyclomatic complexity is to refactor into smaller methods. Let me use Python as pseudocode for this simple example:

class MyApp():
    def send_mail(message=None, receiver=None, attachedPiece=None):
        if message:
            # ... do something ...
        if receiver:
            # ... do another thing ...
        if attachedPiece:
            # ... attach to message ...

should be rewritten into:

class MyApp():
    def send_mail(message=None, receiver=None, attachedPiece=None):
        process_body(message, receiver)
        attach_piece(attachedPiece)
    def process_body(message, receiver):
        if message:
            # ... do something ...
        if receiver:
            # ... do another thing ...
    def attach_piece(attachedPiece):
        if attachedPiece:
            # ... attach to message ...

Now instead of a single method with complexity 8, you have two methods of complexities 4 and 2. This is not repeating yourself, since code is not shared between the refactored methods. I grouped two of the conditionals in the original send_mail method into a single refactored method in order to show that this type of refactor does not necessarily lead to one small method for each of the original parameters.


答:
Placing an object in a variable because you've been "told that we should avoid temporary as possible" is like using static variables because you've been told singletons are bad - you are not really avoiding the problem, only the code smell that comes with the most common form of the problem.

Like nvoigt says, if you want to avoid that problem altogether you should change the method to something that doesn't create objects. However, it might not be possible(maybe you can't modify the object's source code?) or reasonable(maybe the class has other methods, and you don't want to mess up the design) to do so. In that case - I believe the first approach(unnamed temporary object) is better.

The main difference between the two approaches is when the destructor of the temporary object will be called. If it's a named temporary object, it'll be called at the end of the scope. If it's an unnamed temporary object, it'll be called after the call to get_content(), when the object is no longer needed.

I've played a bit with objdump and found out that if you put only these two(or one) line in a function, the only difference between the two is that in the unnamed version the object is destroyed before the result of get_content is moved to content while in the named version it happens afterwards.

However, when I added an instruction to print content after those lines, things get a bit more complicated. The named version produced the code for calling the destructor twice - once at the end of the function, and once at a later point that never gets executed! Or, should I say, never gets executed in the regular flow of the function.

I'm not that familiar with how C++ works assembly-wise, but I assume this has something to do with exceptions handling - when the printing method(well, operator...) throws an exception, the stack unwinding mechanism needs to be able to call the destructors, so it uses that code.

To test this assumption, I've added a throw statement to get_content, and as I expected now the unnamed version also got that code segment, since now it can also get an exception before the normal destruction takes place. But still - gcc managed to optimize that away when it wasn't needed!

So, in conclusion - choose the unnamed version! Modern compilers are very smart but they can't read your mind, so the cues you provide them about your intentions the better job they can do. Using the unnamed version cues the optimizer that you only need the object for get_content and it's free to dispose of it afterwise and optimize things that way. The more things happen between an object's creation and it's destruction the harder it is for the optimizer to reason about it's role.

Title: Use unnamed object to invoke method or not?

Body:
If I have a class with only only public method. When I use this class, is it good to use unnamed object to invoke its method?

normal:

```<code>TaxFileParser tax_parser(tax_file_name);
auto content = tax_parser.get_content();
</code>```

or unnamed object version:

```<code>auto content = TaxFileParser(tax_file_name).get_content();
</code>```

Because I've told that we should avoid temporary as possible. If tax_parser object is used only once, can I call it a temporary and try to eliminate it?

Any suggestion will be helpful.