问:
Why use other number bases when programming
My coworkers and I have been bending our minds to figuring out why anyone would go out of their way to program numbers in a base other than base 10.

I suggested that perhaps you could optimize longer equations by putting the variables in the correct base you are working with (for instance, if you have only sets of 5 of something with no remainders you could use base 5), but I'm not sure if that's true.

Any thoughts?

答:
One area where base 16 (hexadecimal) numbers are used very frequently is in specifying color, especially when using HTML/CSS for the web. The colors we use on digital displays are specified using a combination of 3 intensity values for 3 "base" colors (RGB - red, green, blue) that are blended together to create any of the 16 million displayable colors (using 24-bit color).

For example, full intensity green in hex would be 0x00ff00 and 65280 in decimal. Now imagine trying to "manually" mix a color in your head that has equal parts red and blue, say at half intensity, to create a nice purple :) In hex this would be written simply as 0x800080 while the decimal value for this would be 8388736. It gets even easier when working with shades of grey - 50% grey is 0x808080 (hex) and 8421504 (decimal), 75% is 0xC0C0C0 and 12632256, and so on.

Using hex is much more intuitive and anyone familiar with this use of color will immediately be able to "guess" the color just by looking at the hex value. It is also a lot less error prone to use if you need to use the same color multiple times (which is usually the case).

Check out any web page (and in particular the CSS) for a crazy amount of hex usage :D

NOTE: In CSS the hex values are written using a # prefix, for example: #00ff00 for green, and is also sometimes shortened to just three digits, such as #0f0 for green.


问:
algorithm to print the digits in the correct order
I've been trying to write an algorithm that will print separately the digits from an integer. I have to write it in Pseudocode. I know how to write an algorithm that reverse the digits.

```<code>digi(n):
  while n != 0:
    x = n % 10
    n = n // 10
    print (x)
</code>```

But I don't know how to write an algorithm to print the digits in the correct order. For example, the input is integer 123467 and the output is: 1 2 3 4 6 7. The numbers will be input from the user, and we cannot convert them to a string. I need help getting started on writing algorithms.

答:
The usual algorithm is to write a recursive function which recurses with n/10 and
then prints n%10.  Extra credit for correctly handling negative numbers.</p>

ContentLicense: CC BY-SA 3.0


问:
Using a random string to authenticate HMAC?
I am designing a simple webservice and want to use HMAC for authentication to the service.

For the purpose of this question we have:

a web service at example.com

a secret key shared between a user and the server [K]

a consumer ID which is known to the user and the server (but is not necessarily secret) [D]

a message which we wish to send to the server [M]

The standard HMAC implementation would involve using the secret key [K] and the message [M] to create the hash [H], but I am running into issues with this.  The message [M] can be quite long and tends to be read from a file.  I have found its very difficult to produce a correct hash consistently across multiple operating systems and programming languages because of hidden characters which make it into various file formats.  This is of course bad implementation on the client side (100%), but I would like this webservice to be easily accessible and not have trouble with different file formats.

I was thinking of an alternative, which would allow the use a short (5-10 char) random string [R] rather than the message for autentication, e.g.

```<code>H = HMAC(K,R)
</code>```

The user then passes the random string to the server and the server checks the HMAC server side (using random string + shared secret).

As far as I can see, this produces the following issues:

There is no message integrity - this is ok message integrity is not important for this service

A user could re-use the hash with a different message - I can see 2 ways around this

Combine the random string with a timestamp so the hash is only valid for a set period of time

Only allow each random string to be used once

Since the client is in control of the random string, it is easier to look for collisions

I should point out that the principle reason for authentication is to implement rate limiting on the API service.  There is zero need for message integrity, and its not a big deal if someone can forge a single request (but it is if they can forge a very large number very quickly).

I know that the correct answer is to make sure the message [M] is the same on all platforms/languages before hashing it.  But, taking that out of the equation, is the above proposal an acceptable 2nd best?

答:
So you want to rate limit users. You really don't need to encrypt the message, then. You need a balancer.

If this is due to concerns about high traffic, you better implement it outside your server and use sth like haproxy, lighttpd or nginx. Nginx by default limits requests to 50/minute per IP, and redirects to a customizable 503 page. If IP suits you as an identity, that will be enough.

If IP isn't good, you can accept only requests with a solid key, then use haproxy as the sole balancer, or maybe 3scale which also provides analytics.

If high traffic or DoS, DDoS attacks are a concern for you, you can also use an apache server with mod_evasive as part of your proxy.

Finally, to be honest, there are so many possibilities that the IP is not a trustable id (NAT, users changing their IPs, even MACs). Since your main problem appears to be a decent authentication solution, why don't you use OpenID and a balancer?

Edit:

Nginx has also a limit_proxy_xuid option, which can be up to 255 characters, to limit traffic.


问:
What is the best way of storing centralised data for (and from) translation?
We are starting the process of internationalizing our software. To do this we will be writing a series of filters to convert all of our different resource files (resx, text, java resource, etc) into a common format. We will then be outputting xliff files for translation. After translation the process will run in reverse.

We would like to keep the central store updated with any changes that developers make to the resource files ideally using ant during the build process, maintaining knowledge of which branch or version of the software is using which resources. We'd also like to keep track of which version of software the resources were taken from which are sent for translation, the dates when the files were sent and received back and also who translated them.

The idea then being we apply the translations to the correct branch, we can take diffs of the central store in future to send partial resources for translations and we can report on quantity of translation at a point in time.

We are considering storing all of this data within the svn repository as a tree of xliffs, perhaps branched in a mirror of the main code repository. Is this sufficient or would we be better off just using a database to maintain this information?

答:
I've found SVN (or other good source control system with branching and diff tools) to be really good for this (oh and don't forget the power of patch files).  For example, we use it to compare and contrast Jboss Application server configuration files across the various versions they release as well as our modifications.  Using branches and diff we can pretty quickly figure out that X, Y and Z changed in this point release and we can patch on our regular A & B patches.

But do take the time up front to design your branches and your workflow.  Test it out with small examples first, we had to go through a few iterations before we found a logical way of working.

ContentLicense: CC BY-SA 2.5


问:
How do operating systems… run… without having an OS to run in?
I'm really curious right now. I'm a Python programmer, and this question just boggled me: You write an OS. How do you run it? It has to be run somehow, and that way is within another OS?

How can an application run without being in an OS? How do you tell the computer to run, say, C, and execute these commands to the screen, if it doesn't have an OS to run in?

Does it have to do with a UNIX kernel? If so, what is a Unix kernel, or a kernel in general?

I'm sure OSes are more complicated than that, but how does it work?

答:
In the beginning there was no power in the CPU.

And the Man said "let there be power", and the CPU started to read from a given address in memory and execute the instruction that was present there. Then the next one and so on until the end of the power.

This was the boot up. Its task was to load another piece of software to gain access to the environment, where the main software was, and load it.

Finally, a friendly screen invited you to log on.


问:
Is there an established convention for separating Windows file names in a string?
I have a function which needs to output a string containing a list of file paths. I can choose the separation character but I cannot change the data type (e.g. I cannot return a List<string> or something like that).

Wanting to use some well-established convention, my first intuition was to use the semicolon, similar to what Windows's PATH and Java's CLASSPATH (on Windows) environment variables do:

```<code>C:\somedir\somefile.txt;C:\someotherdir\someotherfile.txt
</code>```

However, I was surprised to notice that ; is a valid character in an NTFS file name.

So, is the established best practice to

just ignore this fact (i.e. "no sane person should use ; in a file name and if they do, it's their own fault") or

is there some other established character for separating Windows paths or files? (The pipe (|) might be a good choice, but I have not seen it used anywhere yet for this purpose.)

答: | is one of the invalid filename/directory name characters and is frequently used as a separator, so it is a good choice for a separator of paths in a string.


答:
Surprisingly, but |, <, and other symbols from the link provided by Oded
are reserved characters only for CreateFile function and several other Windows APIs. Definitely, NTFS driver doesn't know about these restriction and allows to create file names with these symbols, which could be easily done for example in Linux using ntfs-3g driver+tools. In Windows also possible to create files with such names which would be not-deleted/readable/writable by ordinar API calls using low-level API or from kernel mode.

I suppose, the best alternative against using some esoteric separator would be using some serialization format like XML or json, which supported in many modern languages.

E.g. you data in json format will look like:

