Formatting multiple line statement in vim - vim

In vim a long statements like
if ( (image == null) || (image.getFileHash() == null) || (image.getFileImage() == null) )
gets indented like this when I hit an enter before the second OR
if ( (image == null) || (image.getFileHash() == null)
|| (image.getFileImage() == null) ) {
is there a way to set vim to indent it like
if ( (image == null) || (image.getFileHash() == null)
|| (image.getFileImage() == null) ) {
i.e., aligning it with the column after ( of the first previous line.
With dash-tom-bang's help was able to solve a part of the
problem. However, now the curly brackets are not aligned as expected
if { starts on the same line.
if ( (image == null) || (image.getFileHash() == null) ||
(image.getFileImage() == null) )
{
}
if ( (image == null) || (image.getFileHash() == null) ||
(image.getFileImage() == null) ) {
}

The setting that controls this is cinoptions, see :help cinoptions-values.
Add this to your .vimrc to align with the most local unclosed parenthesis. (You can give extra indent in spaces by giving a different number, including negatives. You can indent N tabstops by using Ns.)
set cinoptions=(0
The default value is 2s, which would indent two tabs past the open paren; hopefully this is what you're seeing. :)

Related

Modifying a Linq query to add another condition

I have a method below pulling specific fields that have null values.
Every record in the table has a status field of 1 or 0. 1 is active and 0 is inactive.
For the query below I only want to pull data for active records i.e status = 1
How can I modify this query to put in the condition for status = 1?
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}
You can chain two Where Linq extension method calls right after one another, like so:
public List<FileModel> GetMyData()
{
var collectedData = _context.MyUploads.AsNoTracking().Where(t =>
t.FirstName == null ||
t.LastName == null ||
t.EmailAddress == null ||
t.Telephone == null)
.Where(t => t.status == 1) // added this line...
.Select(x => _mapper.Map<FileModel>(x))
.ToList();
return collectedData;
}
you should just be able to change the where condition to the below
.Where(t => (t.FirstName == null || t.LastName == null || t.EmailAddress == null || t.Telephone == null) && t.status == 1)
The && means and, so its saying those fields are null and the status equals 1.

Nested if statements...or use Switch Case?

I have a large nested if. One portion of the nested if works, however, in the second portion only some of the nested if works. I cannot get the switch case to work.
//Match Scoring //All work, when I flip Poker on top of Match the first and second ifs do not work
if playerMatchCardsArray[0] == playerMatchCardsArray[1] &&
playerMatchCardsArray[1] == playerMatchCardsArray[2]{
} else if playerMatchCardsArray[0] == playerMatchCardsArray[1] ||
playerMatchCardsArray[1] == playerMatchCardsArray[2] ||
playerMatchCardsArray[0] == playerMatchCardsArray[2]{
} else if playerMatchCardsArray[0] != playerMatchCardsArray[1] ||
playerMatchCardsArray[1] != playerMatchCardsArray[2] ||
playerMatchCardsArray[0] != playerMatchCardsArray[2] {
//Poker Scoring //All work, when I flip Poker on top of Match. However the following lines work do not work
// SUITED THREE OF A KIND //does not work
if firstRandomNumber == secondRandomNumber && secondRandomNumber == thirdRandomNumber {
// STRAIGHT FLUSH // works
} else if straightCardsArray[0] == 1 && straightCardsArray[1] == 2 && straightCardsArray[2] == 3 &&
flushFirstRandomNumber == flushSecondRandomNumber &&
flushFirstRandomNumber == flushThirdRandomNumber{
// THREE OF A KIND // does not work
} else if pairFirstRandomNumber == pairSecondRandomNumber &&
pairFirstRandomNumber == pairThirdRandomNumber {
//STRAIGHT FORMULA // works
} else if straightCardsArray[0] == 1 && straightCardsArray[1] == 2 && straightCardsArray[2] == 3 {
// SUITED PAIR // does not work
} else if firstRandomNumber == secondRandomNumber ||
secondRandomNumber == thirdRandomNumber ||
firstRandomNumber == thirdRandomNumber {
// PAIR // does not work
} else if pairFirstRandomNumber == pairSecondRandomNumber ||
pairFirstRandomNumber == pairThirdRandomNumber ||
pairSecondRandomNumber == pairThirdRandomNumber {
// FLUSH // works
} else if flushFirstRandomNumber == flushSecondRandomNumber &&
flushFirstRandomNumber == flushThirdRandomNumber {
Thank you in advance for your time and assistance. It's greatly appreciated!
It was the '}' that was causing the issue.

Netsuite: Server-side code not firing for csv uploads?

I created a script and deployed it to automatically populate web store fields when new inventory items are created in our system.
The code works when a new item is created through the interface, but does not when a new item is uploaded via csv.
This is the code:
function userEventAfterSubmit(type) {
if (type == 'create') {
var newItem = nlapiLoadRecord('inventoryitem', nlapiGetNewRecord().getId());
var storeDisplayImage = nlapiGetFieldValue('storedisplayimage');
if (storeDisplayImage == '' || storeDisplayImage == null)
newItem.setFieldValue('storedisplayimage', 620128);
var storeDisplayThumbnail = nlapiGetFieldValue('storedisplaythumbnail');
if (storeDisplayThumbnail == '' || storeDisplayThumbnail == null)
newItem.setFieldValue('storedisplaythumbnail', 620127);
var urlComponent = nlapiGetFieldValue('urlcomponent');
if (urlComponent == '' || urlComponent == null)
newItem.setFieldValue('urlcomponent', nlapiGetFieldValue('storedisplayname'));
var pageTitle = nlapiGetFieldValue('pagetitle');
if (pageTitle == '' || pageTitle == null)
newItem.setFieldValue('pagetitle', nlapiGetFieldValue('storedisplayname'));
var storeDescription = nlapiGetFieldValue('storedescription');
if (storeDescription == '' || storeDescription == null)
newItem.setFieldValue('storedescription', nlapiGetFieldValue('salesdescription'));
var storeDetailedDescription = nlapiGetFieldValue('storedetaileddescription');
if (storeDetailedDescription == '' || storeDetailedDescription == null)
newItem.setFieldValue('storedetaileddescription', nlapiGetFieldValue('salesdescription'));
var metaTagHtml = nlapiGetFieldValue('metataghtml');
if (metaTagHtml == '' || metaTagHtml == null)
newItem.setFieldValue('metataghtml', '<meta name="description" content="' + nlapiGetFieldValue('salesdescription') + '">');
nlapiSubmitRecord(newItem);
}
}
And then this function is called as the "After Submit Function". Am I not calling in this in the right place for it to run for csv uploads?
This is my script deployment:
Goto "Setup > Import/Export > CSV Import preferences"
Make sure " RUN SERVER SUITESCRIPT AND TRIGGER WORKFLOWS" is checked.

Avoiding Multiple If's in c# - Best practise

Scenario:
Lets say we got to check for address lines. which includes addressline1, addressline2,Town,Country,Postcode
If any one of the property is entered, all other fields are mandatory.
If none of it is entered, the validation doesnt have to get trigged.
To achieve it, I ended up with two lines of If statement.
Like
if(AddressLine1 != null || AddressLine2 != null || Town != null || Country != null)
{
if(AddressLine1 != null && AddressLine2 != null && Town != null && Country != null) == false
{
return false;
}
}
Note: I am using c#. Are there any language constructs i can make use of.
private bool IsAddressValid(params string[] addressParts)
{
return addressParts.Any(p => p != null) ? addressParts.All(p => p != null) : true;
}
To be called like so:
var addressValid = IsAddressValid(AddressLine1, AddressLine2, Town, County);
Well, the null-coalescing operator can help with the first:
if (AddressLine1 ?? AddressLine2 ?? Town ?? Country != null)
{
if (AddressLine1 == null || AddressLine2 == null ||
Town == null || Country == null)
{
return false;
}
// Presumably there's more here
}
You might want to write some helper methods though:
if (IsAnyNonNull(AddressLine1, AddressLine2, Town, Country))
{
if (IsAnyNull(AddressLine1, AddressLine2, Town, Country))
{
return false;
}
}
Where the utility methods would be something like:
public static bool IsAnyNonNull(params object[] values)
{
return values.Any(x => x != null);
}
public static bool IsAnyNull(params object[] values)
{
return values.Any(x => x == null);
}
Of course, you've still got two if statements - but I think that's basically necessary here anyway.
If you make an array of the fields in the group, then you can do:
var fields = new object[] {AddressLine1, AddressLine2, Town, Country};
return fields.All(f => f == null) || fields.All(f => f != null);
Define this:
public static bool SameNullness(params object[] values)
{
int nullCount = 0;
foreach (var value in values)
{
if (value == null) nullCount++;
}
return nullCount == values.Length;
}
Then use it like:
SameNullness(AddressLine1, AddressLine2, Town, Country);

Simple IF statement question

How can I simply the below if statements?
if ( isset(var1) & isset(var2) ) {
if ( (var1 != something1) || (var2 != something2) ) {
// ... code ...
}
}
Seems like this could be condensed to only one IF statement but am not certain if I'd use an AND or OR
Boolean varsAreSets = isset(var1) & isset(var2); // or some other name that indicates what this is doing
Boolean someMeaningfulName = (var1 != something1) || (var2 != something2); // would suggest a meaningful name but don't know what this is accomplishing
if ( varsAreSets && someMeaningfulName ) {
// ... code ...
}
This makes the code very readable and helps you and whoever reads the code understand what these checks are actually doing.
if (isset(var1) && ((var1 != something1) || (var1 != something2)))
// ... code ...
}
You would use an and because you can only get to the // ... code ... part if both if-statements are true.
You can do:
if(isset(var1) && isset(var2) && ( (var1 != something1) || (var1 != something2) ) ){
//..code
}
As a general example:
if( cond1 && cond2 ) {
if( cond3 || cond4) {
// ...code..
}
}
The code will be executed only when both cond1 and cond2 are true and either of cond3 or cond3 is true.
It's a question of in what order your computer interprets boolean logic:
Take for example the following conditions:
A: False
B: True
if you were to write if (A && B) what your computer actually does is think:
Is A true? No.
Well, A and B can't be true because A isn't true. Therefore this statement is false. [computer ignores the rest of the logic]
Because of this, when you evaluate the statement isset(var1) && ( (var1 != something1) || (var1 != something2) ) it first checks isset(var1) and if that's false, then it skips the rest of the condition, just like your double-if statement.
if ( isset(var1) && isset(var2) && ( (var1 != something1) || (var2 != something2) ) ) {
// ... code ...
}
if (isset(var1) && isset(var2) && ((var1 != something1) || (var2 != something2)))
{
// ... code ...
}
Another option:
if (isset(var1) && isset(var2)
&& !(var1 == something1 && var2 == something2)) {
...
I think most of the examples above that have 1 IF may spit out an error if var1 or var2 is NOT set
(isset($var1) && isset($var2)) ? ($var1!='something1' && $var2!='something2') ? $go=TRUE : $go=FALSE : $go = FALSE;
if ($go){
echo 'hello';
}

Resources