New answers tagged

-2

The preprocessor has already given meaning to newline characters. You can't completely undo that at a higher level. Compare: char s1[] = "This is how macros work in C\nExample\n #define IS_GOOD 1\n"; with char s2[] = "This is how macros work in C Example #define IS_GOOD 0 "; Clearly the second is easier to read (in a hypothetical C compiler ...


-2

Wikipedia has an entry with some details: Two problems with multiline string literals are leading and trailing newlines, and indentation. If the initial or final delimiters are on separate lines, there are extra newlines, while if they are not, the delimiter makes the string harder to read, particularly for the first line, which is often indented ...


-2

I cannot answer on the "why"; as far as I know language designers tend to copy the "bad stuff" as many times as the "good stuff" when designing a language based on other languages. I do have to say that using RegEx to parse your code is not the best way to do it and writing a parser which can keep track of multiline strings might be harder than you would ...


3

FWIW, Ocaml accepts a limited form of multi-line string literal : String literals are delimited by " (double quote) characters. The two double quotes enclose a sequence of either characters different from " and \, or escape sequences from the table given above for character literals. To allow splitting long string literals across lines, the sequence ...


10

What happens when you didn't mean to have a multi-line string, but instead forgot to close the quote? The parser will chew through the code until it hits another quote in a completely different part of the program, then proceed as normal. This will very likely lead to confusing, unrelated errors since the string is no longer the parse error. At worst, you ...


0

One possibility is to (ab)use maps and sets (or lists). You have a map with "current word" as key and "list or set of successor words" as value. You can then populate your map using something like the following: for word in dictionary: for 0 <= position < length(word): key = drop_letter(word, position) ## drop_letter("bath", 3) -> bat ...


1

Is there a reason why C#/JS require explicitly differentiating a declaration from assignment? There is, you don't want typos to declare new variables, you want them to not work. For example, consider the following Python code: someValue = 42 if someCondition: someVaule = 43; This is clearly a bug caused by a typo, but Python does not prevent you ...


0

You need to learn more programming languages, there is much more than C# & Javascript & C++ & Python. So read Scott's  Programming Language Pragmatics book & SICP (a very good introductory book that would teach you Scheme) In particular, variables in Ocaml, Scheme, Common Lisp, Haskell are quite different than what you believe (or what they ...


3

there are two distinct steps involved, "creating" a variable, and "assigning" a value. Think of it like "building a house" and "renting a house". the house may only be built once, but can be rented an infinite number of times. Different languages handle it differently because some language designers think its a good idea to let a house be built whenever you ...


10

But that's easy - just check if foo was declared earlier And therein lies your problem. In languages like Javascript, it's all too easy to "shadow" a variable that you've already defined earlier without knowing it, and now you've got strange behavior that you can't explain. The problem is exacerbated in Javascript because all variables not otherwise ...


6

This depends heavily on the filesystem and the operating system. For example, on Unix, there are no folders at all: Windows has completely different terminology. Classic MacOS had a different terminology, which, in OSX, is now mixing with Unix terminology.


5

It really depends upon the file system and the operating system. In POSIX and Unix, a file can be a directory, a plain file, a block device, a character device, a FIFO, a socket, a symbolic link, ... See POSIX stat & Linux stat(2), notice that file can have different types. So if you want to speak of a file whose type is plain and which is a sequence ...


2

In a pure language like Haskell, all data is immutable and no existing data structures can be changed in any way Actually that is not generally true. Pure languages use non-strict (lazy) evaluation so the evaluation of potentially all subexpressions is deferred. Unevaluated expressions are generally heap allocated as a "thunk". When required the ...


-2

Perhaps the most common example of late binding is resolving Internet URLs. It supports dynamic systems and large systems without trying to link and bind every site in the world before you can reach any, but on the other hand it does incur some overhead (DNS lookup, much less IP routing) at runtime. By that light, most varieties of binding in language ...


3

(As promised, my experience-based opinion.) First of all, if you can prevent it: do not mix languages, whenever possible. As per your example, it seems like you are mostly wondering about how it should be done in a templating language mixed with HTML, which I will cover. (Another case would be building SQL using another programming language, which would ...


5

This is one of the primary reasons for avoiding systems that mix two languages together like this. It can get really difficult to check how the control flow of the outer language affects the block structure of the inner language, and when you can't see that, bugs happen. I prefer to keep them separated and when generating output in another structured ...


11

In my opinion, the main principle for good coding is minimize the intermixture of code. Two-language files are inherently a mess to work with. Your priority should be to minimize the extent to which the two languages are intertwined. For example, in a template file, the logic implemented by the templating language should be kept at a bare minimum. ...


1

QUESTION: Is it a good or a bad style to replace this pointer with variables and why? To me, if we try to go as language agnostic as possible, then a lot of the temptation to give this a more meaningful name seems kind of counter-productive. Focus on Abstract Interfaces First, a lot the flexibility of languages that allow you to write ...


8

I think there are two layers to this question (pun intended) Note: I assume that you are referring to javascript, and I base my answer in that language. 1) this refers to the object that invokes the function (or the object that the function was bound too, but we don't need to get into all of that...) Why is this important? Because, in javascript, ...



Top 50 recent answers are included