["C:\somedir\somefile.txt",C:\someotherdir\someotherfile.txt"]

Title: Is there an established convention for separating Windows file names in a string?

Body:
I have a function which needs to output a string containing a list of file paths. I can choose the separation character but I cannot change the data type (e.g. I cannot return a List<string> or something like that).

Wanting to use some well-established convention, my first intuition was to use the semicolon, similar to what Windows's PATH and Java's CLASSPATH (on Windows) environment variables do:

```<code>C:\somedir\somefile.txt;C:\someotherdir\someotherfile.txt
</code>```

However, I was surprised to notice that ; is a valid character in an NTFS file name.

So, is the established best practice to

just ignore this fact (i.e. "no sane person should use ; in a file name and if they do, it's their own fault") or

is there some other established character for separating Windows paths or files? (The pipe (|) might be a good choice, but I have not seen it used anywhere yet for this purpose.)


问:
What did programmers do before variable scope, where everything is global?
So, I am having to deal with seemingly archiac language (called PowerOn) where I have a main method, a few datatypes to define variables with, and has the ability to have sub-procedures (essentially void methods) that does not return a type nor accepts any arguements.  The problem here is that EVERYTHING is global.  I've read of these type of languages, but most books take the aproach "Ok, we use to use a horse and cariage, but now, here's a car so let's learn how to work on THAT!" We will NEVER relive those days".  I have to admit, the mind is struggling to think outside of scope and extent.

Well here I am.  I am trying to figure out how to best manage nothing but global variables across several open methods.  Yep, even iterators for for loops have to be defined globaly, which I find myself recycling in different parts of my code.

My Question:  for those that have this type experience, how did programmers deal with a large amount of variables in a global playing field?  I have feeling it just became a mental juggling trick, but I would be interested to know if there were any known aproaches.

答:
Don't know how they did.

But I think that modern OOP languages had a very similar problem regarding the naming collision.

The solution is adopting namespace. It is an abstract concept, but widely adopted by several implementation (Java packages, .NET namespace, Python modules).

If the language you are using has not too narrow limitation about naming lenght, then you can apply the namespace to a good variable naming.

So the variable name also represents the variable scope.

Try to define a naming pattern like this: order_detail_product_code, order_detail_product_unit_price. Or for the temporary counters or swaps: tmp_i, tmp_swap.


问:
Can higher-order functions in FP be interpreted as some kind of dependency injection?
According to this article, in object-oriented programming / design dependency injection involves

a dependent consumer,

a declaration of a component's dependencies, defined as interface contracts,

an injector that creates instances of classes that implement a given dependency interface on request.

Let us now consider a higher-order function in a functional programming language, e.g. the Haskell function

```<code>filter :: (a -> Bool) -> [a] -> [a]
</code>```

from Data.List. This function transforms a list into another list and, in order to perform its job, it uses (consumes) an external predicate function that must be provided by its caller, e.g. the expression

```<code>filter (\x -> (mod x 2) == 0) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</code>```

selects all even numbers from the input list.

But isn't this construction very similar to the pattern illustrated above, where

the filter function is the dependent consumer,

the signature (a -> Bool) of the function argument is the interface contract,

the expression that uses the higher-order is the injector that, in this particular case, injects the implementation (\x -> (mod x 2) == 0) of the contract.

More in general, can one relate higher-order functions and their usage pattern in functional programming to the dependency injection pattern in object-oriented languages?

Or in the inverse direction, can dependency injection be compared to using some kind of higher-order function?

答:
Yes, your example definitely represents a simple form of dependency injection.

It's not very impressive in that form - you can do the same in an OO language by having methods that take their dependencies as parameters (or classes that require them in their constructor). This just gets rather unwieldy as you pass around dependencies everywhere. Thus dependency injection containers which are responsible for constructing entire object graphs with all dependencies injected. I'm not sure what the FP equivalent would be.

Or in the inverse direction, can dependency injection be compared to using some kind of higher-order function?

I'd say so, yes. The problem that dependency injection tries to solve is the lack of flexibility when dependencies are hardcoded, which is what would happen if you had separate removeOddNumbersFromList, getAdminUsersFromList etc. functions.


问:
Where ORMs blur the lines between code and data, how do you decide what logic should be a stored procedure, and what should be coded?
Take the following pseudocode:

```<code>CreateInvoiceAndCalculate(ItemsAndQuantities, DispatchAddress, User);
</code>```

And say CreateInvoice does the following:

Create a new entry in an Invoices table belonging to the specified User to be sent to the given DispatchAddress.

Create a new entry in an InvoiceItems table for each of the items in ItemsAndQuantities, storing the Item, the Quantity, and the cost of the item as of now (by looking it up from an Items table)

Calculate the total amount of the invoice (e.g., shipping and taxes) and store it in the new Invoice row.

At a glance you wouldn't be able to tell if this was a method in my application's code, or a stored procedure in the database that is being exposed as a function by the ORM. And to some extent it doesn't really matter.

Now technically none of this is business logic. You're not making any decisions - just performing a calculation and creating records. However some may argue that because you are performing a calculation that affects the business (the total amount to be invoiced) that this isn't something that should be done in a stored procedure and instead should be in code.

So for this specific example - why would it be more appropriate to do one or the other? And where do you draw the line? Or does it even particularly matter as long as it's sufficiently well documented?

答:
Now technically none of this is business logic. You're not making any decisions - just performing a calculation and creating records.

How do you figure that this is not business logic?  I would argue that most developers agree that this is the very definition of business logic.  It encapsulates an application behavior that performs a service for a user or task to directly or indirectly further a business goal of creating invoices.  While simply fetching some data on criteria can be argued is not in fact business logic, calculating new data certainly is.

So where do Stored Procedures come into play?  This answer is not so clear and not everybody agrees on the proper role of Stored Procedures.  Not everybody even agrees that stored procedures should ever exist, arguing that they fundamentally blue the line between the data tier and business logic which should ideally exist solely in the application layer.  Others take the other hard line approach that stored procedures should contain as much business logic as possible and the application tier should be thin.

There is always of course the middle road when it comes to stored procedures.  For resource or time intensive tasks where a significant performance advantage can be had by joining records and performing calculations at the database tier, (Eg. analytics, data marts, reports, etc...) then a strong argument can be made for the importance and role of stored procedures in an application.


问:
Using Completed User Stories to Estimate Future User Stories
In Scrum/Agile, the complexity of a user story can be estimated in story points.  After completing some user stories, a programmer or team of programmers can use those experiences to better estimate how much time it might take to complete a future user story.

Is there a methodology for breaking down the complexity of user stories into quantifiable or quantifiable attributes?

For example, User Story X requires a rich, new view in the GUI, but User Story X can perform most of its functionality using existing business logic on the server.  On a scale of 1 to 10, User Story X has a complexity of 7 on the client and a complexity of 2 on the server.  After User Story X is completed, someone asks how long would it take to complete User Story Y, which has a complexity of 3 on the client and 6 on the server.  Looking at how long it took to complete User Story X, we can make an educated estimate on how long it might take to complete User Story Y.

I can imagine some other details:

The complexity of one attribute (such as complexity of client) could have sub-attributes, such as number of steps in a sequence, function points, etc.

Several other attributes that could be considered as well, such as the programmer's familiarity with the system or the number of components/interfaces involved

These attributes could be accumulated into some sort of user story checklist.

To reiterate:  is there an existing methodology for decomposing the complexity of a user story into complexity of attributes/sub-attributes, or is using completed user stories as indicators in estimating future user stories more of an informal process?

答:
```<code>> use (those) experiences to better estimate how much time
> it might take to complete a future user story.
</code>```

The Function_point_analysis fpa is not based on userstories
but it might be helpful to estimate once there is enough experinence.

The "attributes" or dimensons of fpa are outputs, inquiries, inputs, internal files, and external interfaces

Body:
> use (those) experiences to better estimate how much time
> it might take to complete a future user story.

The Function_point_analysis fpa is not based on userstories
but it might be helpful to estimate once there is enough experinence.

The "attributes" or dimensons of fpa are outputs, inquiries, inputs, internal files, and external interfaces


答:
Stored procedures are horrible places for any kind of business logic, and if an ORM masks a stored procedure this is simply for convenience.

Your entire hypothetical code above can be done without stored procedures, and should be. A database in terms of an ORM should mean that the database doesn't matter any more. It becomes just a persistence layer. A stored procedure should only be made if there is no other alternative to a specific problem.

This is of course a strict approach, but my experience in trying to work with both did nothing but frustrate me. I ended up moving all the logic to my code. Stored procedures shield you from the underlying data model, ORM exposes the model to you. I Just think they are two fundamentally different ways of working.

It's also easier and less ambiguous when you take a stricter approach to the problem.

Title: Where ORMs blur the lines between code and data, how do you decide what logic should be a stored procedure, and what should be coded?

Body:
Take the following pseudocode:

```<code>CreateInvoiceAndCalculate(ItemsAndQuantities, DispatchAddress, User);
</code>```

And say CreateInvoice does the following:

Create a new entry in an Invoices table belonging to the specified User to be sent to the given DispatchAddress.

Create a new entry in an InvoiceItems table for each of the items in ItemsAndQuantities, storing the Item, the Quantity, and the cost of the item as of now (by looking it up from an Items table)

Calculate the total amount of the invoice (e.g., shipping and taxes) and store it in the new Invoice row.

At a glance you wouldn't be able to tell if this was a method in my application's code, or a stored procedure in the database that is being exposed as a function by the ORM. And to some extent it doesn't really matter.

Now technically none of this is business logic. You're not making any decisions - just performing a calculation and creating records. However some may argue that because you are performing a calculation that affects the business (the total amount to be invoiced) that this isn't something that should be done in a stored procedure and instead should be in code.

So for this specific example - why would it be more appropriate to do one or the other? And where do you draw the line? Or does it even particularly matter as long as it's sufficiently well documented?


问:
Check-list to avoid vendor lock-in?
Is there a set of industry-approved rules to avoid vendor lock-in?

I mean, something one can show to a manager, or other decision maker, that is easy to understand and easily verifiable.

Are there any universally accepted set of rules, a checklist or set of conditions that helps detect and prevent vendor lock-in in an objective, measurable way?

Have any of you warned a manager about the risk of vendor lock-in during the initial stages of a project?

答:
In short, i would look for reviews and feedback about vendor and the product.

Technically speaking, vendor locking happens when project has tight coupling to the vendor(the third party product).

How to avoid that ? By having alternatives and researching one question about each alternative -  How hard is it to change out the solution for another one?

In addition to technical details of product that vendor promoting, it is very important to know the track of success/failure that other customers had with this vendor. It might look hard thing (Goggling, reading reviews, identifying how genuine is the review, etc..) to accomplish. However, there is credible rating system in US called BBB (Better business Bureau).

The records of US based companies in this independent Bureau are very useful and in 95% do reflect the reality. Thus, i will strongly advice to check with them as well.

Title: Check-list to avoid vendor lock-in?


答:
Sorry to be late, but I'll describe it as such:

The motherboard gets power.

Timing circuits start and stabilize if necessary, based solely on their electrical characteristics. Some newer devices may actually use a very limited microprocessor or sequencer.

It should be noted, alot[sic] of the things like "timing circuits start and stabilize if necessary" don't really happen in hardware anymore. A tremendous amount of that work is actually extremely specialized software running on very limited subprocessors/sequencers.

-jkerian at 5:20 on October 25

Power is given to the CPU and the RAM.

The CPU loads(based on its internal wiring) data from BIOS. On some machines, the BIOS may be mirrored to RAM and then executed from there but that is rare IIRC.

When turned on, x86-compatible CPUs start at the address 0xFFFFFFF0 in the address space...

-Micheal Steil, 17 Mistakes Microsoft Made in the Xbox Security System(archive)

The BIOS makes calls to hardware ports and addresses used by the motherboard for disk and other hardware IO and spins up disks, gets the rest of RAM working, among other things.

The BIOS code(by way of the CMOS settings, stored in hardware) uses low-level IDE or SATA commands to read the boot sector of each disk, in an order specified by the CMOS or a user override with a menu.

The first disk with a boot sector gets its boot sector executed. This boot sector is Assembly that has instructions to load more data from the disk, load a larger NTLDR, later stages of GRUB, etc.

Finally, the OS machine code is executed by the bootloader, directly or indirectly via chainloading loading a boot sector from an alternate or offset location.

You then get a friendly kernel panic, a suffocated penguin, or your disk grinds to a halt due to a head crash. =) In the alternative scenario, your kernel sets up process tables, in-memory structures, and mounts disks, loading drivers, modules, and a GUI or set of services(if on a server). Then, programs are executed as their headers are read, and their assembly is brought into memory and mapped accordingly.

