Friday, March 2, 2018

Why and When to use Padding vs. Margin


To me the biggest difference between padding and margin is that vertical margins auto-collapse, and padding doesn't. Consider two elements one above the other each with padding of 1em. This padding is considered to be part of the element, and is always preserved. So you will end up with the content of the first element, followed by the padding of the first element, followed by the padding of the second, followed by the content of the second element. Thus content of the two elements will end up being 2em apart.

Now replace that padding with 1em margin. Margins are considered to be outside of the element, and margins of adjacent items will overlap. So in this example you will end up with the content of the first element followed by 1em of combined margin followed by the content of the second element. So the content of the two elements is only 1em apart.

This can be really useful when you know that you want say 1em of spacing around an element, regardless of what element it is next to.




The other big difference is that padding is included in the click region and background color/image, but not the margin. Hence padding can be used to add more space around the anchor text of a text link in order to be better clickable on handheld devices; this cannot be done with margin since it adds space outside of the box where the contents (the anchor text) lie.

Since margin is on the outside of block elements while padding is on the inside, I tend to follow these usage guidelines:

  • Use margin to separate the block from things outside it
  • Use padding to move the contents away from the edges of the block.
TL;DR: By default I use margin everywhere, except when I have a border or background and want to increase the space inside that visible box.
 


Margin Padding
1  considered to be outside of the element considered to be part/inside of the element
2  vertical margins auto-collapse They do not collapse, never!!
3  Its outside the element, so wont work like this included in the click region and background color/image




2014 - Year of Custom ROM's

Hello!!
I see there are no posts for a full year. That's bad, it shows I  haven’t learnt anything new. But thats not completely true. I have been abusing my 18month old Sony Xperia-U with a variety of custom roms.

It all started with the unlocking problems; that button got so broken that i was barely able to use my mobile. Cutting the chase, i have used below OS so far.


Sunday, November 10, 2013

Bob, What's this CLOB and BLOB?

The main differences between BLOB and CLOB are:
  • BLOB stores values as LOB (Large OBject) in bitstreams.
  • CLOB stores values as LOB (Large OBject) in character steams.
CLOBs are for text like XML, JSON, etc. You should not assume what encoding the database will store it as (at least in an application) as it will be converted to whatever encoding the database was configured to use.

BLOBs are for binary data. You can be reasonably assured that they will be stored how you send them and that you will get them back with exactly the same data as they were sent as.


CLOB is encoding and collation sensitive, BLOB is not.

When you write into a CLOB using, say, CL8WIN1251, you write a 0xC0 (which is Cyrillic letter А).
When you read data back using AL16UTF16, you get back 0x0410, which is a UTF16 representation of this letter.

If you were reading from a BLOB, you would get same 0xC0 back.

Wednesday, July 3, 2013

Tail Call Optimization

A tail call is a subroutine call that happens inside another procedure as its final action; it may produce a return value which is then immediately returned by the calling procedure. The call site is then said to be in tail position, i.e. at the end of the calling procedure.

Tail because it has to happen as the very last operation in a function, or as the function's tail operation. Call because that last operation is calling a function. Making tail calls can sometimes (though not by any means always) require support from your platform's calling conventions, linker, and run-time library. It also needs a non-trivial amount of effort in the compilers that support it.

There's also a more restricted version, called tail recursion, which only happens if a function, as its last operation, returns the result of calling itself. Tail recursion is easier to deal with because rather than having to jump to the beginning of some random function somewhere, you just do a goto back to the beginning of yourself, which is a darned simple thing to do.

A recursive function is tail recursive if the final result of the recursive call is the final result of the function itself.  If the result of the recursive call must be further processed (say, by adding 1 to it, or consing another element onto the beginning of it), it is not tail recursive. Tail calls need not be recursive.

Tail calls are significant because they can be implemented without adding a new stack frame to the call stack. Most of the frame of the current procedure is not needed any more, and it can be replaced by the frame of the tail call, modified as appropriate (similar to overlay for processes, but for function calls). The program can then jump to the called subroutine. Producing such code instead of a standard call sequence is called tail call elimination, or tail call optimization. Tail call elimination allows procedure calls in tail position to be implemented as efficiently as goto statements, thus allowing efficient structured programming.

When a function is called, the computer must "remember" the place it was called from, the return address, so that it can return to that location with the result once the call is complete. Typically, this information is saved on the call stack, a simple list of return locations in order of the times that the call locations they describe were reached. For tail calls, there is no need to remember the place we are calling from – instead, we can perform tail call elimination by leaving the stack alone (except possibly for function arguments and local variables), and the newly called function will return its result directly to the original caller. The tail call doesn't have to appear lexically after all other statements in the source code; it is only important that the calling function return immediately after the tail call, returning the tail call's result if any, since the calling function will never get a chance to do anything after the call if the optimization is performed.

For non-recursive function calls, this is usually an optimization that saves little time and space, since there are not that many different functions available to call. When dealing with recursive or mutually recursive functions where recursion happens through tail calls, however, the stack space and the number of returns saved can grow to be very significant, since a function can call itself, directly or indirectly, creating new call stacks each iteration. It often asymptotically reduces stack space requirements from linear, or O(n), to constant, or O(1).

Besides space and execution efficiency, tail call elimination is important in the functional programming idiom known as continuation passing style (CPS), which would otherwise quickly run out of stack space.

Friday, October 26, 2012

Demangling the namemangling

Found an awesome command today. Thanks to the "symbol not found" error. As usual i was debugging using "nm" command. It tells me if a symbol is present or not, but it doesn't tell me what that symbol stands for.

tada...thats where "c++filt" comes to rescue.

The C++ and Java languages provides function overloading, which means that you can write many functions with the same name (providing each takes parameters of different types). All C++ and Java function names are encoded into a low-level assembly label (this process is known as mangling). The c++filt program does the inverse mapping: it decodes (demangles) low-level names into user-level names so that the linker can keep these overloaded functions from clashing.

Every alphanumeric word (consisting of letters, digits, underscores, dollars, or periods) seen in the input is a potential label. If the label decodes into a C++ name, the C++ name replaces the low-level name in the output.

$ c++filt -n -s gnu-v3 _ZN8BcastMsg10mBroadcastE6AppMsgz
BcastMsg::mBroadcast(AppMsg, ...)
 

Ain't she beautiful?