Title: How do operating systems… run… without having an OS to run in?

Body:
I'm really curious right now. I'm a Python programmer, and this question just boggled me: You write an OS. How do you run it? It has to be run somehow, and that way is within another OS?

How can an application run without being in an OS? How do you tell the computer to run, say, C, and execute these commands to the screen, if it doesn't have an OS to run in?

Does it have to do with a UNIX kernel? If so, what is a Unix kernel, or a kernel in general?

I'm sure OSes are more complicated than that, but how does it work?


问:
Design Pattern Books, Papers or Resources for Non-Object Orientated Paradigms?
After viewing this video on InfoQ about functional design patterns I was wondering what resources are out there on design patterns for non-object orientated paradigms. There are plenty out there for the OO world (GOF, etc, etc) and for architecture (EoEAA, etc, etc) but I'm not aware of what's out there for functional, logic, or other programming paradigms. Is there anything? A comment during the video suggests possibly not - does anyone know better?

(By the way, by design patterns I don't mean language features or data structures but higher level approaches to designing an application - as discussed in the linked video)

答:
The best books I have come across that where not about OOP, but about programming in general is the Art of Computer Programming books (3 books when I bought them - now a 4th released) by Donald Knuth.

http://en.wikipedia.org/wiki/The_Art_of_Computer_Programming

A few years back I used one of his well taught data processing algorithms to speed up an import/merge of 100,000+ records from about a 20 minutes process down to about 30 secs.... I was just not doing it the right way!

And remember, before they were called Desigh Patterns, we called them Algorithms!


问:
Synchronous sockets with Node.js
I want to port an existing app from Java to Node.js - would like to seek your advise.

The java app is a multithreaded socket server where arbitrary number of clients can connect. The app runs on java executors. Each client is given a socket to connect to the app.

The client will connect to the socket server and there will be handshake. After handshake, the data will be transmitted between the client and server. The data transferred is in sequential order because the client sends the data over as such. And I.m able to control the thread that the client is connected on.

Now, Node.js does not have threads so I am confused on how to port the multithreaded Java socket server over. Any ideas?

答:
Node.js does not have threads, but it also does not have blocking sockets or blocking anything for that matter. It all runs in an event loop and dispatches callbacks as data arrive. So you need to restructure the code so that each socket call set a function to continue the processing. This is called "continuation passing style".

It means really big restructuring. Do you really have a compelling reason to spend that amount of time to port the application to a slower and less mature runtime?

Title: Synchronous sockets with Node.js


答:
There are many good answers but I wanted to add this: You mentioned you come from a Python background. Python is a ninterpreted (or "interpiled" or whatever, at least in typical CPython use cases) language. This means you have some other software (the Python interpreter) looking at the source and executing it in some way. This is a fine model and allows quite nice high level languages well abstracted from the actual hardware. Downside is you always need this interpreter software first.

Such interpreter software,typically, is written in a language which compiles to machine code, for instance C or C++. Machine code is what the CPU can handle. What a CPU can do is read some bytes from memory and depending on the byte values start a specific operation. So one byte sequence is a command to load some data from memory into a register, another sequence to add two values, another to store the value from a register back to the main memory and soon (a register is a special memory area which is part of the cpu where it can work best), most of these commands are quite low on that level. The human readable for of these machine code instructions is assembler code. This machine code, basically, is what what is stored in .exe or.com files on windows or inside Linux/Unix binaries.

Now if a computer is started up it is dumb, it has some wiring though which will read such machine code instructions. On a PC this usually (currently) is an EEPROM chip on the mainboard containing the BIOS (basicinput ouptput system), this system can't do much, it can ease access to some hardware etc and then do a key operation: go to the boot and copy the first few bytes (aka the master boot record, MBR) into memory and then tell the CPU "here, there's your program" the CPU will then treat those bytes there as machine code and execute it. Typically this is some operating system loader which will load the kernel with some parameters and then hand over the control to that kernel, which will then load all itsdriversto access all hardware, load some desktop or shell program or whatever and allow the user to login and use the system.


答:
There isn't really any way to "port" the application. The best approach would be to model the existing behavior then write a completely new Node application that conforms to that behavior.

JavaScript and Java are hugely different programming models. JavaScript isn't strongly typed and relies heavily on an asynchronous programming model. Trying to do a direct port would give you awful code and you would lose out on a lot the features that make Node good.

Title: Synchronous sockets with Node.js

Body:
I want to port an existing app from Java to Node.js - would like to seek your advise.

The java app is a multithreaded socket server where arbitrary number of clients can connect. The app runs on java executors. Each client is given a socket to connect to the app.

The client will connect to the socket server and there will be handshake. After handshake, the data will be transmitted between the client and server. The data transferred is in sequential order because the client sends the data over as such. And I.m able to control the thread that the client is connected on.

Now, Node.js does not have threads so I am confused on how to port the multithreaded Java socket server over. Any ideas?


问:
How to avoid javascript becoming spaghetti code?
I've done quite a lot of javascript over the years and I'm using a more object-oriented approach, specifically with the module pattern. Which kind of approach do you do use to avoid a bigger code-base to becoming spaghetti. Are there any "best approach"? </p>

答:
A separation between working code and deployed code is helpful. I use a tool to Combine and Compress my javascript files. So I can have any number of modules in a folder, all as separate files, for when I am working on that specific module. But for deploy time, those files get combined into a compressed file.

I use Chirpy http://chirpy.codeplex.com/ , which also supports SASS and coffeeScript.


问:
How would one go about reading memory from a process? Is it different by OS?
As an experienced web-developer, but a novice "low level" programmer, this stuff is sort of voodoo to me still.

I'm curious about how one would even begin to go about finding a memory block, and then reading it (for example, reading the timer in a game of mines)? Is this different by OS / OS version?

答:
It's entirely different by OS. Some won't allow you to do it at all, and you have to have some means of knowing where the data you want is in the target's memory space.

Here's how to do it on Linux:
https://unix.stackexchange.com/questions/6301/how-do-i-read-from-proc-pid-mem-under-linux


问:
Being productive during downtime
Lets say you are somewhere where coding and getting online isn't possible (On a busy flight, for example)what do you do to stay productive?

Things I would do are read whatever tech book I am currently slogging through and maybe doodle some UI stuff or workflows.  What else could I be doing?

答: I'd sleep so that I'm more rested and productive when I get to a more productivity enhancing environment.</p>

Title: Being productive during downtime


答:
This is a somewhat tricky and even complex question, in a sense that you can go quite deep with this. But to simplify things a bit:

Considering your example of "reading the timer in a game of mines":

First step is to find the memory address. This is usually done with a tool called memory editor(a debugger would do too, in some cases) which can use various methods to find the location of a variable in the process memory. Common way is to look for a certain value(for example the value of the timer) in target process' memory space, then change the target value(e.g. advance the timer) and look for matches again. This process pins down the candidates every iteration until there's only the exact variable left. Taking the address of that variable within the process' memory space is just a matter of a mouse click with a memory editor.

Second step is to modify the data in that particular address. How this is done depends on the operating system. With Windows, there's a WinAPI call named WriteProcessMemory which can be used to write pre-defined data to a given address within the target process' memory space. In our example, you would use this function to overwrite the timer variable in the target process with your own desired value, effectively changing the timer in the game.

In practice you'd have to find the target process' process ID, and then attach your rogue process to the target process to gain the ability to modify its memory space. This is quite trivial task, but does not contribute to answering the question so I've left it out as an exercise to the reader. ;)

Title: How would one go about reading memory from a process? Is it different by OS?

Body:
As an experienced web-developer, but a novice "low level" programmer, this stuff is sort of voodoo to me still.

I'm curious about how one would even begin to go about finding a memory block, and then reading it (for example, reading the timer in a game of mines)? Is this different by OS / OS version?


问:
Is this an example of a composition or aggregation?
Composition and aggregation both are confusion to me. Does my code sample below indicate composition or aggregation?

```<code>class A {
     public static function getData($id) {
         //something
     }

     public static function checkUrl($url) {
         // something
     }

 class B {
    public function executePatch() {
           $data = A::getData(12);
     }

    public function readUrl() {
           $url = A::checkUrl('http/erere.com');
     }

    public function storeData() {
            //something not related to class A at all
     }
  }
}
</code>```

Is class B a composition of class A or is it aggregation of class A? Does composition purely mean that if class A gets deleted class B does not works at all and aggregation if class A gets deleted methods in class B that do not use class A will work?

答:
What you coded made B an inner class of A.

This would let you end up with a composition, since an object of class A can't be created without an object of class B.

```<code>class A {
  private B _myObject;
  public constructor( B someObject ) {
    this._myObject = someObject;
  }
}
</code>```

also this

```<code>class A {
  private B _object;

  public constructor() {
    this._object = new B();
  }
}
</code>```

The difference between composition and aggregation is, that an object of a class, which aggregates the objects of another class can exist without those other class objects.

A object of a class, which is a composition of objects of other classes can't exist without the other class objects.

Body:
What you coded made B an inner class of A.

This would let you end up with a composition, since an object of class A can't be created without an object of class B.

class A {
  private B _myObject;
  public constructor( B someObject ) {
    this._myObject = someObject;
  }
}

also this

class A {
  private B _object;

  public constructor() {
    this._object = new B();
  }
}

The difference between composition and aggregation is, that an object of a class, which aggregates the objects of another class can exist without those other class objects.

A object of a class, which is a composition of objects of other classes can't exist without the other class objects.


答:
The Wikipedia page on object composition might be a good starting point. Aggregation is a more general form of composition, or composition is a stronger form of aggregation. Composition is effectively an ownership relationship, while aggregation is a "contains" relationship. In composition, the parts can not exist outside the thing that contains them, but they individual things can exist on their own as unique entities in aggregation.

Your code isn't an example of either aggregation or composition. Your class B does not contain an instance of class A. In addition, there's no way to say if class A can or can not exist without an instance of class B. It appears that your code sample indicates that class B is dependent on class A, but that's one of the weakest forms of relationship between two classes in that it is among the least descriptive of how they are structurally related.

Title: Is this an example of a composition or aggregation?

Body:
Composition and aggregation both are confusion to me. Does my code sample below indicate composition or aggregation?

```<code>class A {
     public static function getData($id) {
         //something
     }

     public static function checkUrl($url) {
         // something
     }

 class B {
    public function executePatch() {
           $data = A::getData(12);
     }

    public function readUrl() {
           $url = A::checkUrl('http/erere.com');
     }

    public function storeData() {
            //something not related to class A at all
     }
  }
}
</code>```

Is class B a composition of class A or is it aggregation of class A? Does composition purely mean that if class A gets deleted class B does not works at all and aggregation if class A gets deleted methods in class B that do not use class A will work?


问:
A better JavaDoc?
The standard JavaDoc template(Doclet) didn't change much until the recent JDK7. In my opinion, JDK7 template only made it even more difficult to browse. I looked at several third-party doclets, but none is what I want. So I started to design a new open source JavaDoc template from scratch to improve the usability. Here's something I thought about,

Searching. It should be done at client side to make it working on your local file system.

IDE-like interactive navigation. The standard JavaDoc uses two frames to show packages and classes separately, which is far less usable than an Eclipse-like navigator. What made it even worse is that if you clicked inside the right side frame, the package index and class index will not refresh accordingly. You have no idea where you are.

URLs. One drawback of the frames is that the URL do not change according to the frames. It's very frustrating that you can't send a JavaDoc URL to someone unless you explicitly check the URL of the individual frame.

Look and feel. You don't need to be a designer to see JavaDoc is ugly.
I want to make JavaDoc better and I want to make sure I do it right. The question for you to answer is,

What you expect a better JavaDoc should be?

答:
This isn't answering your question directly, but since you're keen to make a difference :-).

I'd contact Jonathon Gibbons on the compiler project in OpenJDK - he's looking for people to help overhaul the Javadoc system and to provide the sorts of features you're talking about.  It means your changes would go into Java itself!

Alternatively come and join the Adopt OpenJDK project and we'll get you started.

Title: A better JavaDoc?


答:
I need to take two asides in order to answer your question.  #1 is "Should Business Logic be performed in stored procedures, in application code, or both?"  #2 is about blurring the lines between code and data.  The answer to the first question depends on the environment you work in.

Use Stored Procedures When:

You have a crackerjack database team that does the design for your applications and the middle and front-end developers are very junior

The logic will be shared by multiple applications whose only commonality is that they use the same database.

The number of calls between application and database would make application code too slow and a stored procedure would be faster (and you need it to be that fast).

Don't Use Stored Procedures When:

Your application design is done by the middle guys

You don't want to be tied to a certain brand of database

Your DBA's are more like sysadmins than programmers - focused on keeping the database running rather than on application design

You have invested in a Service Oriented Architecture where all your code interacts with a service which in turn calls the database.

Your database stored procedure language is from the stone age (e.g. MySQL).

You can achieve the necessary performance without them.

Stored Procedure Summary

There has definitely been a move away from stored procedures in recent years.  Basically, put the important logic where the best people can take care of it.

Code vs. Data

Implementing a Set as a definition of what belongs in that set instead of as a container that holds things is using code instead of data.  Passing functions to other functions treats them like data.  Modeling decision-making as a tree structure treats the tree data like code.

Performing processing in the database vs. a web service, vs. the "back end", vs. the "front end", vs. in AJAX in someone's browser is still just code operating on data, albeit in different areas of your infrastructure.  I would say rather that it blurs the responsibilities between different layers of your architecture.

Object-Relational Mapping (ORM)

Yes, the goal of ORM is supposed to let you work in your chosen language, temporarily forgetting about the storage format.  ORM succeeds when it simplifies the problems of manipulating your abstractions.  A procedure that wraps a stored procedure is just another level of abstraction.  If you don't need to think about whether or not it's backed by a stored procedure, then ORM is succeeding.


问:
Do people in non-English-speaking countries code in English?
I've heard it said (by coworkers) that everyone "codes in English" regardless of where they're from. I find that difficult to believe, however I wouldn't be surprised if, for most programming languages, the supported character set is relatively narrow.

Have you ever worked in a country where English is not the primary language?

If so, what did their code look like?

答:
I'm from Valencia, Spain and the proper answer is "depends".

For most non-public/non-OS things i do a mix of English and Spanish in variable naming, but all my comments are in Spanish and I hope noone tells me to document/comment things in Valencian

But things change when you're publishing something under Open Source, since doing it in your local language restricts your potential users and collaborators to a smaller audience.

I can give you an example of language as a barrier: When I was running a WoW guild i began to look for alternatives to EQDKP and i found them, but most had it's documentation and comments in german so i lost any interest because relying on Google Translator wasn't an option for me, heck, some of them were awesome projects.

You also asked for a code example, as i told i tend to mix English and Spanish, but this snippet is so brief that i did not need any kind of mixture

```<code>function validaDNINIE(numero) {
// Eliminar todo lo que no sea número o letra
numero.replace(/[^0-9A-Z]/i, '').toUpperCase();
if (!numero.match(/((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)/i)) {
    return false;
}
// Comprobación de NIF
if (numero.match(/^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKE]{1}$/i)) {
    var letra = numero.charAt(numero.length-1);
    if (letra == "TRWAGMYFPDXBNJZSQVHLCKE".charAt(numero.substr(0, numero.length-1) % 23)) {
        return true;
    }
}
// Comprobación de NIE
// T00000000
if (numero.match(/^[T]/)) {
    if (numero == numero.match(/^[T]{1}[A-Z0-9]{8}$/)) return true;
}
// ZXY
if (numero.match(/^[XYZ]{1}/)) {
    // X = 88, asi conseguimos X=0;Y=1;Z=2
    var su = numero.charCodeAt(0)-88 + '' + numero.substr(1, numero.length-2);
    if (numero.charAt(numero.length-1) == "TRWAGMYFPDXBNJZSQVHLCKE".charAt(su % 23)) {
        return true;
    }
}
// Si hemos llegado aqui es que el numero no es valido
return false;
}
</code>```

Body:
I'm from Valencia, Spain and the proper answer is "depends".

For most non-public/non-OS things i do a mix of English and Spanish in variable naming, but all my comments are in Spanish and I hope noone tells me to document/comment things in Valencian

But things change when you're publishing something under Open Source, since doing it in your local language restricts your potential users and collaborators to a smaller audience.

I can give you an example of language as a barrier: When I was running a WoW guild i began to look for alternatives to EQDKP and i found them, but most had it's documentation and comments in german so i lost any interest because relying on Google Translator wasn't an option for me, heck, some of them were awesome projects.

You also asked for a code example, as i told i tend to mix English and Spanish, but this snippet is so brief that i did not need any kind of mixture

function validaDNINIE(numero) {
// Eliminar todo lo que no sea número o letra
numero.replace(/[^0-9A-Z]/i, '').toUpperCase();
if (!numero.match(/((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)/i)) {
    return false;
}
// Comprobación de NIF
if (numero.match(/^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKE]{1}$/i)) {
    var letra = numero.charAt(numero.length-1);
    if (letra == "TRWAGMYFPDXBNJZSQVHLCKE".charAt(numero.substr(0, numero.length-1) % 23)) {
        return true;
    }
}
// Comprobación de NIE
// T00000000
if (numero.match(/^[T]/)) {
    if (numero == numero.match(/^[T]{1}[A-Z0-9]{8}$/)) return true;
}
// ZXY
if (numero.match(/^[XYZ]{1}/)) {
    // X = 88, asi conseguimos X=0;Y=1;Z=2
    var su = numero.charCodeAt(0)-88 + '' + numero.substr(1, numero.length-2);
    if (numero.charAt(numero.length-1) == "TRWAGMYFPDXBNJZSQVHLCKE".charAt(su % 23)) {
        return true;
    }
}
// Si hemos llegado aqui es que el numero no es valido
return false;
}


答:
An application is given a range of memory by the OS.  Generally, the application has to request the memory but that functionality may be obscured to the programmer because of the language.

Languages like C allow for block requests for specific sizes, whereas other languages like C++, C#, and Java allow for requests through the use of keywords like new.  Each language has a number of ways to allocate memory, so this is just a brief overview.  Releasing the memory back to the OS may either be done explicitly or through a garbage collector.

Accessing the memory within the application depends upon how it was allocated.  C and C++ are the best known for using the concept of pointers to indicate / track where the memory is located.  Otherwise, memory access is handled through the class or variable that was created.

Most of the time, you don't have to worry about specific memory access within your program.  The language constructs and OS effectively obscure that concern for you.

Your example of a timer in a game is a great example of where you wouldn't need to worry about the underlying memory allocation.  You'll have a variable representing the timer, and you'll just read from the variable.

My answer is relevant for when you are writing the application, whereas zxcdw's answer is relevant for accessing memory that belongs to another application.  Your LMGTFY terms would be "debugging" and "reverse-engineering" to delve further into that topic.

Some additional reading:

The Wikipedia article on Computer Memory will give you a decent overview of things.

Then look into the Wikipedia article on Memory-mapped I/O to get a deeper understanding of the machinations that go on.

Finally, look at the article on Virtual Memory to get a better answer for how each OS will handle memory mapping a little bit differently than the others.


答:
As Stephen C. said, it's not just about starting up the Operating System, it's also about how it runs, interacts with the hardware and with the software on top of it.

I'll just add to his answer, that you may want to take a look at "The Elements of Computing Systems". It's a book and some tools, that explains how a computer, operating system and compilers interact. The unique thing about it is that it gives you the tools to very quickly develop your own operating system in a simulated environment, ignoring the many details required for a real one, so that you can grasp the concepts. It does a great job of letting you see the forest instead of the trees.

If you want to go into more details about how the Operating System interacts with the hardware, then check out Minix.


问:
In what order do people build websites?
For a website, you need to have an idea, you need to have a design and you need to have data, events and output, right? Whether it be a blog, web app, Q&A site, search engine...

Anyway, that is only slightly related to my question. My question is, when designing a website, providing I know the purpose, what should I start with?

Should I start with the CSS, design and look&feel using dummy data first, or should I program in the logic, events and output, and style it later? What is the design process of most websites that are built from the ground up?

答:
You say the requirements are known ahead of time, but I would argue that no software project of a useful complexity has requirements that are set in stone.

A website project is still a software project, and accordingly it benefits from agile practices. Develop iteratively, demonstrate working software to the customer every one or two weeks. Start with the minimum working product (not much more than a mock-up), and iteratively flesh out the missing functionality / content. Adapt to changing requirements, and enable the customer to decide what to build while you're building it. Avoid "production line" processes and encourage close collaboration and shared responsibility between team members instead. There's a free e-book that explains scrum practices.

In a scrum-style project you won't be building any of the different technical layers first. You'll build in-depth, delivering one working feature from the top to the bottom, and then moving on to the next (where the next feature might be a revised version of a previous feature). There is therefore no right answer on what to do first, front-end or back-end, because this might change from sprint to sprint and feature to feature.

Title: In what order do people build websites?


问:
What methods of requirements elicitation are suitable when I do not have direct access to the user base?
I am working on an application to create invoices. There are some features that are required based on the type of the application and are common to all invoice applications. However, we still need to determine what unique needs the user base might have. We do not have direct access to the users to obtain requirements or user stories. What techniques are most suitable for eliciting high-quality requirements from users when direct or frequent access is not possible?</p>

答:
Hire an expert from the field - this will offer you a view of someone working in the domain. Even if this is only one person, it is much better than you guessing how accounting works.

Find potential clients clients and go talk with them - this is where you sell department comes in action. They can try to go out and sell the product to a few early adopters in exchange for valuable information for you.

Release a version with less features and listen to what users want afterwards (do iterative releases in agile style). - This is how many agile projects work. They release often and in incremental updates. They release with a restricted set of features and let the product live in the real world. The feedback from these many and ordinary users is the most valuable.

Later Edit:

the backlog should not be prepared for the whole lifetime of the project. That is impossible, unless you are making something very simple.

let the backlog be minimal and add to it on the way


问:
Storing and analyzing rock climbing difficulty
I'm working on a WordPress plugin to manage rock climbing data, and I need to think of a way to store rock climbing grades from all of the different systems in a unified way. There are many different systems, all of which have some numerical system.

A comparison of all the systems: http://en.wikipedia.org/wiki/Grade_(climbing)#Comparison_tables

Is there some unified way that I can store and analyze these, or do I just need to assign numbers to them all and call it a day? My current plan is to save the score type and then assign each score a numerical value, which I can then use to compare and graph them.

答:
In short, your plan seems pretty sound.

For something like this you should always store data in a way which doesn't lose any of the original information. In this case, that would be (1) the grading system used and (2) the grade within that system. If you've stored the data in a non-lossy way, you can change how it's interpreted later on.

As soon as you optimise away some of the original data, you lose the ability to do that. For example, you decide to create a unified grade in which a Finnish '5' is the same as a French '4b'. Later, you decide that they ought to be treated as '4c's instead. Unless you've stored the original grading system used, there's no way for you to go through and change just the routes which were graded with the Finnish system.

Another thing to think about: for various reasons you'll sometimes get routes with more than one grade. You might want to think about being able to store more than one grade per route, perhaps with a note explaining the reasoning/conditions for each grade.


问:
What are the differences between server-side and client-side programming?

I've seen questions (mainly on Stack Overflow), which lack this basic
  knowledge. The point of this question is to provide good information
  for those seeking it, and those referencing to it.

In the context of web programming, what are the differences between Server-side programming and Client-side programming? Which languages belong to which, and when do you use each of them?

答:
Background

Web development is all about communication. In this case, communication between two (2) parties, over the HTTP protocol:

The Server - This party is responsible for serving pages.

The Client - This party requests pages from the Server, and displays them to the user. In most cases, the client is a web browser.

The User - The user uses the Client in order to surf the web, fill in forms, watch videos online, etc.

Each side's programming, refers to code which runs at the specific machine, the server's or the client's.

Basic Example

The User opens his web browser (the Client).

The User browses to http://google.com.

The Client (on the behalf of the User), sends a request to http://google.com (the Server), for their home page.

The Server then acknowledges the request, and replies the client with some meta-data (called headers), followed by the page's source.

The Client then receives the page's source, and renders it into a human viewable website.

The User types Stack Overflow into the search bar, and presses Enter

The Client submits that data to the Server.

The Server processes that data, and replies with a page matching the search results.

The Client, once again, renders that page for the User to view.

Programming

Server-side Programming

Server-side programming, is the general name for the kinds of programs which are run on the Server.

Uses

Process user input.

Compiles pages.

Structure web applications.

Interact with permanent storage (SQL, files).

Example Languages

PHP

Python

ASP.Net in C#, C++, or Visual Basic.

Nearly any language (C++, C#, Java). These were not designed specifically for the task, but are now often used for application-level web services.

Client-side programming

Much like the server-side, Client-side programming is the name for all of the programs which are run on the Client.

Uses

Make interactive webpages.

Make stuff happen dynamically on the web page.

Interact with temporary storage, and local storage (Cookies, localStorage).

Send requests to the server, and retrieve data from it.

Provide a remote service for client-side applications, such as software registration, content delivery, or remote multi-player gaming.

Example languages

JavaScript (primarily)

HTML*

CSS*

Any language running on a client device that interacts with a remote service is a client-side language.

*HTML and CSS aren't really "programming languages" per-se. They are markup syntax by which the Client renders the page for the User.

ContentLicense: CC BY-SA 4.0


问:
Considerations when designing a file type
I'm about to start writing a process for saving some data structure from code in to a file of some proprietary, as-yet-undefined type. However, I've never designed a file type or structure before.

Are there any things, generally speaking, that I should consider before starting my design?

Are there any accepted good practices here? Bad practices I should avoid?

Any absolute do's and don'ts?

答:
First, try to find a format that is close enough to what you are about to build. In general, it is better to use someone's format than to invent your own, even if the format appears to be slightly more complex than what you need1.

If you cannot find a suitable ready-made format, see if you can build your own on top of an existing general-purpose format, such as XML or Binary XML. This should be possible in nearly all cases when you are about to start a new file format. Text-based XML takes more space, but gives humans some measure of readability. However, if you find yourself using Base-64 encoding inside an XML file, that's a clear indication that you should have used a binary encoding instead.

As far as good and bad practices go, make sure that you do not "bake in" the hardware feature of your initial target platform into the design of your file format. Specifically, make sure that your numbers are stored in a format that can be read correctly on platforms with endianness that is different from that of the writer, and that your user-facing strings are stored in UNICODE.

Another good practice is to include a header from which it is possible to determine the type of your file in case its extension is missing or incorrect. It is a good idea to include a version of your file format in the header. This would let you change the format later, and stay backward-compatible.

If possible, do not make your format dependent on the specifics of the default serialization mechanism built into your platform. For example, binary-serialized Java objects do not make a good file format2.

Finally, decide if your files need to be streamable. This introduces additional complexity, because one should be able to interpret individual "frames" of your file in isolation. In cases when you need streamability, however, you should almost always be able to locate a suitable file format that already exists.

1 On the other hand, you should avoid formats that require extraordinary efforts to support the complexity that your application requires.

2 This does not mean, however, that you should not attempt to custom-integrate reading and writing of your new format with the serialization scheme of your platform, only that you should not rely on the default mechanisms of serialization.


答:
The first thing you should consider is whether you actually need a new format or if you can get by using an already existing format. Consider using SQLite; if you can adapt your needs to fit the RDBMS model, this could save you a lot of headaches. Also, consider using XML or JSON, this will save you from having to write your own parser.

If you do have to make your own format, the first consideration is whether you want a text format or a binary format. There are advantages to both. A text format is a big win for portability and has the advantage of being easier for a human to read or edit. A binary format could be more efficient, but it has a lot of portability problems that come with it. Don't be tempted to read bytes directly into variables, you'll regret it if you need to port the code to another platform.

Title: Considerations when designing a file type

Body:
I'm about to start writing a process for saving some data structure from code in to a file of some proprietary, as-yet-undefined type. However, I've never designed a file type or structure before.

Are there any things, generally speaking, that I should consider before starting my design?

Are there any accepted good practices here? Bad practices I should avoid?

Any absolute do's and don'ts?


答:
Based on your comment:

because in ORMs, stored procedures often look like functions the same
  as they would as if the functionality was written in code. Hence my
  comment about the lines being blurred because if you treat it as a
  black box, there's suddenly no difference

then yes, if someone else had to write the database access part of your application and all you had to deal with was the result, they're the same. I would only care if the other dev used an ORM or Stored Proc if I need to work with that code. Maintenance is an issue, as well as, knowing what is going on in the black box.

With an ORM, you know where the code is at. If you use a stored procedure, I don't know if you're manipulating the results in the proc itself or if your data access layer is doing something else with it.

ORM's blur the line between code and data

I would say code and databases And using the word "blur" has a negative connotation here. As if you're unsure what is going on. Blurring bothers me; putting it in a black box is the opposite of a distraction (assuming I know what was put in the black box which if that's the point of the question is just a matter of understanding the code used.). Out of site, out of mind.


问:
Execute code every hour
I need to create a web service that executes every hour.  It will be used to review data in a database and add alerts to a table in the same database if certain conditions are met/not met.    What we currently have is:

We have end devices that use Python to report to an Amazon Web Services (AWS) virtual server.  The AWS server takes that information and stores it in a MySQL database.  The AWS server is Linux running Django and Apache.  I need to be able to have some python code run every hour that verifies the data that has been stored by the end devices.  If certain conditions are not met then a record will be added to the alerts table in the database.

We originally contracted to have the above setup created.  I am new to Python, Django, and Apache.  However, I have already made several changes to the Python code that sends and also receives the data from the end devices.  I am a coder that is breaking into web programming.

Does anyone have any recommendations on how I can do this?

答:
How about making a cronjob, assuming you have shell access?

The cron daemon exists on virtually any UNIX-like system and schedules commands to run based on a description in a file called the crontab.

Each line of the file contains a set of fields to indicate the timepoints when a command shall be executed.

Your task could be either a standalone program that does the task you wish to accomplish or as another answer suggests, an invocation of a HTTP client like wget, curl or fetch to access a web resource that will perform the action.

If you've got limits for how long a request may take to serve, you might have to move the task into an offline script or program that doesn't run inside your web framework/server.

Title: Execute code every hour


答:
With Django on AWS, I'd look into Celery.

Celery adds asynchronous tasks and includes a scheduler, and on AWS you can configure Celery to use the Amazon Simple Queue Service as the broker (see Celery with Amazon SQS on Stack Overflow and this blog post on the subject).

You set up a Celery periodic task schedule and it'll run a configured task according to that schedule.

Advantage is that you can use the whole setup to run any asynchronous task, offloading heavy tasks from your web server to the Celery workers.

The light-weight alternative is to just set up a crontab job; you could even configure a route in your Django application to be called using curl or wget:

```<code>* 5 * * * curl http://username:password@hostname/route_to_job
</code>```

Title: Execute code every hour

Body:
I need to create a web service that executes every hour.  It will be used to review data in a database and add alerts to a table in the same database if certain conditions are met/not met.    What we currently have is:

We have end devices that use Python to report to an Amazon Web Services (AWS) virtual server.  The AWS server takes that information and stores it in a MySQL database.  The AWS server is Linux running Django and Apache.  I need to be able to have some python code run every hour that verifies the data that has been stored by the end devices.  If certain conditions are not met then a record will be added to the alerts table in the database.

We originally contracted to have the above setup created.  I am new to Python, Django, and Apache.  However, I have already made several changes to the Python code that sends and also receives the data from the end devices.  I am a coder that is breaking into web programming.

Does anyone have any recommendations on how I can do this?

Body:
With Django on AWS, I'd look into Celery.

Celery adds asynchronous tasks and includes a scheduler, and on AWS you can configure Celery to use the Amazon Simple Queue Service as the broker (see Celery with Amazon SQS on Stack Overflow and this blog post on the subject).

You set up a Celery periodic task schedule and it'll run a configured task according to that schedule.

Advantage is that you can use the whole setup to run any asynchronous task, offloading heavy tasks from your web server to the Celery workers.

The light-weight alternative is to just set up a crontab job; you could even configure a route in your Django application to be called using curl or wget:

* 5 * * * curl http://username:password@hostname/route_to_job


答:
In layman's words:

Here I will talk only about web programming.

Client side programming has mostly to do with the user interface, with which the user interacts. In web development it's the browser, in the user's machine, that runs the code, and it's mainly done in javascript, flash, etc. This code must run in a variety of browsers.

Its main tasks are:

validating input (Validation must be done in the server. A redundant validation in the client could be used to avoid server calls when speed is very critical.)

animation

manipulating UI elements

applying styles

some calculations are done when you don't want the page to refresh so often

The person in charge of front end programming must know:

javascript

css

HTML

basic graphic design

Ajax

maybe Flash

some 3rd party javascript libraries like JQuery

UI design

information design, etc.

Server side programming has to do with generating dynamic content. It runs on servers. Many of these servers are "headless". Most web pages are not static, they search a database in order to show the user updated personalized information. This sides interacts with the back end, like say, the database.

This programming can be done in a lot of languages:

PHP

Java and jsp

asp

Perl

Python

Ruby on Rails, etc.

This code has to do with:

Querying the database

Encode the data into html

Insert and update information onto the database

Business rules and calculations

The person in charge of server side programming must know:

some of the languages mentioned above

HTML

SQL,

linux/unix shell scripting

OOP

business rules, etc.

Title: What are the differences between server-side and client-side programming?

Body:
I've seen questions (mainly on Stack Overflow), which lack this basic
  knowledge. The point of this question is to provide good information
  for those seeking it, and those referencing to it.

In the context of web programming, what are the differences between Server-side programming and Client-side programming? Which languages belong to which, and when do you use each of them?


答:
Since your AWS instance runs Linux, you can probably accomplish this as a cron job.

You could take what I would term Drupal's cron approach which, in the case of Django, involves creating a controller to respond to a URL and then perform the action you want.

You then configure a cron task to curl the controller's URL, triggering your script.

This has the added advantage of being easily callable at any time from a URL: maybe an impatient manager wants a report generated from data from last 20 minutes.

Title: Execute code every hour


问:
What does "Human Readable" mean? Is it a misnomer?
Two examples spring to mind:

One of the reasons that .Net programmers are encouraged to use .config files instead of the Windows Registry is that .config files are XML and therefore human-readable.

Similarly, JSON is sometimes considered human-readable compared with a proprietary format.

Are human-readable formats actually readable by humans? In the example of configuration data:

The format doesn't change the underlying meaning of the information - in both cases, the data represents the same thing.

Both registry and .config file are stored internally as a series 0s and 1s. To that extent, the underlying representaion is equally unreadable by humans.

Both registry and .config file require a tool to read, format and display those 0s and 1s and convert them into a format that humans can read. In the case of configuration stored in the Windows Registry, this is a Registry Editor. In the case of XML it could be a text editor or XML reader. Either way, the tool makes the data readable, not the data format.

So, what is the difference between human-readable data formats and non-human-readable formats?

答: .config files are much easier to edit (for most people) than the Registry. It's easier to open a dedicated config file, find the relevant data, and make changes without opening the possibility of editing something that will affect other programs.</p>


问:
Is it reasonable for REST resources to be singular and plural?
I have been wondering if, rather than a more traditional layout like this:

```<code>api/Products
GET // gets product(s) by id
PUT // updates product(s) by id
DELETE // deletes (product(s) by id
POST // creates product(s)
</code>```

Would it be more useful to have a singular and a plural, for example:

```<code>api/Product
GET // gets a product by id
PUT // updates a product by id
DELETE // deletes a product by id
POST // creates a product

api/Products
GET // gets a collection of products by id
PUT // updates a collection of products by id
DELETE // deletes a collection of products (not the products themselves)
POST // creates a collection of products based on filter parameters passed
</code>```

So, to create a collection of products you might do:

```<code>POST api/Products {data: filters} // returns api/Products/<id>
</code>```

And then, to reference it, you might do:

```<code>GET api/Products/<id> // returns array of products
</code>```

In my opinion, the main advantage of doing things this way is that it allows for easy caching of collections of products. One might, for example, put a lifetime of an hour on collections of products, thus drastically reducing the calls on a server. Of course, I currently only see the good side of doing things this way, what's the downside?

答:
The downside is that the calling program also has to pluralize the resource name, which can be tricky for those client languages which don't have built-in pluralization mechanisms. If you leave it singular, it's easier for the caller to automate code generation for his or her client library.

If you want to mitigate this, you can generate the SDKs for your REST service yourself. Or, you can just be opinionated and deal with the complaining :)


问:
Should I use structure from a core library graphic toolkit in my domain?
In java (and many other programming language), there are often structure to deal with graphic element : Colour, Shape, etc. Those are most often in a UI toolkit and thus have a relatively strong coupling with UI element.

Now, in the domain of my application, we often deal with colour, shape, etc, to display statistic information on an element. Right now all we do with it is display/save those element with little or no behaviour. Would it make sense to avoid "reinventing the wheel" and directly use the structures in java.awt.* or should I make my own element and avoid a coupling to this toolkit? Its not like those element are going away anytime soon (they are part of the core java library after all), but at the same time it feel weird to import java.awt.* server side. I have no problem using java.util.List everywhere. Should I feel different about those class?

What would be the "recommended" practice in that case?

答: As long as you don't persist it. Once you're making RDBMS schema to hold the structure, you should rethink your approach.</p>


答:
Often when a collection is going to be the most useful way of interacting with your api, it can be simpler to just think of a single as a collection with only one member. Then if later you want to expose an API for interacting with singles, it merely needs to under the covers convert the single passed in to a collection with that member and then it uses the identical implementation of the other API.

I guess what I'm saying here is, if you want collections to be an interaction mechanism, start with the collection only API and after the system is complete, the other API is a simple ancillary attatchment at the interface layer you can add then if you find it particularly useful. You may however find it's usefulness is minimal enough to apply YAGNI and just use the collections interface for the few instances of singles you want.

Title: Is it reasonable for REST resources to be singular and plural?

Body:
I have been wondering if, rather than a more traditional layout like this:

```<code>api/Products
GET // gets product(s) by id
PUT // updates product(s) by id
DELETE // deletes (product(s) by id
POST // creates product(s)
</code>```

Would it be more useful to have a singular and a plural, for example:

```<code>api/Product
GET // gets a product by id
PUT // updates a product by id
DELETE // deletes a product by id
POST // creates a product

api/Products
GET // gets a collection of products by id
PUT // updates a collection of products by id
DELETE // deletes a collection of products (not the products themselves)
POST // creates a collection of products based on filter parameters passed
</code>```

So, to create a collection of products you might do:

```<code>POST api/Products {data: filters} // returns api/Products/<id>
</code>```

And then, to reference it, you might do:

```<code>GET api/Products/<id> // returns array of products
</code>```

In my opinion, the main advantage of doing things this way is that it allows for easy caching of collections of products. One might, for example, put a lifetime of an hour on collections of products, thus drastically reducing the calls on a server. Of course, I currently only see the good side of doing things this way, what's the downside?


问:
What can go wrong if the Liskov substitution principle is violated?
I was following this highly voted question on possible violation of Liskov Substitution principle. I know what the Liskov Substitution principle is, but what is still not clear in my mind is what might go wrong if I as a developer do not think about the principle while writing object-oriented code.</p>

答:
You can also look at it from a modelling point of view. When you say that an instance of class A is also an instance of class B you imply that "the observable behavior of an instance of class A can also be classified as observable behavior of an instance of class B" (This is only possible if class B is less specific than class A.)

So, violating the LSP means that there is some contradiction in your design: you are defining some categories for your objects and then you are not respecting them in your implementation, something must be wrong.

Like making a box with a tag: "This box contains only blue balls", and then throwing a red ball into it. What is the use of such a tag if it shows the wrong information?


问:
Can higher-order functions in FP be interpreted as some kind of dependency injection?
According to this article, in object-oriented programming / design dependency injection involves

a dependent consumer,

a declaration of a component's dependencies, defined as interface contracts,

an injector that creates instances of classes that implement a given dependency interface on request.

Let us now consider a higher-order function in a functional programming language, e.g. the Haskell function

```<code>filter :: (a -> Bool) -> [a] -> [a]
</code>```

from Data.List. This function transforms a list into another list and, in order to perform its job, it uses (consumes) an external predicate function that must be provided by its caller, e.g. the expression

```<code>filter (\x -> (mod x 2) == 0) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</code>```

selects all even numbers from the input list.

But isn't this construction very similar to the pattern illustrated above, where

the filter function is the dependent consumer,

the signature (a -> Bool) of the function argument is the interface contract,

the expression that uses the higher-order is the injector that, in this particular case, injects the implementation (\x -> (mod x 2) == 0) of the contract.

More in general, can one relate higher-order functions and their usage pattern in functional programming to the dependency injection pattern in object-oriented languages?

Or in the inverse direction, can dependency injection be compared to using some kind of higher-order function?

答:
An important concept in programming is the distinction between bound and free variables.

Dependency injection is concerned with converting free things into bound things.  This makes the code more general, easier to test, easier to mock, reduces coupling because the code knows less about its dependencies, etc.

Examples -- first with free things, then with bound things:

In this example, the first Foo knows that Bar has a zero-arg constructor, and knows the exact type of the instance.  The second example doesn't know anything about how bar is created, and could even be getting a subclass:

```<code>class Foo {
    private Bar bar;
    Foo() {
        this.bar = new Bar();
    }
}

class Foo{
    private Bar bar;
    Foo(Bar bar) {
        this.bar = bar;
    }
}
</code>```

In this example, pred appears free in myFilter but bound in filter.  Thus myFilter is far less general.  (Side note:  the value [] and the function : appear free in both functions -- if they were instead bound, the result could be even more general!):

```<code>-- assuming 'pred :: Integer -> Bool' is defined elsewhere
myFilter :: [Integer] -> [Integer]
myFilter  [] = []
myFilter (x:xs)
  | pred x        = x : myFilter xs
  | otherwise     = myFilter xs

filter :: (a -> Bool) -> [a] -> [a]
filter   _    []    = []
filter pred (x:xs)
  | pred x         = x : filter pred xs
  | otherwise      = filter pred xs
</code>```

Here's a type-level example.  The type constructor [] appears free in map, whereas in fmap,  the specific Functor is bound.  (I hope that example isn't too confusing; map itself is a higher-order function.  :)  )

```<code>map :: (a -> b) -> [a] -> [b]

fmap :: Functor f => (a -> b) -> f a -> f b
</code>```

In both examples, the code with fewer free variables, and more bound ones, is a more general version.

tl,dr; yes, dependency injection is similar to higher-order functions.

More examples demonstrating bound vs. free variables (because I think they're interesting):

This function has no free variables, because every variable that is used in the body is a parameter:

```<code>flip                    :: (a -> b -> c) -> b -> a -> c
flip f x y              =  f y x
</code>```

In this example, f and g are bound from the point of view of (.), but free from the P.O.V. of the lambda abstraction (\x -> f (g x)):

```<code>(.)    :: (b -> c) -> (a -> b) -> a -> c
(.) f g = \x -> f (g x)
</code>```

Body:
An important concept in programming is the distinction between bound and free variables.

Dependency injection is concerned with converting free things into bound things.  This makes the code more general, easier to test, easier to mock, reduces coupling because the code knows less about its dependencies, etc.

Examples -- first with free things, then with bound things:

In this example, the first Foo knows that Bar has a zero-arg constructor, and knows the exact type of the instance.  The second example doesn't know anything about how bar is created, and could even be getting a subclass:

class Foo {
    private Bar bar;
    Foo() {
        this.bar = new Bar();
    }
}

class Foo{
    private Bar bar;
    Foo(Bar bar) {
        this.bar = bar;
    }
}

In this example, pred appears free in myFilter but bound in filter.  Thus myFilter is far less general.  (Side note:  the value [] and the function : appear free in both functions -- if they were instead bound, the result could be even more general!):

-- assuming 'pred :: Integer -> Bool' is defined elsewhere
myFilter :: [Integer] -> [Integer]
myFilter  [] = []
myFilter (x:xs)
  | pred x        = x : myFilter xs
  | otherwise     = myFilter xs

filter :: (a -> Bool) -> [a] -> [a]
filter   _    []    = []
filter pred (x:xs)
  | pred x         = x : filter pred xs
  | otherwise      = filter pred xs

Here's a type-level example.  The type constructor [] appears free in map, whereas in fmap,  the specific Functor is bound.  (I hope that example isn't too confusing; map itself is a higher-order function.  :)  )

map :: (a -> b) -> [a] -> [b]

fmap :: Functor f => (a -> b) -> f a -> f b

In both examples, the code with fewer free variables, and more bound ones, is a more general version.

tl,dr; yes, dependency injection is similar to higher-order functions.

More examples demonstrating bound vs. free variables (because I think they're interesting):

This function has no free variables, because every variable that is used in the body is a parameter:

flip                    :: (a -> b -> c) -> b -> a -> c
flip f x y              =  f y x

In this example, f and g are bound from the point of view of (.), but free from the P.O.V. of the lambda abstraction (\x -> f (g x)):

(.)    :: (b -> c) -> (a -> b) -> a -> c
(.) f g = \x -> f (g x)


答:
I would apply YAGNI here and use the structures from the standard library, however I would be careful to ensure all parts of the domain using this stuff are as close together in the system as possible. This will make searching out the usage for replacement later trivial in case you do end up needing a decorator on top of these structures.

You don't want these structures to end up spread all over your code base unless you're absolutely certain the requirements of them will never change (hint: requirements of every piece of code you ever write will always change). I guess I should also say, if their only purpose is for the code to be used by client side pieces which rely on the java standard implementations then you're probably safe treating them as primitives.

Title: Should I use structure from a core library graphic toolkit in my domain?

Body:
In java (and many other programming language), there are often structure to deal with graphic element : Colour, Shape, etc. Those are most often in a UI toolkit and thus have a relatively strong coupling with UI element.

Now, in the domain of my application, we often deal with colour, shape, etc, to display statistic information on an element. Right now all we do with it is display/save those element with little or no behaviour. Would it make sense to avoid "reinventing the wheel" and directly use the structures in java.awt.* or should I make my own element and avoid a coupling to this toolkit? Its not like those element are going away anytime soon (they are part of the core java library after all), but at the same time it feel weird to import java.awt.* server side. I have no problem using java.util.List everywhere. Should I feel different about those class?

What would be the "recommended" practice in that case?


答:
Human Readable means I can open the thing in Notepad if I want to, and change "password=foo" to "password=bar" if I so desire. I don't have to use a proprietary tool to look at or edit the content.

Contrast to a PDF which you cannot edit with a simple text editor - you need a specific tool that knows the format. Or a binary .dat file that came with some application from 25 years ago that you can't read, edit, or understand.

Title: What does "Human Readable" mean? Is it a misnomer?

Body:
Two examples spring to mind:

One of the reasons that .Net programmers are encouraged to use .config files instead of the Windows Registry is that .config files are XML and therefore human-readable.

Similarly, JSON is sometimes considered human-readable compared with a proprietary format.

Are human-readable formats actually readable by humans? In the example of configuration data:

The format doesn't change the underlying meaning of the information - in both cases, the data represents the same thing.

Both registry and .config file are stored internally as a series 0s and 1s. To that extent, the underlying representaion is equally unreadable by humans.

Both registry and .config file require a tool to read, format and display those 0s and 1s and convert them into a format that humans can read. In the case of configuration stored in the Windows Registry, this is a Registry Editor. In the case of XML it could be a text editor or XML reader. Either way, the tool makes the data readable, not the data format.

So, what is the difference between human-readable data formats and non-human-readable formats?


答:
I think, you can use some stuff like a django-extensions

Django-Extensions Website

There is a module - jobs. For me this is a very good tool to control your cronjobs.

Second option is use a Fabric and create a function for this.

And I see third way. Use your imagination and knowledge and create own function
with subprocess and sh.

Title: Execute code every hour


问:
How do operating systems… run… without having an OS to run in?
I'm really curious right now. I'm a Python programmer, and this question just boggled me: You write an OS. How do you run it? It has to be run somehow, and that way is within another OS?

How can an application run without being in an OS? How do you tell the computer to run, say, C, and execute these commands to the screen, if it doesn't have an OS to run in?

Does it have to do with a UNIX kernel? If so, what is a Unix kernel, or a kernel in general?

I'm sure OSes are more complicated than that, but how does it work?

答:
Answer to your question requires the knowledge of how the native (for CPU) code looks like and how is it interpreted by CPU.

Usually whole compilation process is based of translating things you write in C, Pascal or even Python (using pypy) and C# into things CPU understands, i.e. simple instructions like "store something under [memory address]", "add numbers stored under registers eax and ebx", "call function foo", "compare eax to 10". Those instructions, executed one by one, do things you wanted to do with your code.

Now think about this: you don't really need an OS to execute this native code! All you need is to load this code into memory and tell the CPU it's there and you want it to be executed. Don't be concerned too much by that, though. That's the job BIOS should worry about - it loads your code (one and one sector only), just after the CPU starts, under physical address 0x7C00. Then CPU start to execute this one sector (512 B) of your code. And you can do whatever you imagine! Without, of course, any support from the OS. That's because YOU are the operating system. Cool, huh? No standard library, no boost, no python, no programs, no drivers! You have to write everything by yourself.

And how do you communicate with hardware? Well, you have two choices:

You stay inside of "Real Mode" - CPU execution mode with only 1 MB of memory (and even less), no advanced CPU features like CPU extensions, memory protection, multitasking; 16-bit executable code, ancient addressing mode... But with some routines provided by BIOS, including simple screen output, keyboard support, disk I/O and power management. In one word, you're back in times of MS-DOS and 16-bit CPUs.

You go under "Protected Mode" with all the features your CPU have, all the memory you have installed and so on. But in Protected Mode you ARE completely alone and you have to do EVERYTHING by yourself (and you communicate with hardware using "in" and "out" instructions to input/output data to I/O ports and using interrupts. And memory-mapped I/O). Do I have to say every OS since Windows 95 and very first Linux chooses this option?

Now you're asking what kernel is.
Shortly, kernel is everything you don't see and experience directly. It manages, along with drivers, everything, beginning from your keyboard to almost every piece of hardware inside your PC. You communicate with it by graphical shell or terminal. Or by functions inside your code, now executed, luckily, with support of the OS.

For better understanding I can give you one advice: try to write your own OS. Even if it's gonna write "Hello world" on the screen.


答: If your company is in the target market for such a software product, you can become your own customer.  See "Eat your own dog food".</p>

Title: What methods of requirements elicitation are suitable when I do not have direct access to the user base?

Body: I am working on an application to create invoices. There are some features that are required based on the type of the application and are common to all invoice applications. However, we still need to determine what unique needs the user base might have. We do not have direct access to the users to obtain requirements or user stories. What techniques are most suitable for eliciting high-quality requirements from users when direct or frequent access is not possible?</p>


答:
There are some differences for how an operating system operates that are extremely system dependent.  To be useful a system needs to have some predictable behavior on start-up, such as "start executing at address X".  For systems that have non-volatile storage (such as Flash memory) mapped into their program space this is fairly easy since you just make sure that you put start-up code at the right location within the processor's program space.  This is extremely common for microcontrollers.  Some systems have to retrieve their start-up programs from some other location before executing it.  These systems will have some operations hardwired (or almost hardwired) into them.  There are some processors that retrieve their start-up code via i2c from another chip, so the processor does non-trivial operations without running any assembly instructions and then starts executing instructions at a predefined address.

Systems using the x86 family of processors typically use a multi-stage boot process that is fairly complex due to its evolution and backwards compatibility issues.  The system executes some firmware (called BIOS - Basic Input/Output System, or similar) that is in some non-volatile memory on the motherboard.  Sometimes some or all of this firmware is copied (relocated) into RAM to make it execute faster.  This code was written with knowledge of what hardware would be present and usable for booting.

Start-up firmware is usually written with assumptions about what hardware will be present on the system.  Years ago on a 286 machine there would probably be an assumption that there would be a floppy drive controller at I/O address X and would load sector 0 to a certain memory location if given a certain set of commands (and the code at sector 0 knows how to use the BIOS's own functions to load more code, and eventually enough code to be an OS is loaded).  On a microcontroller there may be an assumption that there is a serial port operating with certain settings that it should wait for commands (to update more complex firmware) from for X amount of time before continuing with the boot process.

The exact start-up process of a given system isn't as important for you as knowing that it differs on different systems, but also that they all have things in common.  Often within the start-up (bootstrapping) code when I/O needs to be done the I/O devices are polled rather than relying on interrupts.  This is because interrupts are complex, use stack RAM (which may not be fully set up yet), and you don't need to worry about blocking other operations when you are the only operation.

Upon first being loaded the OS kernel (the kernel is the main part of most OSes) will initially act a lot like the firmware.  It will need to either be programmed with knowledge of or discover hardware present, set up some RAM as stack-space, do various tests, set up various data structures, possibly discover and mount a filesystem, and then probably start some program that is more like the programs you are used to writing (a program that relies on an OS being present).

OS code is usually written in a mixture of C and assembly.  The very first code for the OS kernel  is probably always in assembly and does things like set up the stack, which C code relies on, and then calls a C function.  Other hand written assembly will be in there too because some operations that an OS needs to do are often not expressible in C (like context switching/swapping stacks).  Often special flags have to be passed to the C compiler to tell it not to rely on the standard libraries that most C programs use and to not expect that there is an int main(int argc, char *argv[]) in the program.  Additionally special linker options that most application programmers never use have to be used.  These may make the kernel program expect to be loaded at a certain address or set up things to look like there are external variables at certain locations even though those variables were never declared in any C code (this is useful for memory mapped I/O or other special memory locations).

The whole operation seems like magic at first, but after you look into it and understand parts of it the magic becomes just a set of programs that require a lot more planning and system knowledge to implement.  Debugging them, though, takes magic.